第三方库介绍——tslib库

第三方库介绍——tslib库第三方库介绍 tslib 库 tslib

大家好,欢迎来到IT知识分享网。

tslib 简介

tslib 是一个触摸屏的开源库,可以使用它来访问触摸屏设备,可以给输入设备添加各种“ filter ”( 过滤器,就是各种处理)

编译 tslib 后,可以得到 libts 库,还可以得到各种工具:较准工具、测试工具。

官网地址:C library for filtering touchscreen events

tslib 框架分析

tslib 的主要代码位置
在这里插入图片描述

  • plugins目录下的每个文件都是一个 module,每个模块都会被编译成动态库,每个 module 都提供 2 个函数:read、read_mt,前者用于读取单点触摸屏的数据,后者用于读取多点触摸屏的数据。
  • tests目录下是测试程序:参考 ts_test.c 和ts_test_mt.c,前者用于一般触摸屏(比如电阻屏、单点电容屏),后者用于多点触摸屏。

tslib 的框架

在这里插入图片描述

框架分析:框架分析视频

交叉编译、测试 tslib

安装依赖库:

sudo apt-get install autoconf automake libtool 
./configure --host=arm-buildroot-linux-gnueabihf --prefix=/ make make install DESTDIR=$PWD/tmp cd tmp/ cp include/* /home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/arm-buildroot-linux-gnueabihf/sysroot/usr/include cp lib/*so* -d /home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/ 

make install后,会在tmp目录生成4个子目录:include lib etc bin

  • lib目录下还有一个子目录ts,它包含了许多校准用到的库(如input.so等)。
  • etc下的ts.conf为配置文件
  • bin目录下为校准、测试工具(如校准的ts_calibrate,测试用的ts_test),可以直接放在开发板上运行

把前面编译出来的 tslib-1.21/tmp/部分文件复制到板子上,因为程序运行时需要

sudo cp lib/*so* -d /home/book/nfs_rootfs/lib sudo cp bin/* /home/book/nfs_rootfs/bin sudo cp etc/ts.conf -d /home/book/nfs_rootfs/etc 

此时开发板的网络文件系统mnt中有共享的文件,将文件拷贝到开发板的/bin和/lib和/etc目录下

cp lib/*so* -d /lib cp bin/* /bin cp etc/* /etc 

在mnt/lib目录下,将两个文件夹也拷贝到lib目录下

cp ts /lib -rf cp pkgconfig/ /lib -rf 

首先需要关闭默认的 qt gui 程序,才可以执行ts_print_mt测试命令,关闭 qt 命令如下所示:

systemctl stop myir 

在单板上执行测试程序,因为放在/bin目录下,可以直接执行:

ts_print_mt 

恢复开发板

systemctl start myir 

tslib应用程序

触摸屏可能支持多个触点,比如 5 个:tslib 为了简化处理,即使只有 2个触点,ts_read_mt 函数也会返回 5 个触点数据(存储在ts_sample_mt结构体中),可以根据标志位判断数据是否有效。

在这里插入图片描述
ts_sample_mt 结构体如下:
在这里插入图片描述

#include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <getopt.h>  #include <linux/input.h> #include <sys/ioctl.h> #include <tslib.h> int distance(struct ts_sample_mt *point1, struct ts_sample_mt *point2) { 
    int x = point1->x - point2->x; int y = point1->y - point2->y; return x*x + y*y; } int main(int argc, char **argv) { 
    struct tsdev *ts; int i; int ret; struct ts_sample_mt **samp_mt; struct ts_sample_mt **pre_samp_mt; int max_slots; int point_pressed[20]; struct input_absinfo slot; int touch_cnt = 0; ts = ts_setup(NULL, 0); if (!ts) { 
    printf("ts_setup err\n"); return -1; } if (ioctl(ts_fd(ts), EVIOCGABS(ABS_MT_SLOT), &slot) < 0) { 
    perror("ioctl EVIOGABS"); ts_close(ts); return errno; } max_slots = slot.maximum + 1 - slot.minimum; samp_mt = malloc(sizeof(struct ts_sample_mt *)); if (!samp_mt) { 
    ts_close(ts); return -ENOMEM; } samp_mt[0] = calloc(max_slots, sizeof(struct ts_sample_mt)); if (!samp_mt[0]) { 
    free(samp_mt); ts_close(ts); return -ENOMEM; } pre_samp_mt = malloc(sizeof(struct ts_sample_mt *)); if (!pre_samp_mt) { 
    ts_close(ts); return -ENOMEM; } pre_samp_mt[0] = calloc(max_slots, sizeof(struct ts_sample_mt)); if (!pre_samp_mt[0]) { 
    free(pre_samp_mt); ts_close(ts); return -ENOMEM; } for ( i = 0; i < max_slots; i++) pre_samp_mt[0][i].valid = 0; while (1) { 
    ret = ts_read_mt(ts, samp_mt, max_slots, 1); if (ret < 0) { 
    printf("ts_read_mt err\n"); ts_close(ts); return -1; } for (i = 0; i < max_slots; i++) { 
    if (samp_mt[0][i].valid) { 
    memcpy(&pre_samp_mt[0][i], &samp_mt[0][i], sizeof(struct ts_sample_mt)); } } touch_cnt = 0; for (i = 0; i < max_slots; i++) { 
    if (pre_samp_mt[0][i].valid && pre_samp_mt[0][i].tracking_id != -1) point_pressed[touch_cnt++] = i; } if (touch_cnt == 2) { 
    printf("distance: %08d\n", distance(&pre_samp_mt[0][point_pressed[0]], &pre_samp_mt[0][point_pressed[1]])); } } return 0; } 

参考视频:编写应用程序

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/116558.html

(0)
上一篇 2025-11-28 09:10
下一篇 2025-11-28 09:20

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信