Category: raspberry-pi

Fixing an unbootable Raspberry Pi

Fixing an unbootable Raspberry Pi

Raspberry-Pi
Image by Benjamin Nelan from Pixabay

Well to be fair, it was me that made it unbootable. I’d been reading this Wiki page on configuring the Pi. I’d told it to give the GPU almost a GB of RAM. The Pi is a 4 GB Pi 4B.  I did it last night and so this morning, I found it not working at all well.

My first thoughts were I’d messed up the Operating system and so I took the SD card put it in a holder and booted up my old laptop which has Ubuntu 18.04 LTS on it. This page on switchdoc suggested I could do a repair with these commands.

First lsblk to view attached devices. There was a /dev/sdc2 . There was also a /dev/sdc1

Then

sudo fsck -fy /dev/sdc1

That gave information about the drive but not the disk. That took

sudo fsck -fy /dev/sdc2

That took a few seconds and listed information, but still my drive wouldn’t boot back in the Pi.

Then I remembered I’d changed the boot config.txt and it was back with the SD card in the laptop and rebooted that. It showed two devices on the desktop (no need to mount anything) and clicking boot gave me a directory listing of /boot. I edited config.txt and changed the GPU Mem value to 256MB.

That fixed it and my PI is now booting quite happily again. I am now going to make a backup copy! It wouldn’t the end of the world if I had made it permanently unbootable, I’d just burn the OS again. It’s just the time wasted and minor hassles copying files, downloading and reinstalling software. Best avoided if possible!

 

 

Success with gamepad mappings

Success with gamepad mappings

SNES Gamepad
Image by Marco Sberveglieri from Pixabay

My ploy with running gamepad-tool on the Linux laptop worked and I was able to get the correct mappings to use in my game. Though weirdly it seems to have X and Y buttons mixed up as well as A and B.

In the end I hard-coded the mapping string and set it up with this code:

	if (SDL_GameControllerAddMapping("030000001008000001e5000010010000,NEXT SNES Controller2,platform:Linux,a:b2,b:b1,x:b3,y:b0,back:b8,start:b9,leftshoulder:b4,rightshoulder:b5,dpup:-a1,dpdown:+a1,dpleft:-a0,dpright:+a0")==-1){
		LogError("Unable to load gamepad mappings from gamepad.txt");
	}

Then in my ProcessEvents() function which handles I/O events, I added this code:

			case SDL_CONTROLLERBUTTONDOWN:
			if (event.cbutton.state== SDL_PRESSED){
				switch(event.cbutton.button){
					case SDL_CONTROLLER_BUTTON_A:
					    fireFlag =1;
					    break;
					case SDL_CONTROLLER_BUTTON_B:						
					    jumpFlag =1;
					    break;
					case SDL_CONTROLLER_BUTTON_X:
					    shieldFlag =1;
					    break;
					case SDL_CONTROLLER_BUTTON_Y:						
					    thrustFlag =1;
					    break;						
					case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
					    rotateFlag = 1;
					    break;
					case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
					    rotateFlag= 2;				
					    break;
					}
				}

There’s similar code for the SDL_CONTROLLERBUTTONUP except it checks for the state = SDL_RELEASED and in the switch statement it sets each flag to 0. So inspite of the code, it’s the Y button that does shields and the X button does thrust, B button fires and A button does the hyper space jump.

More on Raspberry Pi Gamepad

More on Raspberry Pi Gamepad

jstest-gtkUnfortunately there’s no gamepad-tool for the PI, but there is a program called jstest and in particular a visual one called jstest-gtk. That’s it on the left. You install it on any Linux system, including Raspbian on the Pi with the command

sudo apt install jstest-gtk

Then run it with a jstest-gtk command from a terminal.

It then detects your game pad and responds to button presses and joystick movements.

The weird thing is although my gamepad identifies it as a NEXT SNES but so far attempts to load that have not been successful in the Asteroids game. It loads but doesn’t seem to work and its not recognising the right shoulder button and has A and B buttons muddled up. Yet in jstest, it is clearly able to identify them.

Time I think to create a mapping under Linux with gamepadtool. Unfortunately it doesn’t work on the Hyper-V Ubuntu so it’s time to fetch my Linux laptop and try it on that.

 

My Orange Pi – self resurrected

My Orange Pi – self resurrected

USB JoypadBack in November 2018, I was in Nottingham and passed a stand at the Winter Fayre. It was offering a console with 18,000 games on it for about £70. I bought one and what I got was an Orange PI with a 16 GB SD Card (all but full) and two USB joypads.

