Setting up the Touchscreen
Now that the screen is working nicely, we'll take care of the touchscreen. There's just a bit of calibration to do, but it isn't hard at all.
Before we start, we'll make a udev rule for the touchscreen. That's because the eventX name of the device will change a lot and its annoying to figure out what its called depending on whether you have a keyboard or other mouse installed.
Run
sudo nano /etc/udev/rules.d/95-stmpe.rules
to create a new udev file and copy & paste the following line in:
SUBSYSTEM=="input", ATTRS{name}=="stmpe-ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"
Remove and re-install the touchscreen with
sudo rmmod stmpe_ts; sudo modprobe stmpe_ts
Then type ls -l /dev/input/touchscreen
It should point to eventX where X is some number, that number will be different on different setups since other keyboards/mice/USB devices will take up an event slot
There are some tools we can use to calibrate & debug the touchscreen. Install the "event test" and "touchscreen library" packages with
sudo apt-get install evtest tslib libts-bin
Running evtest
Now you can use some tools such as
sudo evtest /dev/input/touchscreen
which will let you see touchscreen events in real time, press on the touchscreen to see the reports.
AutoMagic Calibration Script
If you rotate the display you need to recalibrate the touchscreen to work with the new screen orientation. You can manually run the calibration processes in the next section, or you can re-run the installer script and select a new rotation:
Manual Calibration
If the "automagic" calibration technique isn't working for you, or you have some other setup where you need to carefully calibrate you can do it 'manually'
You will want to calibrate the screen once but shouldn't have to do it more than that. We'll begin by calibrating on the command line by running
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_calibrate
follow the directions on the screen, touching each point. Using a stylus is suggested so you get a precise touch. Don't use something metal, plastic only!
Next you can run
sudo TSLIB_FBDEVICE=/dev/fb1 TSLIB_TSDEVICE=/dev/input/touchscreen ts_test
which will let you draw-test the touch screen. Go back and re-calibrate if you feel the screen isn't precise enough!
X Calibration
You can also calibrate the X input system but you have to use a different program called xtcal (xinput_calibrator no longer works)
You can do this if the calibration on the screen isn't to your liking or any time you change the rotate=XX module settings for the screen. Since the screen and touch driver are completely separated, the touchscreen doesn't auto-rotate
Download and compile it with the following:
sudo apt-get install libxaw7-dev libxxf86vm-dev libxaw7-dev libxft-dev git clone https://github.com/KurtJacobson/xtcal cd xtcal make
You must be running PIXEL (the GUI) while calibrating.
Before you start the calibrator you will need to 'reset' the old calibration data so run
DISPLAY=:0.0 xinput set-prop "stmpe-ts" 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
Now you'll have to run the calibrator while also running X. You can do this by opening up the terminal program and running the the xtcal command (which is challenging to do on such a small screen) OR you can do what we do which is create an SSH/Terminal shell and then run the calibrator from the same shell, which requires the following command:
DISPLAY=:0.0 xtcal/xtcal -geometry 640x480
Note that the geometry
may vary!
If you are using a 2.4"/2.8"/3.2" 320x240 display with landscape orientation, use 640x480. If you're in portrait, use 480x640.
If you are using a 3.5" display with landscape, use 720x480, portrait is 480x720
Follow the directions on screen
Run sudo nano /usr/share/X11/xorg.conf.d/20-calibration.conf and copy the 9 numbers into the TransformationMatrix option so it looks like:
Section "InputClass" Identifier "STMPE Touchscreen Calibration" MatchProduct "stmpe" MatchDevicePath "/dev/input/event*" Driver "libinput" Option "TransformationMatrix" "-0.000087 1.094214 -0.028826 -1.091711 -0.004364 1.057821 0 0 1" EndSection
or whatever you got, into there.
You will want to reboot your Pi to verify you're done
Your touchscreen is now super calibrated, hurrah!
Page last edited March 08, 2024
Text editor powered by tinymce.