Skip to content

Install and configure TSLIB

Richard Goodwin edited this page Nov 26, 2017 · 8 revisions

Background

tslib is a touchscreen library that handles input from Touchscreen devices. Touchscreen devices vary enormously in the types and variety of data they generate, and tslib attempts to take this raw stream, filter and scale it such that an application can use it without needing to know too much about the specific touchscreen itself.

Linking tslib directly with the Europi application doesn't really help us, as although that would enable Europi to read from and react to the Touchscreen device, we wouldn't benefit from RayLib's gesture detection, collision detection etc.,

The solution is to use ts_uinput this uses tslib to create a raw input device stream, which can be read by RayLib. The advantage being that this additional device stream has already passed through tslib's modules, so is scaled, and dejittered before RayLib reads it.

Prerequisites

To build tslib on a Raspberry Pi, you will need automake and libtool. If you are following these steps in order, then these will already have been installed in the initial Raspberry Pi configuration step.

sudo apt-get install automake libtool

I also found that cloning and building tslib didn't create the default configuration file /etc/ts.conf so it is easier to simply install it:

sudo apt-get install ts-lib

Clone the tslib repository

Navigate to the root directory beneath which you want to install the tslib source

git clone git://github.com/kergoth/tslib.git
cd tslib
./autogen.sh
./configure
make
sudo make install

finally, to make sure the library is available to other applications:

sudo cp -P /usr/local/lib/libts* /lib/arm-linux-gnueabihf/

Configuring tslib

tslib works by reading the raw event stream from the touchscreen device, and passing this through a series of modules, each of which aims to address a specific issue - scaling or dejittering for example.

Modules are loaded and configured using the the file ts.conf found in

/etc/ts.conf

This isn't created by default, so create it:

sudo nano /etc/ts.conf

and paste in the following lines, which set the default configuration that I have found works reasonably well with the touch screens used by Europi:

module_raw input
module variance delta=100
module dejitter delta=500
module linear

Also inspect the contents of the following directory, and make sure that there are .la and .so files that match the filter names above:

ls /usr/local/lib/ts

Calibrating the touchscreen

tslib calibration data is held in the following file:

/etc/pointercal

This file doesn't need to be edited manually. Instead, calibration data is captured and stored using a separate program called ts_calibrate which guides the user through tapping on 5 separate positions on the touchscreen. However I did find that ts_calibrate would fail with an fopen error if it couldn't find that file at all, so I simply created an empty file:

sudo touch /etc/pointercal

The ts_calibrate procedure can be run as often as you like, and it is particularly important to do so if the on-screen cursor doesn't seem to align accurately with the point you touched. Note: if you are running ts_uinput as a daemon you will need to stop and re-start it following a re-calibration, as the calibration data is only read once when ts_uinput loads.

I found I needed to explicitly specify the location for the various parameters when running both ts_calibrate and ts_uinput

The important aspects of the following command string are that our Touchscreen appears to the Pi as /dev/fb1 and its raw event stream is available as /dev/input/event3 Note: these may vary if you are compiling and using the code in an environment other than with the usual Europi hardware (Eg., with a variety of USB devices attached to the Pi).

sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event3 TSLIB_CALIBFILE=/etc/pointercal TSLIB_CONFFILE=/etc/ts.conf TSLIB_PLUGINDIR=/usr/local/lib/ts ts_calibrate

The Calibration procedure is very straightforward, and just involves tapping on the screen as each of the 5 crosses is displayed.

Once calibrated, the touchscreen can be tested using:

sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event3 TSLIB_CALIBFILE=/etc/pointercal TSLIB_CONFFILE=/etc/ts.conf TSLIB_PLUGINDIR=/usr/local/lib/ts ts_test

Then,to create the input event stream that is going to be read by RayLib, run the following:

sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event3 TSLIB_CALIBFILE=/etc/pointercal TSLIB_CONFFILE=/etc/ts.conf TSLIB_PLUGINDIR=/usr/local/lib/ts ts_uinput -v -n europi -d

The arguments at the end are as follows:

-v Verbose (lists more information as ts_uinput loads) -n Name (we're calling it 'europi') -d Run as a daemon

If all is well, ts_uinput should report that it has created a new virtual input device input4, which should be listed as event4 and mouse2 if you enter the following:

ls /dev/input

Finally, to launch ts_uinput as a daemon at boot time edit /etc/rc.local and add the following line just above the section that prints the IP address:

TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/event3 TSLIB_CALIBFILE=/etc/pointercal TSLIB_CONFFILE=/etc/ts.conf TSLIB_PLUGINDIR=/usr/local/lib/ts ts_uinput -v -n europi -d

Next:

You can’t perform that action at this time.