Ported using tslib library

In the last chapter, we learned how to write touch screen applications, including single-touch and multi-touch, mainly analyzing the read struct input_event type data to obtain the coordinates of each touch point. This chapter introduces you to the tslib library, which is an application layer function library specially developed for touch screens under Linux systems. In this chapter we will learn how to write touch screen applications based on the tslib library.

Introduction to tslib

Insert image description here
tslib is a Linux application layer function library specially developed for touch screen devices, and it is open source, which means that we can directly obtain the
source code of tslib. The next section will introduce how to obtain the source code of tslib.
tslib is the adaptation layer between the touch screen driver and the application layer. It encapsulates the operation process of reading the touch screen struct input_event type data (this is the original data reported by the input device to the application layer) and parsing it in the application, and provides Users provide encapsulated API interfaces. tslib obtains the original coordinate data from the touch screen, and removes the noise and converts the original touch screen coordinates into the corresponding screen coordinates through a series of denoising, debouncing, coordinate transformation and other operations.
tslib has a configuration file ts.conf, which provides some configuration parameters that users can modify. The specific configuration information will be introduced later!
tslib can be used as a touch screen input plug-in for Qt to provide touch input support for Qt. Readers who have developed
Qt applications under the embedded Linux hardware platform should know; of course, not only tslib can be used as a plug-in for Qt to provide touch input support. Input support, and many plug-ins are available, but most of them will choose to use tslib.
So much about tslib, let's see how to port the tslib library to our development board platform.

tslib porting

Download tslib source code

First download the tslib source code package, go to the git warehouse of tslib and download the source code https://github.com/libts/tslib/releases, as follows:
Insert image description here
tslib has been transplanted into the ALPHA/Mini development board factory system, and the version is 1.16. You can The development board executes the ts_finddev command to view its version information, as shown below:
Insert image description here
So for the sake of unification, we also download the 1.16 version of tslib, scroll down to find the download link for the 1.16 version:
Insert image description here
click on the red box to enter the download page:
Insert image description here
it is recommended to download tar. bz2 or tar.gz format compressed package, or tar.xz compressed package. Here I download the tar.gz format compressed package file, click on the text to download.
Insert image description here

Compile tslib source code

Copy the tslib-1.16.tar.gz source code package to the user's home directory of the Ubuntu system:
Insert image description here
extract it to the current directory:

tar -xzf tslib-1.16.tar.gz

Insert image description here
After decompression, the tslib-1.16 directory will be generated. Create a tools directory in the home directory, and then create the tslib directory under the tools directory. When the tslib library is compiled, specify the installation directory here, as shown below: Enter the tslib-1.16 directory
Insert image description here
. , prepare to compile the tslib source code:
Insert image description here
Next, compile. The compilation of the entire source code is divided into 3 steps:
⚫ The first step is to configure the project;
⚫ The second step is to compile the project;
⚫ The third step is to install, and compile the Library files, executable files, etc. are installed into a specified directory.
First, before configuring the project, set up the environment of the cross-compilation tool and use source to execute the environment-setup-cortexa7hf-neon-poky-linux-gnueabi script file in the cross-compilation tool installation directory:

source /opt/fsl-imx-x11/4.1.15-2.1.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi

Execute the following command to configure the tslib source code project:

./configure --host=arm-poky-linux-gnueabi --prefix=/home/dt/tools/tslib/

As for how the project is configured, you can execute ./configure --help to view its configuration options and meanings. The –host option is used to specify which platform the cross-compiled library file will run on. Usually –host is set to cross-compilation. The prefix of the device name, for example, arm-poky-linux-gnueabi-gcc. The prefix is ​​arm-poky-linux-gnueabi; the –prefix option is used to specify the installation path of the library file. We set the installation path to the one previously created in the home directory. tools/tslib directory.
Figure 18.2.10 Configuration project

Then compile the project and execute make directly:

make

Insert image description here
Finally execute make install to install:

make install

Insert image description here

Introduction to the folders in the tslib installation directory

Enter the tslib installation directory:
Insert image description here
bin directory
There are some small tools provided by tslib in the bin directory, which can be used to test the touch screen, as shown below:
Insert image description here
etc directory
There is a configuration file ts.conf in the etc directory, which I mentioned to you before.
Figure 18.2.15 Configuration file ts.conf

