Mar 19, 2015

Raspberry Pi: TFT 3.5" Touchscreen over Python

tslib (SDL, pygame)

tslib is used by some non X windows applications and frameworks.



Input devices get a device name which depends on the order of detection (/dev/input/eventX).

These udev rules will create a symlink /dev/input/touchscreen pointing to the touch controller device. Use the one matching your touch controller. There is no harm in adding both. Reloading the driver or rebooting is necessary for the change to take effect.

/etc/udev/rules.d/95-ads7846.rules

SUBSYSTEM=="input", KERNEL=="event[0-9]*", ATTRS{name}=="ADS7846*", SYMLINK+="input/touchscreen"

/etc/udev/rules.d/95-stmpe.rules

SUBSYSTEM=="input", ATTRS{name}=="stmpe-ts", ENV{DEVNAME}=="*event*", SYMLINK+="input/touchscreen"

Install tslib

sudo apt-get install -y tslib libts-bin
# install ts_test with Quit button
sudo wget -O /usr/bin/ts_test http://tronnes.org/downloads/ts_test
sudo chmod +x /usr/bin/ts_test

Set environment variables and let them pass through when using sudo

# tslib environment variables
cat <<EOF | sudo tee /etc/profile.d/tslib.sh
export TSLIB_TSDEVICE=/dev/input/touchscreen
export TSLIB_FBDEVICE=/dev/fb1
EOF
cat <<EOF | sudo tee /etc/sudoers.d/tslib
Defaults env_keep += "TSLIB_TSDEVICE TSLIB_FBDEVICE"
EOF
sudo chmod 0440 /etc/sudoers.d/tslib
# SDL environment variables
cat <<EOF | sudo tee /etc/profile.d/sdl.sh
export SDL_VIDEODRIVER=fbcon
export SDL_FBDEV=/dev/fb1
if [[ -e /dev/input/touchscreen ]]; then
  export SDL_MOUSEDRV=TSLIB
  export SDL_MOUSEDEV=/dev/input/touchscreen
fi
EOF
cat <<EOF | sudo tee /etc/sudoers.d/sdl
Defaults env_keep += "SDL_VIDEODRIVER SDL_FBDEV SDL_MOUSEDRV SDL_MOUSEDEV"
EOF
sudo chmod 0440 /etc/sudoers.d/tslib

Re-login to set the environment variables.

Calibrate

sudo ts_calibrate

Test

sudo ts_test

More info...