Monday 13 November 2017

Touchscreen Raspberry Pi



Description
Raspberry Pi is a great machine that people want to make amazing stuff with it, and one great think is to use the a visual interface that you can get from the hdmi port.
In my case I wanted to have a screen and touch sensor to interact with a custom GUI.

Item

  • Innolux 7 with touch screen (link)
  • Raspberry Pi ( in my case version 2 )
  • Battery pack
  • USB to DC Jack


Installation

1) Driver
The good news is that Raspberry Pi has finally introduced a simple  system to enable touch screen with most common board on the market ( here a list ) but in my case I had to enable Egalax sensor.

To make sure your device is supported, just connect the usb and run ( make sure its the only device usb connected)
lsusb
and you should see the model and manufacture of the sensor.

In my case everything is supported, if your sensor is not supported, try to poke them on github otherwise you are forced to recompile the kernel with the driver built-in.

The module kernel ( if supported ) has to be enabled by running the command and the sensor connected in the usb slot:
 rpi-update
when the process will be completed, you should be able to access the device.

2) Testing
Make sure you have installed all the tools to test your sensor by running this command:
 sudo apt-get install evtest tslib libts-bin

When you have confirmed that, you have to find the right path to your device by looking into the folder /dev/input/.
The common path are  /dev/input/event0 or /dev/input/touchscreen, but make sure you dont have a keyboard or mouse connected otherwise it might be event1 , event2, etc.. 

After you have discovred the correct path, in my case /dev/input/event0, try to run the command:
sudo evtest /dev/input/event0
and you should see some values after you pressed the  touch screen.
If nothing happened, that means you sensor is not connected properly or the driver module is not loaded.

3) Calibrate
Now that you made sure that the device is plugged, module loaded and something is coming out from the device, you can run the first calibration command:
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event0 ts_calibrate
after this command has been launch, you should see a calibration screen where you are asked to press 5 different spots.

Once the calibration is completed, you should have a file at the location /etc/pointcal.

Double check that you can draw correctly with this command:
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event0 ts_test
then you are ready to go!

4) Issues
You might have some problems by using the device Egalax ( as I had ), which is the missing module in the latest build of  libts-bin ( probably if this guide is a few weeks older, this problem would have been solved ).

I couldn't run the calibration test because the software would close by saying "sensor not supported", this is because the build dint have the Egalax driver embedded in the build.
What you have to do, is to make sure libts-bin is removed from your system and purged all references, usually by using:
apt-get remove --purge libts-bin

Make sure you got all the software to compile the source code of libts:
sudo apt-get install git automake libtool

Checkout the repository ( apparently the patch for the driver has been included on the main repo ):
git clone git://github.com/kergoth/tslib.git
cd libts
and compile
./autogen.sh
./configure
make
sudo make install

then follow the guide here that is just moving the library in the right folder: https://myraspberryandme.wordpress.com/2015/02/12/making-an-egalaxpollin-touchscreen-work-with-tslib/ (pointless to re-write what this guy did best )

Once you finished, you should be able to run ts_calibrate without any errors.

Also, you might want to change some parameters into /etc/ts.conf.

Pygame without X server

You small game, application, script or else dont always need to have a X server running just to access to the graphical unit.
The trick is to use pyscope, offered by Adafruit ( link ), that is a simple class which makes sure your software will inject the output of pygame into the frame buffer.

In my case I had to add few environment variables to make sure that the touch screen would work with pygame:

#Reference to the calibration file
os.putenv('TSLIB_CALIBFILE', '/etc/pointercal')
#Point to the right library folder
os.putenv('TSLIB_PLUGINDIR', '/usr/lib/arm-linux-gnueabihf/ts')
#Setup the touch screen
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/event0')
otherwise the pointer would be jumpy or not following correctly as the ts_test was showing.