The game software used the Retro-Pie software, but the games were a massively illegal collection of games (for about ten different consoles including CBM-64, SNES, ZX Spectrum) put together by the company that supplied these “consoles”. Amazingly I found four games on there that I had written back in the 80s in this collection.

It was a bit of an odd PI. Instead of powering up through the power slot, it came with a USB to 5V connector. Looking about on the web I found it is this model in a nice acrylic case. Anyway, after three months it stopped working and it was pushed in to a drawer where it remained until today. I tried it today and it came back to life, I suspect the mains-to-USB adaptor is broken but as I’ve a drawer full of them it’s not a problem.

The reason for mentioning this is not the Orange Pi, nor the games but the joypads. I had a look on the libSDL website and I hadn’t realised that there was quite extensive support built in to SDL.  So my next task is to add game controller support for the asteroids game.

 

Developing for Raspberry Pi without one

Developing for Raspberry Pi without one

Hyper-V RaspberryPi running VS CodeYes it is possible, as the screenshot shows. You can download a copy of Raspbian from Raspberry-pi. You can either download the .iso directly or save them a bit of bandwidth and do it via a file Torrent. It then takes about 15 minutes or so to install it. When you first run it, it will do an update.

It looks exactly like Raspbian on a PI but there are one or two little things to bear in mind. It’s 32-bit, the clipboard doesn’t seem to work and SSH by default is disabled. It’s real easy to enable it, just find Raspberry Pi Configuration. Click the Raspberry in a circle menu button at the top, then click Preferences and there it is on the menu that pops up. In the Interfaces tab, tick the Enabled checkbox for SSH.

I use the excellent WinSCP to connect to it. Conveniently the Pi will show you its ip address (or from a terminal run ifconfig and it will show you the ip address of eth0) and just put that in with username pi and your pi’s password which you should have changed when you set up Raspbian.

Finally you have to install Visual Studio Code and as usual, it’s not quite so easy. Most Linux distros these days are 64-bit and the official VS Code is only available for Debian in 64-bit and Raspbian is of course 32-bit. But there are community builds and it’s the same place that I got the ARM version from.

And here’s the result. A slightly sluggish (well it is under Hyper-V). I had to comment out the code that initialises the audio. Line 580 ish.

/*	int success=Mix_OpenAudio(22050, AUDIO_S16LSB, 2, 8192);
	if (success==-1 ) {
		LogError("InitSetup failed to init audio");
	}
	LoadSoundFiles();
*/

And here’s the results.
Asteroids running on Hyper-V Raspbian

Slight change in direction – more C++ and Pi

Slight change in direction – more C++ and Pi

Raspberry Pi
Image by planet_fox from Pixabay

As I said recently, writing about C and games probably isn’t enough to sustain this site, but if I extend it to include C++ and Raspberry Pi and still maintain the overall direction of writing about game development then that I think will do it.

So I’ve added a new page for C++. I’m currently working on the C++ version of Asteroids and making good progress.

I’m striving to write it in modern C++. To that end, the first entry in the C++ page is a link to a very long document: C++ Core Guidelines written by two luminaries of the C++ world Herb Sutter and Bjarne Stroustrup (creator of C++). You should definitely give it a read.

Interesting fact about the Raspberry PI. Did you know that in March 2020, they sold 640,000! That’s pretty amazing! And while that article says that AAA game playing isn’t something you can do on a Pi, we now know that you can run simple 2D arcade quality games on a Raspberry Pi 4.

Raspberry Pi 4 – Perfect for arcade quality games and development

Raspberry Pi 4 – Perfect for arcade quality games and development

lot s of asteroidsI got my Pi 4 a week ago and have been doing experiments on it with my Asteroids game. If I disable the line of code that kills the player ship in the DestroyObject() function and just add a return after case tPlayer: and uncomment the code that adds Asteroids when you press A then I can have lots of asteroids on screen. Also set the MAXASTEROIDS #define to 128.

Then you get the likes of this screenshot which is a 1024 x 768 size window. It’s still running at 40 fps, there are 116 asteroids on screen, around 60-70 explosions and the Pi’s temperature is still a balmy 44.79 C. I’ve never got the Pi’s temperature (and it does have an internal 3.3V fan) to rise above 51 C.

But what’s also impressive is that with Visual Studio Code and Clang on the PI, I can compile the whole 2,200 lines of C on the PI in about a second. This makes the Pi very usable for developing games. And I say this as someone who is used to powerful Windows PCs for compiling code.

