How to read a Raspberry PI temperature in C code

How to read a Raspberry PI temperature in C code

Raspberry PiReading the temperature of a Raspberry PI can be done in a couple of ways. This command:

vcgencmd measure_temp

Outputs something like temp=32.7’C. My 3B+ PI has heatsinks and a cooling fan so runs cool. Even with asteroids it only peaked at 50.5C. Well below the throtle back temperature of 85C.

Another way (I suspect they are the same) is to do

cat /sys/class/thermal/thermal_zone0/temp

Which outputs values like 32705. Just divide by 1000 to get the temperature in Celsius.

But how do we do it in code? Well I’ve tested this code below and it seems to work.

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int main(void) {
  char buffer[10];
  char * end;
  FILE * temp = fopen("/sys/class/thermal/thermal_zone0/temp","rt");
  int numread = fread(buffer,10,1,temp);
  fclose(temp);
  printf("Temp = %5.2fC\n",strtol(buffer,&end,10)/1000.0);
}

It reads the device as a string then converts to a long using strtol and divides that by 1000 then prints it. Output is something like:

Temp (C) = 34.88C

Now it just needs combined with the code to detect that it’s running on a Pi and you’re good.

Identifying a Linux system in code

Identifying a Linux system in code

Since I got asteroids running on a Raspberry Pi, I have decided I want to incorporate the temperature in the window caption when you switch it to debug mod by pressing Tab. Currently all that does is display position info on moving objects and bounding boxes.

But if I include that code in, I want to be sure that it only works when running on a Raspberry Pi. So I need some code to identify the system. A bit of digging and I discovered the Linux uname command. That link goes to an online man page for uname.

If I run uname -a on my Ubuntu 18.04LTS I get this.

Linux david-Virtual-Machine 4.15.0-96-generic #97-Ubuntu SMP Wed Apr 1 03:25:46 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

And on my PI.

Linux raspberrypi 4.19.97-v7+ #1294 SMP Thu Jan 30 13:15:58 GMT 2020 armv7l GNU/Linux

In fact the uname -n command gives david-Virtual-Machine on Ubuntu and raspberrypi on the PI. These are the names though names are often changeable and what if someone is running ubuntu on a PI? Yes it is a thing. But the uname -m identifies the CPU.  x86-64 on my Ubuntu and armv71 on the pi.

I did a bit of digging and found a C program on stackoverflow that will do the same as uname.

#include 
#include 
#include 
#include <sys/utsname.h>

int main(void) {

   struct utsname buffer;

   errno = 0;
   if (uname(&buffer) != 0) {
      perror("uname");
      exit(EXIT_FAILURE);
   }

   printf("system name = %s\n", buffer.sysname);
   printf("node name   = %s\n", buffer.nodename);
   printf("release     = %s\n", buffer.release);
   printf("version     = %s\n", buffer.version);
   printf("machine     = %s\n", buffer.machine);

   #ifdef _GNU_SOURCE
      printf("domain name = %s\n", buffer.domainname);
   #endif

   return EXIT_SUCCESS;
}

And this is what it outputs on a PI.


system name = Linux
node name   = raspberrypi
release     = 4.19.97-v7+
version     = #1294 SMP Thu Jan 30 13:15:58 GMT 2020
machine     = armv7l

So that bit is easy to do. Next is getting the temperature, but that’s for another blog entry…

New version of Visual Studio Code

New version of Visual Studio Code

Update to Visual Studio codeNormally I wouldn’t mention it, as there’s nothing really outstanding about the update (accessibility improvements, Timeline view, Better quick open for files etc.) You can read all about it here.

But the online documentation has improved and more importantly there’s a new tutorial using C++ on Linux. I could have done with this a few weeks back; everything there I had to figure out for myself! In our case I’ve used clang not gcc and C not C++ but those are very minor differences.

But it does have better explanations for many of the fields in the various .json files. Also the changes to tasks.json to allow multiple files. I just added them in based on gcc.

 

Building Asteroids on a Raspberry-Pi

Building Asteroids on a Raspberry-Pi

