If you have a PiTFT display, you can use it to display the output from Pygame, and avoid the overhead of running the desktop system at the same time. Since my goal is to use a PiTFT as an auxiliary display to my main computer, I've been targetting a 320x240 display.

For this guide, I'm using the 320x240 2.8" TFT+Touchscreen for Raspberry Pi. We have detailed installation instructions here. Make sure you configure the console to appear on the PiTFT.

Under the hood, Pygame uses SDL for graphics and audio. SDL needs to be told what device it should use as a framebuffer display. Remember these commands from earlier?

Download: file
export SDL_FBDEV=/dev/fb1
shuf /usr/share/dict/words | head -50 | ./machine_stars.py

export just tells the shell that a variable should be available to processes you start within the shell. SDL_FBDEV=/dev/fb1 tells the SDL library what device file maps to the display we want it to use.

raspipe_pitft.sh & machine_stars_pitft.sh

You could remember to export this variable every time you open up a new shell, but there's a good chance you'll forget. A better approach might be to write a wrapper script like this one:

Download: file
#!/usr/bin/env bash

# A wrapper for displaying raspipe.py output on /dev/fb1

export SDL_FBDEV=/dev/fb1
cat /dev/stdin | ./raspipe.py [email protected]

Since /dev/stdin is just a file from the perspective of the script, we can pass it off to a command really easily. We also tack on [email protected], which is all the arguments to the script. This way we can say things like ./raspipe_pitft.sh -r '.*foo.*' and raspipe.py will see the right options.

You can see this in the Adafruit-RasPipe repository, as raspipe_pitft.sh, and it can be invoked like so:

Download: file
shuf /usr/share/dict/words | head -50 | ./raspipe_pitft.sh

If you write your own wrapper script, you'll want to tell Linux that it's executable. Let's say you decide to write the equivalent for machine_stars.py. First, open a new file in Nano:

Download: file
nano machine_stars_pitft.sh

And then add the following:

Download: file
#!/usr/bin/env bash

# A wrapper for displaying machine_stars.py output on /dev/fb1

export SDL_FBDEV=/dev/fb1
cat /dev/stdin | ./machine_stars.py [email protected]

Finally, use chmod to set the executable bit on your new script:

Download: file
chmod +x ./machine_stars_pitft.sh

Now you should be able to test like so:

Temporarily unable to load embedded content:

What Now?

By now, hopefully you have a better idea of how to use Pygame for simple visualizations.

Next, we'll explore building a simple network wrapper for RasPipe so you can send it data to visualize from anywhere on the network. Stay tuned for Part 2!

This guide was first published on Mar 20, 2015. It was last updated on Mar 20, 2015.

This page (Pygame on the PiTFT and Next Steps) was last updated on Jan 19, 2021.