Using Rasbian Stretch and pygame with the AdaFruit 2.8″ Touchscreen

I am currently putting together a lawn sprinkler control system based around a Raspberry PI 3 Model B+ and the OpenSprinkler system. I will post a much more complete post when it is working, but here are a couple of useful tips that I’ve found.

Although it will be programmed using a web interface over Wifi, I need the local ability to manually switch stations on or off for testing. This is for my own convenience, but I also don’t want to go through the hassle of telling a contractor how to log in to the local wifi and to go to an obscure URL to test a valve or station!

I’ve chosen to use the Adafruit 2.8″ TFT Capacitive Touch LCD screen. This is a neat little screen that works as a ‘hat’ that plugs straight on to the Raspberry PI. Unfortunately the drivers are for the older ‘wheezy’ and ‘jessie’ versions of Rasbian and not the latest ‘Stretch’. The command line and X configuration options appear to work okay, but the framebuffer touch option does not. A lot of people had problems with ‘wheezy’ and they had to move back to ‘jessie’. I tried a ‘jessie’ image, but this will not boot on the Raspi 3B+!

I needed a simple graphical user interface, and a typical X desktop is far too small for a 2.8″ touchscreen, so I wrote a full screen menu system with pygame. This displays well in framebuffer mode, but the touch does not work. This took some figuring out, but the solution is to configure the display to use X, and then tell pygame to run in “full screen mode” with the mouse displayed. X will then interpret the touch events as mouse events which pygame can then interpret correctly.

Here is the pygame setup code:

The only slight oddity is that the touchscreen is technically showing a HDMI copy, and this will have the minimum coordinates of 640x480px – double the touchscreen’s actual resolution.

Another issue which a lot of people appear to have had trouble with, is how to get the script to run at startup with X. This is solved by editing ~/.config/lxsession/LXDE-pi/autostart. Add the following line:

where /home/pi/sprinkle/ is the script that you would like to run. The trick here appears to be the use of both sudo and the full path to the python executable.





Leave a Reply