My asteroids running on a Raspberry-Pi 3B+Silly me completely forgot that Raspberry Pis have a different CPU architecture compared to my PC. You just get used to something working on Linux and it was only after copying and it didn’t run that I realised my mistake. So I now have to recompile Asteroids (from Chapter-48.zip).

I’ve had a Raspberry-Pi 3 B+ for a year and wanted to run the Linux asteroids on it, now that I have it working on Ubuntu.

To do that I had to setup  Pi, running Raspbian downloaded from the Raspberry Pi website onto my Windows PC and then I used Win32 DiskImager (free Windows software) to burn the Raspbian OS onto a SD Card. Raspberry-Pis boot from SD Cards and the better and faster the SD card, the quicker the OS runs. Get a class 10 with A1 SD Card if you can.

After the Pi booted and Raspbian was installed and configured I had to enable SSH on the Pi; it’s disabled by default.

So now my PC was talking to the Pi using the excellent free WinSCP.  I copied all the files over, including the masks, sounds and images folders and all the source code and my exe built on Ubuntu which was the wrong file type. (Trying to run Intel code on an ARM- good luck with that!)

Now it turns out that the Raspbian version I installed (I’d gone for the Raspbian Buster with desktop and recommended software-2.5 GB download) included the non-dev version of SDL2. But as I needed to recompile the whole program, I had to install Visual Studio Code, Clang and the dev versions of SDL2, image and mixer.

Visual Studio Code on Arm?

Haedmelted VS Code running on Raspberry Pi 3b+Microsoft don’t do an official version for Raspberry-Pi or other ARM boards. However I discovered that a developer called Jay Rodgers has taken their source code (VS Code is open source) and  you can get a version from his site. It’s very usable on the Pi. Almost but not quite identical.

After installing the dev versions of libsdl2, image and mixer, I was almost able to compile it. I’d installed Clang but unlike the version on Ubuntu which was Clang-6, the version on Raspbian (based on Debian Buster) is Clang-7.

This means you have to edit the tasks.json file in the hidden .vscode folder. On line 25 where it says “command”: change the path to “/usr/bin/clang-7”.

That built ok, but when I ran it, it stopped with an error in the errorlog.txt file. For some reason, it failed loading the file “masks/am2.msk”.  Now I’d had no errors copying files from Windows to the Pi. This had been a very intermittent problem when copying several files in one go from Windows 10 to Ubuntu running under Hyper-V. Occasionally it would come up with some weird error but usually copied OK on the second go.

Despite several attempts, I could not get it to load that mask so as a temp fix I commented the line out. This means that one of the four asteroids sizes can never collide with anything. But it now ran.

However, I’m only getting about 25-27 fps with it in the early levels, not the 60 fps that I get on Ubuntu on other hardware. Given that the bulk of the work is blitting graphics, I suspect the GPU is just a bit under powered. Apparently on the Raspberry Pi 4, the GPU is newer and twice as fast as on the 3B+ so I’ll have to try it on a 4 when I get one. But the game works and is just about playable.

Now I’ll have to try and figure out why it won’t load that one mask. This has the makings of an interesting bug…

 

Install Clang and Visual Studio Code on Ubuntu

Install Clang and Visual Studio Code on Ubuntu

Visual Studio CodeClang is very easy to install. Open a terminal and issue this command:

sudo apt install clang

It takes a little bit more for Visual Studio Code (aka VS Code). Start on the download page of VS Code. Just google for download Visual Studio Code or click here. Click the Deb rectangle and pick the Open with Software install (default) and that will install it.

After that is installed, run VS Code (open a terminal and type code) and click the extensions icon (5th down on the left) and type in C++ in the search box. The first result is by Microsoft so select it and click install.

Now I created a Projects folder under my home, so click on open Folder in the File menu and select Projects. You can create individual folders for applications under that. If you look closely you can see Asteroids, AsteroidsDND and Examples with Asteroids being open and containing two .c files.

To compile, make sure the open tab is the file you wish to compile then click Terminal on the top menu and Run Build Task…  You’ll see the compiled programs on the left under the Asteroids folder (decnot and ex1).

There’s a bit more for configuring Builds and Debugging but I’ll cover that another time.

 

 

Why C and not C++?

Why C and not C++?

