Author: David

My raspberry Pi touchscreen arrived but it wasn’t smooth going

My raspberry Pi touchscreen arrived but it wasn’t smooth going

I bought the Longruner (no, not a typo!) 7″ touch screen and it turned up after a few days. It’s nicely made and similar to this, though it looks slightly different. That hand must be a dolls hand!

It doesn’t have any documentation but there was a DVD with three MS Word documents which I opened on my Windows PC (It comes in useful sometimes!). One of them the “7inch HDMI Display user Manual(En).docx” has a photo of the three ports.

There’s a full size HDMI socket and a USB to Android (Micro USB-B I believe) cable is included that plugs into a USB port and either of the Android type ports. You can either have power and touch screen with the port nearest the HDMI as I did or just power alone, if you want to use it as a monitor. The two green squares show the USB and touch/power cables.

When I switched it on, it just sat there saying no HDMI signal. That MS Word document included instructions to edit the config.txt in \boot. Of course I had no display but the Pi was plugged into the network and I ran WinSCP which logged in and let me view files. I don’t think you can edit files as root with WinSCP, and that config.txt file needs root access to edit it.

However I am a long time user of Putty. (You can also get putty from putty.org but I don’t know if that is also an official one). After that I could ssh into the pi, do a sudo -s command to switch in root and then nano /boot/config.txt to edit config.txt.

longruner touchscreen being touchedHere’s a photo of my fat finger moving the cursor round the screen. It works surprisingly well, and the display is lovely though my photos probably do not do it justice.

These are the lines you paste into the end of the config.txt, save it out and reboot.

max_usb_curren=1
hdmi_force_hotplug=1
config_hdmi_boost=7
hdmi_group=2
hdmi_mode=87
hdmi_drive=1
display_rotate=0
hdmi_cvt 1024 600 60 6 0 0 0

Now I just have to figure out how to use it in my games. The display is 1024 x 600 btw.

Plugging in a second monitor worked fine as this photo shows. The blacks are darker on the touchscreen but that’s just because the gammas don’t match and I was too lazy to change them!

Touchscreen and 2nd monitor on Raspberry Pi

Playing with 64-bit Raspberry Pi OS

Playing with 64-bit Raspberry Pi OS

Binary digits
From Pixabay.com

So after deciding I was going to wait until the 64-bit Pi OS was tested and ready for use, I changed my mind and downloaded the beta and installed it.

It ran ok, though as with any Beta there are bound to be little glitches. There’s a survey where they are interested in what the PI is being used for and it showed UNDEFINED for my Pi’s serial number, though

cat /proc/cpuinfo

worked fine and displayed it.

The display utility let me change my display to 75 HZ at 1920 x 1080 and then promptly crashed. After reboot though it was at 75 HZ.

My main reason for trying it out though, I was curious about development tools, particularly IDES. I had forgotten that code.headmelted.com, the alternative source for Visual Studio Code (my favourite) supported arm64 which is what the 64-bit runs.  I was able to install VS Code from there without any problems.

64-bit vs 32-bit

32-bit has the memory limitation of 3 GB (in both Windows and Linux)  whereas 64-bit offers any amount of RAM up to what’s available. It’s not that big  thing with a 4 GB PI but with 8 GB it can make a big difference.  This article (Sorry that the link is to an article on Medium.com which offers a few free articles then paid) suggests that 64-bit Debian (which Raspberry Pi OS is) is quite a bit faster than 32-bit.  My impression is that it feels a bit snappier but that could be wishful thinking n my part!

I’ll put Asteroids on and see what fps I can get though that may not be a great way to compare them as the GPU plays a big part and I can’t see it making any difference, but I could be completely wrong on that. More as I investigate further.

C Amalgamation – for SQLite

C Amalgamation – for SQLite

SQLite Logo
Image by CopyrightFreePictures from Pixabay

If I were to use a database in a game, it’s likely that it would be SQLite. It’s an open source SQL database that is 102 different C files and 32 others. Of those 102, 75% are C and the remainder header files.

Unlike many other SQL databases (for example MySQL, SQL Server, PostgreSQL) it doesn’t need to run as a separate server. It compiles directly into your application, adding a whopping 400 KB! (I’ve seen bigger picture files…)  It’s fast and very well tested. For every line of code there are 644 lines of test code! Details of testing here.

