Touchscreen

The Librem 5 development board is supplied with an LCD screen with an overlaid touchscreen. User input from the screen is delivered to user-space via touch events. This guide describes how to receive and interpret these events.

Note: This guide assumes that you are using a device tree binary file that is configured for LCD display and touchscreen input. Follow the procedure in Next Steps to copy the correct file into place on the development board.

Permissions

The commands and script given in the next section will need to be executed using sudo unless the user is in the input group. We assume that the user has been added to this group from this point onwards.

Receiving Touch Events

Events from the touchscreen are typically delivered to a file in the /dev/input directory. The exact file can be found by using the lsinput tool from the input-utils Debian package. Install and run the tool like this:

sudo apt install input-utils
lsinput

This should produce output containing an entry like the following:

/dev/input/event4
   bustype : BUS_I2C
   vendor  : 0x416
   product : 0x1638
   version : 256
   name    : "Goodix Capacitive TouchScreen"
   phys    : "input/ts"
   bits ev : (null) (null) (null)

One way to examine these events is to run the evtest command on the device file; in this case, the /dev/input/event4 file:

evtest /dev/input/event4

This will listen for events from the touchscreen and write them in human-readable form to the console.

Events can also be read using the python-evdev package. The following program reads events from the device and prints their screen coordinates:

#!/usr/bin/env python3

import evdev
from evdev.ecodes import (ABS_MT_TRACKING_ID, ABS_MT_POSITION_X,
                          ABS_MT_POSITION_Y)
import select
import sys


# Obtain an object for the input device.
for path in evdev.list_devices():
    device = evdev.InputDevice(path)
    if evdev.ecodes.EV_ABS in device.capabilities():
        break
else:
    sys.stderr.write('Failed to find the touchscreen.\n')
    sys.exit(1)

while True:

    # Wait for an event.
    r, w, x = select.select([device.fd], [], [])

    id_ = -1
    x = y = 0

    # Read the events for the device.
    for event in device.read():

        if event.code == event.value == 0:
            if id_ != -1:
                print(x, y)
        elif event.code == ABS_MT_TRACKING_ID:
            id_ = event.value
        elif event.code == ABS_MT_POSITION_X:
            x = event.value
        elif event.code == ABS_MT_POSITION_Y:
            y = event.value

The python-evdev tutorial shows several ways of handling events that can be applied to various situations, using different techniques to read the event queue.