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?
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.
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:
#!/usr/bin/env bash # A wrapper for displaying raspipe.py output on /dev/fb1 export SDL_FBDEV=/dev/fb1 cat /dev/stdin | ./raspipe.py $@
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 $@, 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:
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:
nano machine_stars_pitft.sh
And then add the following:
#!/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 $@
Finally, use chmod to set the executable bit on your new script:
chmod +x ./machine_stars_pitft.sh
Now you should be able to test like so:
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!
Page last edited March 08, 2024
Text editor powered by tinymce.