You wouldn’t use SQLite in an arcade game as it would be a bit of an overkill just to hold a high-score table, but in something like a Rogue or Mud game, I could easily see it being used to store rooms, corridors, treasures etc. SQLite is also very easy to use.

The word amalgamation means that all the 102 source files have been combined (amalgamated) into one file called SQLite3.c. So building it is a single command.

SQLite is probably the worlds most popular database. It’s found in most operating systems and is used in every smartphone.

Raspbian vs Raspberry Pi OS

Raspbian vs Raspberry Pi OS

Raspian.orgI’ve always referred to the version of Debian running on a Raspberry Pi as Raspbian and so it was until the recent 8 GB RAM Raspberry Pi was launched.  But it turns out that Raspbian was an independent version of Debian created by people at raspbian.org.  As they say “Note: Raspbian is not affiliated with the Raspberry Pi Foundation. Raspbian was created by a small, dedicated team of developers that are fans of the Raspberry Pi hardware, the educational goals of the Raspberry Pi Foundation and, of course, the Debian Project.

From now on, it seems the correct name is Raspberry Pi OS and no longer Raspbian. The change was announced at the bottom of this Raspberry Pi Foundation blog post. This has something to do with the fact that Raspbian is mainly 32-bit while the Raspberry Pi OS is both 32-bit and 64-bit though the latter is still at beta. You can read more about the recent changes to Raspberry Pi OS on this blog entry.

A very small sort in C

A very small sort in C

Tiniest sort in CI came across this article on efforts to make the smallest sort function in C.  Being cynical I did wonder if it would compile and run. I mean 56 bytes is kind of short!

To get it to compile in VS 2019 I had to add a couple of changes, mainly ints and the void. This is it wrapped in a program. The screenshot shows it running and the output at the bottom. Very clever!

The sort function is that void s(… at the top.

 

#include <stdio.h>

void s(int *a, int n) { n-- > 1 ? s(a, n), s(a + 1, n), 
     n = *a, * a = a[1], a[n > * a] = n : 0; }

int arr[10] = { 9,1,2,6,8,11,45,-1,6,4 };
int main() {
	s(arr, 10);
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}

What I didn’t show you is the number of times Windows defender went ape and declared it a trojan, every time it compiled! My PC is kept pretty bug free and this is a false positive…

A trojan

Looking at Intrinsics in C

Looking at Intrinsics in C

AVX Registers
From WikiMedia. https://commons.wikimedia.org/wiki/File:AVX_registers.svg

This post probably has the most mystifying title yet. Here’s a little background. Modern CPUs have some hardware that can speed up operations like addition or multiplication by doing them in parallel. The words SIMD (short for Single Instruction Multiple Data) and vectorization apply.

These are very processor specific. Even something like x86-64 CPUS have a whole raft of alphabet soup. SSE, MMX, AVX. Intel even have a website so you can see what instructions are supported with examples in C.

My i5930K CPU supports AVX so if you tick the AVX box on the left hand side of the Intel intrinsics guide, you  can see can there are something like 12,000+ instructions and variants listed. If you tick one of the categories, it filters out the instructions to those applicable to that category.

Click one of the instructions on the right and you’ll see a rough equivalent in C to what it does. Also the header that it is found in, typically

#include <immintrin.h>

which  Visual C++ is happy to compile. An intrinsic is a special C instruction that gives you access to these low level vectorization instructions without you having to drop into assembler.

On a Raspberry Pi, the ARM processor supports a similar type of scheme but it’s called NEON.

If you are interested in finding out more, take a look at Microsoft’s intrinsics documentation which covers both Intel and ARM.

 

Working with SDL_ttf

Working with SDL_ttf

Font set
Image by Gordon Johnson from Pixabay

I’ve decided that I should use SDL_ttf in my games. I had previously incorrectly thought that using it would lead to a performance hit and wrote my own printch and TextAt functions which with a fixed-with (monospaced) font saved as a bitmap worked ok.

However after reading up on this, I see that the main routine for outputting text returns a SDL_Surface. This is an in-ram structure.

SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg)