Open the ts.conf file to see what configuration options it has:
Insert image description here
module_raw input: Uncomment and enable support for input input events;
module pthres pmin=1: If our device supports pressure test, then you can uncomment it. pmin is used to adjust the pressing force sensitivity, and the default is equal to 1.
module dejitter delta=100: tslib provides a touch screen denoising algorithm plug-in. If you need to filter noise samples, uncomment it. The default parameter delta=100.
Module linear: tslib provides the function of coordinate transformation on the touch screen, such as exchanging X and Y coordinates, rotating coordinates, etc. If we need to implement coordinate transformation, we can remove the comments.
There is no need to change it here, just use the default configuration.
include directory
There is only one header file tslib.h in the include directory. This header file contains some structure data structures and API interface declarations. This header file needs to be included to use the API provided by tslib.
lib directory The
lib directory contains library files obtained by compiling the tslib source code. By default, these are dynamic library files. You can also configure the tslib project to generate static library files; the ts directory stores some plug-in libraries.
Figure 18.2.17 lib directory

The share directory
can be ignored!

Test tslib on the development board

The last step of transplantation is to copy the library files in the tslib installation directory, the configuration files under etc and the compiled test tools to the Linux system directory of the development board. Since the tslib library has been transplanted in the factory system of the development board, we will do this here. No need to copy. But if you make your own root file system and have not transplanted tslib, then you need to copy these libraries, executable files and configuration files to the root file system. How to copy them? Here is a brief mention:
⚫ Copy all executable files in the installation directory bin/ directory to the development board /usr/bin directory;
⚫ Copy the configuration file ts.conf in the installation directory etc/ directory to the development board /etc ⚫
Copy all library files in the installation directory lib/ directory to the development board /usr/lib directory.
After copying the test tools, library files and configuration files in the installation directory to the development board, you need to configure some environment variables, because when tslib works, it needs to
rely on some environment variables. For example, it will learn ts by reading environment variables. The .conf configuration file, the path of the library file, and the device node corresponding to the touch screen we want to test, etc.

export TSLIB_CONSOLEDEVICE=none
export TSLIB_FBDEVICE=/dev/fb0
export TSLIB_TSDEVICE=/dev/input/event1
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_PLUGINDIR=/usr/lib/ts

TSLIB_CONSOLEDEVICE: used to configure the console device file name, just configure it as none!
TSLIB_FBDEVICE: used to configure the name of the display device. tslib provides a test tool for finger touch and line drawing, which needs to be displayed on the LCD, so here you need to specify a device node of the display device.
TSLIB_TSDEVICE: used to configure the device node corresponding to the touch screen, configure according to the actual situation.
TSLIB_CONFFILE: used to configure the path of the ts.conf file.
TSLIB_PLUGINDIR: Used to configure the path where the plug-in is located.
Insert image description here
If you want these commands to take effect every time you start the system, you can place these commands in the /etc/profile script for execution; they have been configured in the factory system and do not require user configuration.
Then we use the test tools provided by tslib to test the touch screen. It provides single-touch test tools (ts_print, ts_test) and multi-touch test tools (ts_print_mt, ts_test_mt). ts_print and ts_print_mt can print touch point information on the terminal, and ts_test and
ts_test_mt supports drawing lines on the LCD.
Execute the ts_print command:
Insert image description here
After executing the ts_print command, sliding on the touch screen, or pressing and releasing the touch screen will print out the corresponding information on the terminal. The same
is true for ts_print_mt, but it supports multi-touch and can print information of multiple touch points:
Figure 18.2.20 ts_print_mt test tool

ts_test and ts_test_mt support touch screen line drawing operations. We will not demonstrate them here. Just test them yourself! If you want to see the source code implementation of these testing tools, you can find it in the tslib source code. The specific path is the tests folder under the tslib source code directory:
Figure 18.2.21 tests folder

For example, the source code corresponding to the ts_test program is implemented as ts_test.c. No matter how it is done, it will ultimately be implemented into the content introduced to you in the previous chapter.

Introduction to tslib library functions

This section introduces how to use the API interface provided by tslib to write touch screen applications. To use tslib library functions, we need to include the tslib header file tslib.h in our application. Programming using tslib is actually very simple. The basic steps are as follows: Chapter
1 One step is to open the touch screen device;
the second step is to configure the touch screen device;
the third step is to read the touch screen data.

Turn on the touch screen device

Use the library function ts_open provided by tslib to open the touch screen device. The function prototype is as follows:

#include "tslib.h"
struct tsdev *ts_open(const char *dev_name, int nonblock);

The parameter dev_name specifies the device node of the touch screen; the parameter nonblock indicates whether to open the touch screen device in non-blocking mode. If
nonblock is equal to 0, it indicates blocking mode. If it is a non-0 value, it indicates opening in non-blocking mode.
A successful call returns a struct tsdev * pointer pointing to the touch screen device handle; if the device fails to be opened, NULL will be returned.
In addition to using ts_open() to open the device, you can also use the ts_setup() function, whose function prototype is as follows:

#include "tslib.h"
struct tsdev *ts_setup(const char *dev_name, int nonblock)

