Using A Touch Overlay, In Portrait, On Raspbian Buster

Another wild google-chase in finding reliable information

Published on 03 October 2019

Intro

This is just a short post - mostly for my own benefit - on how to use a touch-overlay, in portrait, on Raspbian Buster

These instructions are for using a standard HDMI monitor (I'm using a Samsung ME46B) and USB Touchscreen overlay (I'm using a Samsung TM46LBC) in a right portrait (i.e rotated 270° clockwise) configuration. They probably won't work for hat/phat style LCDs with built-in touch-panels.

Changing to portrait

Raspbian Buster has moved to the 'G2 GL (Fake KMS) OpenGL desktop driver with fake KMS' OpenGL driver by default (dtoverlay=vc4-fkms-v3d in /boot/config.txt). Fortunately, as they moved to the 'fake' driver, you're still given significant control over the display configuration from /boot/config.txt and changing the display orientation remains unchanged.

As such, use the command sudo nano /boot/config.txt to start editing the file and add the following lines at the bottom (uncommenting the appropriate line):

# display_rotate=1 # rotate 90° clockwise
# display_rotate=2 # rotate 180° clockwise
display_rotate=3 # rotate 270° clockwise
avoid_warnings=1

Reboot the Pi (sudo reboot) and you should see that the display is now in the correct orientation.

Correcting Overscan

Before moving onto installing/calibrating the touch-panel, it is well worth spending some time correcting the Overscan on your display. To do this, again edit config.txt and find lines similar to below:

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
disable_overscan=0

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
overscan_left=-10
overscan_right=-10
overscan_top=8
overscan_bottom=8

The values you see above are those required to make my panel display correctly (nothing off-screen, no big borders). If you need to change overscan you'll need to find the values that work for you by changing values and rebooting to see the effect.

Calibrating the Touch-Panel

While the touch panel I am using was correctly identified as a 'Nexio Touch Device' by xinput --list getting it to respect the display orientation and calibrating it correctly turned out to be a pain. After a lot of searching I finally found two posts which, when combined, got the touch panel working correctly.

Firstly, you'll need to install the evdev driver and the xinput_calibrator using the command:

sudo apt-get install xserver-xorg-input-evdev xinput_calibrator

Next locate the touch-panel display configuration file as described in this instructable; mine was /usr/share/X11/xorg.conf.d/40-libinput.conf

In this file, find the InputClass Section which includes the Identifier libinput touchscreen catchall. In this section, change the Driver value from libinput to evdev and add a TransformationMatrix option to reflect to display orientation as shown below:

Section "InputClass"
        Identifier "libinput touchscreen catchall"
        MatchIsTouchscreen "on"
        MatchDevicePath "/dev/input/event*"
        Driver "evdev" # <- change this
        Option "TransformationMatrix" "0 1 0 -1 0 1 0 0 1" # <- Add this
EndSection

The exact transformation matrix you need is available in the instructable but I've repeated them here for easy reference:

Angle Transformation Matrix
90° "0 -1 1 1 0 0 0 0 1"
180° "-1 0 1 0 -1 1 0 0 1"
270° "0 1 0 -1 0 1 0 0 1"

Now, if you restart your Pi you should notice that the touch panel is at least in the correct orientation (mouse cursor moves in the same direction as your finger). To calibrate it such that the mouse cursor moves to exactly where you touch, use the xinput_calibrator tool by running it from the Terminal window in a desktop session.

Follow the instructions and input_calibrator will do two things:

  1. Correctly calibrate the display - the mouse cursor should now be under your touch; and
  2. Give you a block of text to add to persistent configuration as shown below:
Calibrating EVDEV driver for "Nexio Touch Device(HS) Nexio HID Multi-Touch ATI0460-06 " id=7
	current calibration values (from XInput): min_x=15929, max_x=149 and min_y=16328, max_y=171

Doing dynamic recalibration:
	Setting calibration data: 15872, 0, 16498, -69
	--> Making the calibration permanent <--
  copy the snippet below into '/etc/X11/xorg.conf.d/99-calibration.conf' (/usr/share/X11/xorg.conf.d/ in some distro's)
Section "InputClass"
	Identifier	"calibration"
	MatchProduct	"Nexio Touch Device(HS) Nexio HID Multi-Touch ATI0460-06 "
	Option	"Calibration"	"15872 0 16498 -69"
	Option	"SwapAxes"	"0"
EndSection

Follow the instructions it provides by copying the region from Section "InputClass" down to EndSection then using the following command to create (or edit) the persistent configuration file:

sudo nano /usr/share/X11/xorg.conf.d/99-calibration.conf

Paste the block of text you copied earlier, save and exit, then reboot.

Voila, a perfectly configured touch overlay. Enjoy!