C++ LogoConfession, I can program in C++ though I’m a bit rusty and my C++ knowledge stops short of creating template classes but I can use them.  But given that probably most games these days are programmed in C++ why is this site about C?

Well it’s been mostly personal preference. I like C; it is a heck of a lot simpler than C++ (understatement!) and compilations are very fast! I know most of C though to my chagrin I admit I got include guards wrong for quite a while. I was using them in .c files rather than .h.

Plus, I haven’t really looked into WebAssembly enough. I know it will work with both C and C++. But it has to be said that there is probably only so much you can write about C, so I will be including C++ in the future. I’d planned to do a C++ conversion of my Ebook(s).

Besides which if you google for something involving C, you’d be amazed how many C++ and even C# websites are in the results!

 

 

How I write my ebooks

How I write my ebooks

Screenshot of JutohMy secret: is I use a piece of software called Jutoh from a Scottish software house. It costs £35 and if you are writing a book or e-book I highly recommend it.

Note I have no connection with them except as a very satisfied customer.  

It’s all the little things, inserting pictures, tables, handling source code that make it particularly good for non-fiction books.

It also includes formatting for various programming languages and is cross-platform with versions for Linux, Mac and Windows.

The current version of the Linux e-book has 55144 words, (307407 characters). It takes about 10 seconds to compile that into a .mobi (Amazon) or .epub file with all sorts of warnings.

Originally I started writing the first e-book in a text editor then imported it to MS-Word. But Word is not exactly great software for publishing non-fiction as it’s not desktop publishing. Actually you don’t need desktop publishing per se because Kindle reflows the text, but having pictures, source code and tables makes it more complicated.

I looked about, found Jutoh and consider myself very fortunate; it makes life much easier. I’ve also had excellent support from Jutoh’s creator Dr. Julian Smart who is also well known as a creator of wxWidgets.

Writing an e-book is a multi-pass project that takes me roughly 3-4 months part-time.

  • Pass 1. Develop the software and e-book structure i.e. chapters.
  • Pass 2. Write the chapters.
  • Pass 3. Look for typos. For now this is just me, but eventually I hope to be able to afford a technical editor.
  • Pass 4. Tidy up the layout. This is probably the slowest phase. Because of the images, tables and shortish code listings, it might look neat and tidy at one size, but if you change the viewing font size, it looks er not quite so neat! I can’t see any way round that.

A friend drew the original book cover but I’ve decided to pay for the cover for the Linux version.

A little trick in Visual Studio Code

A little trick in Visual Studio Code

Matching braces in Visual Stduio CodeIf you have a bit of nesting and you want to make sure your braces match up, Visual Studio code (VSC) can help you with that.

Just drag the cursor over the left or right brace and it will highlight the corresponding brace. In the screenshot you can see the brace on the 2nd line and 5th lines are highlighted.

Here’s a second trick. You can quite easily collapse code blocks. On the left below is uncollapsed code. Now move the cursor between 3 and typedef and a down arrow should appear. Click it and it will collapse the block. It now shows a right arrow and you click it to expand it back.

Before Collapsing a block in C
Collapsed code

Made it to the end of the Linux EBook

Made it to the end of the Linux EBook

Tux with books
Image by OpenClipart-Vectors from Pixabay

That is, I have completed every one of the 13 stages in the game’s development and each is up and running on Ubuntu. It turned out easier than I thought though I still need to fix one or two things.

Because Clang doesn’t support the MSVC fopen_s, I did a quick fix and removed the error checking and when it first ran, it did another segmentation fault because the high score table file didn’t exist. D’oh!

That’s fixed now. Crashing because a file is missing is a very bad practice! Also for some reason last night, Windows and Ubuntu fell out and copy/paste between them wasn’t working. It is working today. I’ve noticed this can be a little bit sensitive at times and I think it was the Windows side that fell over.

However I’ve also been playing with Ubuntu 19.10 and Copy/Paste appears to be disabled on that. That kind of thing I find extremely irritating. I have Guest services ticked, so why does nothing I do enable it? 18.04 LTS apart from last night’s glitch is perfectly happy. And 20.04 Focal Fossa (I know!) is due out on April 23rd.  It’s not quite ready but you can try it an early version if you like. I will be upgrading.