Using TSLib for Input Interface on Embedded Linux Platform with SSD212 Example

Click the above “Embedded Application Research Institute” and select “Top/Star Official Account

Valuable Resources Delivered Instantly!

Source | Embedded Application Research Institute

Compiled & formatted | Embedded Application Research Institute

Since the coordinates reported by the driver are not flipped, the reported coordinates are reversed in both X and Y directions on the screen. However, I do not want to modify the driver, so I can only implement coordinate transformation in the Linux application. TSLib has good support for resistive screens, and since the TSLib-related libraries have been ported to my system, I can use them directly.

1. Configuration of TSLib in the File System

Using TSLib for Input Interface on Embedded Linux Platform with SSD212 Example

If you want TSLib to load automatically at boot, you need to add source /etc/profile in rcS to make the environment variables effective. This way, the TSLib environment will automatically load the relevant environment variables successfully when the Linux system starts and enters the file system.

Using TSLib for Input Interface on Embedded Linux Platform with SSD212 Example

2. Implementing Event Initialization and Event Read Functions
#include "evdev.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include "tslib.h"
int evdev_root_x;
int evdev_root_y;
int evdev_button;
struct tsdev *ts;
struct ts_sample samp;
// Event initialization
void evdev_init(void)
{
  // Open event node in read-only & non-blocking mode
  // The call to ts_setup requires the TSLIB_TSDEVICE variable to be declared in the environment
  // Otherwise, it is recommended to use the ts_open function for initialization
  ts = ts_setup(NULL, O_RDONLY | O_NONBLOCK);
  if (!ts) {
    perror("ts_setup");
    exit(1);
  }
  evdev_root_x = 0;
  evdev_root_y = 0;
  evdev_button = LV_INDEV_STATE_REL;
}

// Read input events
void evdev_read(lv_indev_drv_t * drv, lv_indev_data_t * data)
{
  // When touch is detected, assign the coordinates and state to temporary variables
  while(ts_read(ts, &samp, 1) > 0)
  {
    evdev_root_x = samp.x;
    evdev_root_y = samp.y;
  }
  if(0 == samp.pressure)
    evdev_button = LV_INDEV_STATE_REL;
  else
    evdev_button = LV_INDEV_STATE_PR;
  // Register variables into LVGL input device interface environment
  data->point.x = evdev_root_x;
  data->point.y = evdev_root_y;
  data->state = evdev_button;
  // Coordinate limits
  if(data->point.x < 0)
     data->point.x = 0;
  if(data->point.y < 0)
     data->point.y = 0;
  if(data->point.x >= drv->disp->driver->hor_res)
     data->point.x = drv->disp->driver->hor_res - 1;
  if(data->point.y >= drv->disp->driver->ver_res)
     data->point.y = drv->disp->driver->ver_res - 1;
  return;
}

To enable LVGL to support input, we need to register the implemented driver as per the documentation:

Using TSLib for Input Interface on Embedded Linux Platform with SSD212 Example

Therefore, in the main function, we need to perform the following call to register the input device:

evdev_init();
static lv_indev_drv_t indev_drv_1;
lv_indev_drv_init(&indev_drv_1);
indev_drv_1.type = LV_INDEV_TYPE_POINTER; // Touchscreen or mouse
indev_drv_1.read_cb = evdev_read;
lv_indev_drv_register(&indev_drv_1);

At this point, the LVGL input device interface can call the TSLib interface to read touch events.

Additionally, it is important to note that the TSLib path needs to be added in the Makefile:

#
# Makefile
#
CC := arm-linux-gnueabihf-gcc
LVGL_DIR_NAME ?= lvgl
LVGL_DIR ?= ${shell pwd}
CFLAGS ?= -O3 -g0 -I$(LVGL_DIR)/ -Wall -Wshadow -Wundef -Wmissing-prototypes -Wno-discarded-qualifiers -Wall -Wextra -Wno-unused-function -Wno-error=strict-prototypes -Wpointer-arith -fno-strict-aliasing -Wno-error=cpp -Wuninitialized -Wmaybe-uninitialized -Wno-unused-parameter -Wno-missing-field-initializers -Wtype-limits -Wsizeof-pointer-memaccess -Wno-format-nonliteral -Wno-cast-qual -Wunreachable-code -Wno-switch-default -Wreturn-type -Wmultichar -Wformat-security -Wno-ignored-qualifiers -Wno-error=pedantic -Wno-sign-compare -Wno-error=missing-prototypes -Wdouble-promotion -Wclobbered -Wdeprecated -Wempty-body -Wtype-limits -Wshift-negative-value -Wstack-usage=2048 -Wno-unused-value -Wno-unused-parameter -Wno-missing-field-initializers -Wuninitialized -Wmaybe-uninitialized -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wtype-limits -Wsizeof-pointer-memaccess -Wno-format-nonliteral -Wpointer-arith -Wno-cast-qual -Wmissing-prototypes -Wunreachable-code -Wno-switch-default -Wreturn-type -Wmultichar -Wno-discarded-qualifiers -Wformat-security -Wno-ignored-qualifiers -Wno-sign-compare -I /home/yw/share/tslib_1.22/include/;
LDFLAGS ?= -lm -lts -lpthread -L/home/yangyx/share/tslib_1.22/lib/;
BIN = wifi_scan_list
....省略....
....

As shown above, we need to add the TSLib library path in the LDFLAGS:

-L/home/yangyx/share/tslib_1.22/lib/

This allows the program to recognize the TSLib interfaces called in our code. Since this program is used on the ARM platform, it needs to be modified according to different cross-compilation toolchains. Here, I am using the SSD212 platform, so it is configured as:

CC := arm-linux-gnueabihf-gcc

After making these modifications, recompile the program and then transfer the executable file to the corresponding platform for use. Currently, the application routine is still being modified, and will be shared on GitHub or Gitee later, with the address provided in the comments.

Using TSLib for Input Interface on Embedded Linux Platform with SSD212 Example

Previous Highlights

Several methods to download Keil MDK software packages (packs)

A step-by-step guide to using GCC compiler toolchain in Keil MDK

Sharing a configuration guide for code formatting tool Astyle (beautifying code style) in Keil development environment

Keil MDK will be upgraded to Keil Studio, the black theme you want is here, includes a step-by-step usage tutorial

Some recommended features of Keil MDK (coding format, auto-save, code hints, dynamic syntax checking, multi-core compilation)

If you find this article helpful, please click <span>[Looking]</span> and share it, it would also support me.

Leave a Comment