LV15-03-输入类设备-05-触摸屏tslib开源库-02-工具与API

本文主要是输入类设备控制——触摸屏tslib开源库的工具使用和API函数简介的相关笔记,若笔记中有错误或者不合适的地方,欢迎批评指正😃。

点击查看使用工具及版本
PC端开发环境 Windows Windows11
Ubuntu Ubuntu20.04.2的64位版本
VMware® Workstation 17 Pro 17.6.0 build-24238078
终端软件 MobaXterm(Professional Edition v23.0 Build 5042 (license))
Win32DiskImager Win32DiskImager v1.0
Linux开发板环境 Linux开发板 正点原子 i.MX6ULL Linux 阿尔法开发板
uboot NXP官方提供的uboot,NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)
linux内核 linux-4.15(NXP官方提供)
点击查看本文参考资料
分类 网址 说明
官方网站 https://www.arm.com/ ARM官方网站,在这里我们可以找到Cotex-Mx以及ARMVx的一些文档
https://www.nxp.com.cn/ NXP官方网站
https://www.nxpic.org.cn/NXP 官方社区
https://u-boot.readthedocs.io/en/latest/u-boot官网
https://www.kernel.org/linux内核官网
其他网站 kernel - Linux source code (v4.15) - Bootlin linux内核源码在线查看
点击查看相关文件下载
分类 网址 说明
NXP https://github.com/nxp-imx NXP imx开发资源GitHub组织,里边会有u-boot和linux内核的仓库
https://elixir.bootlin.com/linux/latest/source 在线阅读linux kernel源码
nxp-imx/linux-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga NXP linux内核仓库tags中的rel_imx_4.1.15_2.1.0_ga
nxp-imx/uboot-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga NXP u-boot仓库tags中的rel_imx_4.1.15_2.1.0_ga
I.MX6ULL i.MX 6ULL Applications Processors for Industrial Products I.MX6ULL 芯片手册(datasheet,可以在线查看)
i.MX 6ULL Applications ProcessorReference Manual I.MX6ULL 参考手册(下载后才能查看,需要登录NXP官网)

一、工具的使用

我们在编译输出目录会看到许多工具:

image-20240922151739790

它提供了单点触摸测试工具(ts_print、 ts_test)和多点触摸测试工具(ts_print_mt、 ts_test_mt),ts_print 和 ts_print_mt 可以在终端打印触摸点信息,而 ts_test 和ts_test_mt 则支持在 LCD 上画线。

1. ts_print

我们在移植的时候已经配置了环境变量,可以直接在终端执行:

1
ts_print
image-20240922151956017

执行 ts_print 命令之后,在触摸屏上滑动、或按下、松开触摸屏将会在终端打印出相应的信息。

2. ts_print_mt

我们直接在终端执行即可:

1
ts_print_mt
image-20240922152108000

二、API函数

使用 tslib 库函数需要在我们的应用程序中包含 tslib 的头文件 tslib.h 。使用 tslib 编程其实非常简单,基本步骤如下所示:

(1)打开触摸屏设备;

(2)配置触摸屏设备;

(3)读取触摸屏数据。

1. 打开与关闭触摸屏设备

1.1 ts_open

使用 tslib 提供的库函数 ts_open 打开触摸屏设备,其函数原型如下所示:

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

参数 dev_name 指定了触摸屏的设备节点;参数 nonblock 表示是否以非阻塞方式打开触摸屏设备,如果nonblock 等于 0 表示阻塞方式,如果为非 0 值则表示以非阻塞方式打开。

调用成功返回一个 struct tsdev *指针, 指向触摸屏设备句柄;如果打开设备失败,将返回 NULL。

1.2 ts_setup

除了使用 ts_open()打开设备外,还可以使用 ts_setup()函数,其函数原型如下所示:

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

参数 dev_name 指定触摸屏的设备节点,与 ts_open()函数中的 dev_name 参数意义相同; 但对于 ts_setup()来说,参数 dev_name 可以设置为 NULL,当 dev_name 设置为 NULL 时, ts_setup()函数内部会读取TSLIB_TSDEVICE 环境变量, 获取该环境变量的内容以得知触摸屏的设备节点。

参数 nonblock 表示是否以非阻塞方式打开触摸屏设备,如果nonblock 等于 0 表示阻塞方式,如果为非 0 值则表示以非阻塞方式打开。

ts_setup()相比 ts_open(),除了打开触摸屏设备外,还对触摸屏设备进行了配置。

1.3 ts_close

关闭触摸屏设备使用 ts_close()函数:

1
int ts_close(struct tsdev *);  

2. 配置触摸屏设备

调用 ts_config()函数进行配置,其函数原型如下所示:

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

参数 ts 指向触摸屏句柄。

成功返回 0,失败返回-1。

所谓配置其实指的就是解析 ts.conf 文件中的配置信息,加载相应的插件。

3. 读取触摸屏数据

读取触摸屏数据使用 ts_read()或 ts_read_mt()函数,区别在于 ts_read 用于读取单点触摸数据,而ts_read_mt 则用于读取多点触摸数据.

3.1 ts_read

其函数原型如下所示:

1
2
#include "tslib.h"
int ts_read(struct tsdev *ts, struct ts_sample *samp, int nr);

参数 ts 指向一个触摸屏设备句柄。

samp 参数是一个 struct ts_sample *类型的指针,指向一个 struct ts_sample 对象, struct ts_sample 数据结构描述了触摸点的信息; 调用 ts_read()函数获取到的数据会存放在 samp 指针所指向的内存中。 struct ts_sample 结构体内容如下所示:

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

参数 nr 表示对一个触摸点的采样数,设置为 1 即可。

3.2 ts_read_mt

其函数原型如下所示:

1
2
3
#include "tslib.h"

int ts_read_mt(struct tsdev *ts, struct ts_sample_mt **samp, int max_slots, int nr);

参数 ts 指向一个触摸屏设备句柄。

samp 参数是一个 struct ts_sample_mt **类型的指针, 多点触摸应用程序, 每一个触摸点的信息使用 struct ts_sample_mt 数据结构来描述; 一个触摸点的数据使用一个 struct ts_sample_mt 对象来装载,将它们组织成一个 struct ts_sample_mt 数组,调用 ts_read_mt()时, 将数组地址赋值给 samp 参数。struct ts_sample 结构体内容如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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; //此次样本是否有效标志 触摸点数据是否发生更新
};

max_slots 参数,表示触摸屏支持的最大触摸点数, 应用程序可以通过调用 ioctl()函数来获取触摸屏支持的最大触摸点数以及触摸屏坐标的最大分辨率等信息 。

参数 nr 表示对一个触摸点的采样数,设置为 1 即可。