The significance of this is that you pre-render all text strings as much as possible then convert them to SDL_Textures which moves the structures into VRAM. That means those strings can be blitted as fast as my string method. It’s less flexible says when printing numbers, so it might make sense to output a monospaced font of digits in the desired colour and font size  (I call it a digitset) and prepare all the digit sets that you need. I’ll create a test program…

PS. This is my 100th blog entry! Here’s to the next 100….

Developing a game for the SNES

Developing a game for the SNES

Yoyo Shjuriken home made SNES gameThis isn’t about me for a change, but I thought it well worth a mention. For those who have never played with a SNES, it’s a Nintendo console from the early 90s. All the games were cartridge based and I loved games like Super Mario World, Legend of Zelda (I finished that) and Secret of Mana.

There are emulators around now’ I have one that runs on an Orange Pi (Chinese brand of Raspberry -Pi compatibles) and if you can get the SNES game roms (mostly illegal BTW!), you can play those games on it.

The reason I mention it, unlike older SNES games which were programmed in 65816 assembler, the developer (Dr Ludos) programmed it in C. Apparently there is only one C compiler that you can use called tcc816 and it needs PVSNESLIB. I’m not sure which of those two links has the better tcc816 though I suspect it’s PVSNESLIB.

This bloke also made his own cartridges which is pretty amazing. I used to know somebody who worked in a firm where they developed SNES games and apparently Nintendo’s quality control was such that the completed game had to include video of 27 hours of play to show that the game wouldn’t crash.  Given the cost of producing the thousands of cartridges, it’s understandable.

Cartridges stopped people copying them and had a higher profit margin, but its kind of ironic now that a 1,000 SNES games can easily fit on a minuscule SD card.

 

 

 

Expanding my virtual hard disk

Expanding my virtual hard disk

filelight utility running on UbuntuMost Linux development is done on Ubuntu running under Hyper-V on my Windows 10 PC. If you have lots of RAM (and I have a full 64 GB), it’s very convenient. I run Snagit on Windows and this makes it very easy to grab screenshots of the Ubuntu window.

I also have a “Raspberry-pi” running under Hyper-V.  There’s a Raspbian desktop that you can download and run in Hyper-V, VirtualBox or VMWare though I’ve only done Hyper-V. Don’t forget when you are running a Raspberry Pi this way that its x86 based not ARM. That does affect the available software, so it doesn’t behave exactly like a real Pi though often close enough.

Today though I started getting low disk space from my virtual Ubuntu. That’s the problem with virtual machines. When you first setup a Virtual hard disk, you never know just how much disk space you will need.

There’s a terminal command that shows how much space you have left.

df -h --total

This produced this

david@david-Virtual-Machine:~$ df -h --total
Filesystem      Size  Used Avail Use% Mounted on
udev            942M     0  942M   0% /dev
tmpfs           193M  1.4M  192M   1% /run
/dev/sda1        11G  9.9G  603M  95% /
tmpfs           964M     0  964M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           964M     0  964M   0% /sys/fs/cgroup
/dev/sda15      105M  3.6M  101M   4% /boot/efi
tmpfs           193M   16K  193M   1% /run/user/121
tmpfs           193M   24K  193M   1% /run/user/1000
total            14G  9.9G  4.1G  71% -

This was after I’d extended my virtual hard disk.  You can see I now have 4.1 GB free.

The pretty picture is from a utility filelight. You install it in the usual way

sudo apt install filelight

Or if you prefer a more visual insight, install qdirstat.

sudo apt install qdirstat

This is like WinDirStat on Windows but qdirstat seems to run many times faster. It took a couple of seconds to produce this image below. WinDirStat would take 10-30 minutes.

qdirstat

 

 

 

 

 

 

 

So how did I expand my Hyper-V hard drive?

First you have to get rid of any checkpoints. Save your Hyper-V session if open then delete the checkpoint.

Delete Hyper-V checkpointRight click on the checkpoint for the selected VM and click delete. This will take a minute or two and you’ll see it have a Merging status. You may need to shutdown the VM.

After that you can go into the settings and it will let you edit the virtual hard drive and change the size.