The parameter dev_name specifies the device node of the touch screen, which has the same meaning as the dev_name parameter in the ts_open() function; but for ts_setup(), the parameter dev_name
can be set to NULL. When dev_name is set to NULL, the ts_setup() function will read it internally.
TSLIB_TSDEVICE environment variable, obtain the content of this environment variable to know the device node of the touch screen.
The meaning of the parameter nonblock is the same as that of the ts_open() function.
Compared with ts_open(), ts_setup() not only opens the touch screen device, but also configures the touch screen device, which is the second step mentioned next.
To close the touch screen device, use the ts_close() function:
int ts_close(struct tsdev *);

Configure touch screen devices

Call the ts_config() function for configuration. The function prototype is as follows:

#include "tslib.h"
int ts_config(struct tsdev *ts)

The parameter ts points to the touch screen handle.
Returns 0 on success and -1 on failure.
The so-called configuration actually refers to parsing the configuration information in the ts.conf file and loading the corresponding plug-ins.

Read touch screen data

To read touch screen data, use the ts_read() or ts_read_mt() function. The difference is that ts_read is used to read single-touch data, while
ts_read_mt is used to read multi-touch data. The function prototype is as follows:

#include "tslib.h"
int ts_read(struct tsdev *ts, struct ts_sample *samp, int nr)
int ts_read_mt(struct tsdev *ts, struct ts_sample_mt **samp, int max_slots, int nr)

The parameter ts points to a touch screen device handle, and the parameter nr represents the number of samples for a touch point. Just set it to 1!
The ts_read_mt() function has a max_slots parameter, which indicates the maximum number of touch points supported by the touch screen. The application can obtain
information such as the maximum number of touch points supported by the touch screen and the maximum resolution of the touch screen coordinates by calling the ioctl() function, which will be introduced to you later.
The samp parameter of the ts_read() function is a pointer of type struct ts_sample *, pointing to a struct ts_sample object. The struct ts_sample data structure describes the information of the touch point; the data obtained by calling the ts_read() function will be stored in the location pointed to by the samp pointer. in memory. The contents of the struct ts_sample structure are as follows:

struct ts_sample
{
    
    
    int x;                 // X 坐标
    int y;                 // Y 坐标
    unsigned int pressure; // 按压力大小
    struct timeval tv;     // 时间
};

The samp parameter of the ts_read_mt() function is a pointer of type struct ts_sample_mt **. For multi-touch applications, the information of each touch point is described using the struct ts_sample_mt data structure; the data of a touch point is loaded using a struct ts_sample_mt object. Organize them into a struct ts_sample_mt array. When calling ts_read_mt(), assign the array address to the samp parameter.
The contents of the struct ts_sample structure are as follows:

struct ts_sample_mt
{
    
    
    /* ABS_MT_* event codes. linux/include/uapi/linux/input-event-codes.h
     * has the definitions.
     */
    int x;                 // X 坐标
    int y;                 // Y 坐标
    unsigned int pressure; // 按压力大小
    int slot;              // 触摸点slot
    int tracking_id;       // ID
    int tool_type;
    int tool_x;
    int tool_y;
    unsigned int touch_major;
    unsigned int width_major;
    unsigned int touch_minor;
    unsigned int width_minor;
    int orientation;
    int distance;
    int blob_id;
    struct timeval tv; // 时间
    /* BTN_TOUCH state */
    short pen_down; // BTN_TOUCH 的状态
    /* valid is set != 0 if this sample
     * contains new data; see below for the
     * bits that get set.
     * valid is set to 0 otherwise
     */
    short valid; // 此次样本是否有效标志触摸点数据是否发生更新
};

Write touch screen applications based on tslib

Through a brief introduction to the tslib library functions, this section will write touch screen applications based on tslib, including single-touch applications and multi-touch applications.

Single touch application

The path corresponding to the source code of this routine is: development board CD->11, Linux C application programming routine source code->18_tslib->ts_read.c.

#include <stdio.h>
#include <stdlib.h>
#include <tslib.h> //包含tslib.h 头文件
int main(int argc, char *argv[])
{
    
    
    struct tsdev *ts = NULL;
    struct ts_sample samp;
    int pressure = 0; // 用于保存上一次的按压力,初始为0,表示松开
    /* 打开并配置触摸屏设备*/
    ts = ts_setup(NULL, 0);
    if (NULL == ts)
    {
    
    
        fprintf(stderr, "ts_setup error");
        exit(EXIT_FAILURE);
    }
    /* 读数据*/
    for (;;)
    {
    
    
        if (0 > ts_read(ts, &samp, 1))
        {
    
    
            fprintf(stderr, "ts_read error");
            ts_close(ts);
            exit(EXIT_FAILURE);
        }
        if (samp.pressure)
        {
    
                     // 按压力>0
            if (pressure) // 若上一次的按压力>0
                printf("移动(%d, %d)\n", samp.x, samp.y);
            else
                printf("按下(%d, %d)\n", samp.x, samp.y);
        }
        else
            printf("松开\n"); // 打印坐标
        pressure = samp.pressure;
    }
    ts_close(ts);
    exit(EXIT_SUCCESS);
}

