Instructables

Step 65: PC Software: Cube updater thread

Picture of PC Software: Cube updater thread

In cube.c we have a function called cube_push(). This takes the 64 byte array and sends it down the serial line to the LED cube.

It also handles the formatting, sending every 0xff byte twice because 0xff is our escape character. 0xff and 0x00 is sent first to reset the LED cubes internal x and y counters.

In main.c we have the function cube_updater(). This function is launched as a separate thread using pthread_create(). The main thread and the cube updater thread shares the memory area rs232_cube[8][8]. The cube updater thread is just a while true loop that calls cube_push() over and over.


The first attempt at an updater thread turned out to create some flickering in the animations. After some debugging, we found out that frames were being transmitted before they were fully drawn by the effect functions. We generally do a fill(0x00), then some code to draw new pixels. If a frame is transmitted right after a fill(0x00), the cube will flash an empty frame for 1/60th ish of a second.

This wasn't a problem in the code running on the LED cube, since it has a refresh rate of over 1000 FPS, but at 60 FPS you can notice it.

To overcome this we create a double buffer and sync the two buffers at a point in time where the effect function has finished drawing the frame. Luckily all the effect functions use the delay_ms() function to pause between finished frames. We just put a memcpy() inside there to copy the cube buffer to the rs232 buffer. This works beautifully. No more flickering!

The cube runs serial at 57600 pretty well, so maybe you can up the frame rate.
I'm not much of a C programmer, so I am actually having some fun writing code in QBasic. Of course QBasic doesn't let me go beyond 19200 baud, so I have to write, and then change the COM setting, save, and compile with FreeBasic. The compiled routines are really fast and I find myself often having to put in huge time loops. It seems to work fine with or without serial buffers declared, but I set them at 2K (2048) anyway.
paler312 years ago
hi im not much of a programmer but am trying to understand the code. My understanding is that the cube_push just pachages the array into a buffer to send down the serial line. My question is if the array is only 64 bytes why does the buffer need to be 200 bytes big surely 64 bytes is all that is needed?