This is it running in debug mode; that’s all the numbers and wire frame bounding boxes around every moving object.

Further experimenting and removing the limitations (I used a While loop to limit it to 60 fps back before I had enabled the hardware video flyback). With that while removed and the hardware video flyback disabled, I was getting over 100 fps with 100 asteroids on screen at once.  I pushed it up to 6,000 asteroids and the frame rate dropped to 10 fps. Given the amount of stuff that was colliding and blowing up, that’s still very good.

Here’s the Raspberry Pi temperature code

Here’s the Raspberry Pi temperature code

To add temperature to the frame caption, I first do a check to see that it is running on a Raspberry Pi. This function does that.

void SetPiFlag() {
	struct utsname buffer;
	tempFlag = 0;
    if (uname(&buffer) != 0) return;
    if (strcmp(buffer.nodename,"raspberrypi") !=0) return;
	if (strcmp(buffer.machine,"armv7l") != 0) return;
	piFlag=1;
}

Note you have to add this include

#include <sys/utsname.h>

Now you need to call this function to return the temperature as a float.

float ReadPiTemperature() {
    char buffer[10]; 
	char * end; 
	if (!piFlag) return 0.0f;
	if (SDL_GetTicks() - tempCount < 1000) {	
		return lastTemp;
	}
	tempCount = SDL_GetTicks() ;
	FILE * temp = fopen("/sys/class/thermal/thermal_zone0/temp","rt"); 
	int numread = fread(buffer,10,1,temp); 
	fclose(temp); 
	lastTemp = strtol(buffer,&end,10)/1000.0f;
	return lastTemp;
}

Things to notice.

  1. If it’s not running on a Pi it always returns 0.0C.
  2. No matter how often it’s called, it only reads the temperature once a second.
  3. In between reads it caches the temperature in a variable lastTemp

When I first wrote this, it was reading the temperature on every frame. It actually changes that quickly but that made it hard to read. So once a second is fine.

Asteroids now runs on a Pi 4

Asteroids now runs on a Pi 4

Asteroids with built in temperature for Raspberry PiI was interested in seeing what frame rate I got out of it and how much it warmed the PI.

The change to get the texture loaded was to split the five image files (four x asteroid + player ship) into two rows each.

I added this code into the DrawPlayerShip function.

    if (Player.dir >= 12) {
	  	spriterect.y = SHIPHEIGHT;
		spriterect.x -= SHIPWIDTH*12;
	}

So for directions 0-11, it uses the top row and 12-23 the 2nd row. There’s similar code in the DrawAsteroids function.

I’m getting about 55 fps, twice the frame rate of the 3B+.  Sustained play over five minutes got the temperature up to 51C, but if I start the game and let asteroids drift about for a while it settles somewhere around 48-50C.

I have a fan fitted plugged into the 3.3V that runs all the time but is almost inaudible. There’s also a 5V setting that can be used for extra cooling but you only need that when temperatures get up to the 80C mark. There’s also 3 copper heat sinks stuck on three chips on the motherboard.

It’s very playable at 55 fps. This isn’t full screen BTW but on a 1024 x 768 playing area. There’s just one last change I’ve added. I combined the uname code to detect if it is running on a Pi and in that case display the temperature  on the Window caption. If you look closely at the image above you’ll see it says 43.82 C. I use a counter and check against so it only reads the temperature once a second and caches the result.

Two bugs on Raspi – one fixed

Two bugs on Raspi – one fixed

asteroids screenshotAs always bugs are the fault of the creator and mea culpa (my bad!). I can trace this back to my conversion from the Windows source to the Ubuntu version. This line in LoadMask

int numread = fread_s(mask, sizeofmask, sizeofmask, 1, fmask);

Became this line in the Ubuntu version.

int numread = fread(mask, sizeofmask, sizeofmask, fmask);

But should have been this instead.

int numread = fread(mask, sizeofmask, 1, fmask);

It affected numread and failed just on the masks/am2.msk load.
The other bug only applies on Raspberry Pi 4 and is a limitation of the implementation of SDL2 texture size.
One of the asteroids graphics is 6720 x 280 and the error it returns is Textures can’t be bigger than 4096×4096.

What is odd that it works on a Raspberry Pi 3B+ but not on the 4 which has 4 GB of RAM. Anyway I’m writing a program to transform the graphics from 24 x 1 shapes to 12 x 2. So the 280 x 280 shape file will be 3360 x 560. This will affect the player ship and all asteroids and need a slight change to the DrawPlayerShip() and DrawAsteroids() functions.