The code is very simple, so I won’t explain it anymore. Just open and configure the device, and then read the data! The touch status is determined by judging the pressing force. If the pressing force is equal to 0, it means that the finger has been released; if the pressing force is greater than 0, it needs to be judged based on whether the last pressing force is greater than 0.
When an error occurs while reading data, ts_read() returns a negative number.
Next, compile the application. When compiling the code, you need to specify the path of the header file, library file and dynamic link library file name through the cross-compiler:

${
    
    CC} -I /home/dt/tools/tslib/include -L /home/dt/tools/tslib/lib -lts -o testApp testApp.c

Figure 18.4.1 Compiling a single-touch application

The -I option specifies the path to the header file, that is, specifies the include directory under the tslib installation directory. If the header file path is not specified, the tslib.h header file will not be found during compilation; the -L option is used to specify the path to the library file. , that is, specify the lib directory under the tslib installation directory; we compile tslib into dynamic library files, which are provided in the form of library files. These library files need to be linked to during compilation; and the -l option is used to specify the link library (also It can be written as -l ts, that is, libts.so library file, in Linux, the dynamic library file is named as lib+name+.so).
Copy the compiled executable file to the user's home directory of the development board's Linux system, execute the application, and test:
Insert image description here

Multi-touch application

In this section we will write a multi-touch application based on tslib and use the ts_read_mt() function to read multi-touch data.
The path corresponding to the source code of this routine is: development board CD->11, Linux C application programming routine source code->18_tslib->ts_read_mt.c.

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <tslib.h>
int main(int argc, char *argv[])
{
    
    
    struct tsdev *ts = NULL;
    struct ts_sample_mt *mt_ptr = NULL;
    struct input_absinfo slot;
    int max_slots;
    unsigned int pressure[12] = {
    
    0}; // 用于保存每一个触摸点上一次的按压力,初始为0,表示松开
    int i;
    /* 打开并配置触摸屏设备*/
    ts = ts_setup(NULL, 0);
    if (NULL == ts)
    {
    
    
        fprintf(stderr, "ts_setup error");
        exit(EXIT_FAILURE);
    }
    /* 获取触摸屏支持的最大触摸点数*/
    if (0 > ioctl(ts_fd(ts), EVIOCGABS(ABS_MT_SLOT), &slot))
    {
    
    
        perror("ioctl error");
        ts_close(ts);
        exit(EXIT_FAILURE);
    }
    max_slots = slot.maximum + 1 - slot.minimum;
    printf("max_slots: %d\n", max_slots);
    /* 内存分配*/
    mt_ptr = calloc(max_slots, sizeof(struct ts_sample_mt));
    /* 读数据*/
    for (;;)
    {
    
    
        if (0 > ts_read_mt(ts, &mt_ptr, max_slots, 1))
        {
    
    
            perror("ts_read_mt error");
            ts_close(ts);
            free(mt_ptr);
            exit(EXIT_FAILURE);
        }
        for (i = 0; i < max_slots; i++)
        {
    
    
            if (mt_ptr[i].valid)
            {
    
     // 有效表示有更新!
                if (mt_ptr[i].pressure)
                {
    
                                     // 如果按压力>0
                    if (pressure[mt_ptr[i].slot]) // 如果上一次的按压力>0
                        printf("slot<%d>, 移动(%d, %d)\n", mt_ptr[i].slot, mt_ptr[i].x, mt_ptr[i].y);
                    else
                        printf("slot<%d>, 按下(%d, %d)\n", mt_ptr[i].slot, mt_ptr[i].x, mt_ptr[i].y);
                }
                else
                    printf("slot<%d>, 松开\n", mt_ptr[i].slot);
                pressure[mt_ptr[i].slot] = mt_ptr[i].pressure;
            }
        }
    }
    /* 关闭设备、释放内存、退出*/
    ts_close(ts);
    free(mt_ptr);
    exit(EXIT_SUCCESS);
}

The whole idea is the same as the single-touch application. Focus on the inside of the for() loop, read the touch point data through the ts_read_mt() function, store the data in the mt_ptr array, and then judge each touch point data in the fof() loop. Is it valid? If valid, it means that the touch point information has been updated.
Compile the application:

${
    
    CC} -I /home/dt/tools/tslib/include -L /home/dt/tools/tslib/lib -lts -o testApp testApp.c

Insert image description here
Copy the compiled executable file to the user's home directory of the development board's Linux system, and execute the program:
Insert image description here
slot<0> represents touch point 0, slot<1> represents touch point 1, and so on. You can test it yourself, it's okay. It can be said!

Guess you like

Origin blog.csdn.net/zhuguanlin121/article/details/132565877