Linux驱动 2026年7月9日 13 分钟

tslib 快速参考卡

tslib 快速参考卡 一、核心概念(5分钟速记) tslib 是什么? 触摸屏事件过滤和校准库 三大功能 数据流 二、…

tslib 快速参考卡

一、核心概念(5分钟速记)

tslib 是什么?

触摸屏事件过滤校准

三大功能

1. 校准 - 触摸坐标 → 屏幕坐标
2. 过滤 - 去噪声、抖动
3. 抽象 - 统一 API

数据流

硬件 → 驱动 → raw模块 → 过滤器 → 应用
       /dev/input  input   median    ts_read
                           dejitter
                           linear

二、快速上手(10分钟)

1. 配置文件 /etc/ts.conf

module_raw input
module median depth=3
module dejitter delta=100
module linear

2. 校准

ts_calibrate

3. 测试

ts_print        # 打印坐标
ts_test_mt      # 图形测试

4. 第一个程序

#include <tslib.h>

int main() {
    struct tsdev *ts = ts_setup(NULL, 0);
    struct ts_sample samp;
    
    while (ts_read(ts, &samp, 1) == 1) {
        printf("X=%d Y=%d P=%d\n", 
               samp.x, samp.y, samp.pressure);
    }
    
    ts_close(ts);
}

编译:gcc app.c -lts


三、核心 API(3分钟)

初始化

struct tsdev *ts = ts_setup(NULL, 0);
// NULL = 自动检测设备
// 0 = 阻塞模式, 1 = 非阻塞

读取单点

struct ts_sample samp;
int ret = ts_read(ts, &samp, 1);
// samp.x, samp.y - 坐标
// samp.pressure - 压力(0=松开)

读取多点

struct ts_sample_mt *samp[1];
samp[0] = calloc(10, sizeof(*samp[0]));
ts_read_mt(ts, samp, 10, 1);

关闭

ts_close(ts);

四、配置文件(5分钟)

常用模块

模块参数作用
inputgrab_events=1读取设备
mediandepth=3中值滤波
dejitterdelta=100去抖动
linearrot=0校准
pthrespmin=1压力阈值
iirN=6 D=10平滑

配置示例

基础配置

module_raw input
module median depth=3
module dejitter delta=100
module linear

高精度配置

module_raw input
module pthres pmin=10
module median depth=5
module iir N=8 D=10
module dejitter delta=50
module linear

旋转屏幕

module_raw input
module linear rot=1  # 顺时针90°

五、环境变量

export TSLIB_TSDEVICE=/dev/input/event0
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_CALIBFILE=/etc/pointercal
export TSLIB_PLUGINDIR=/usr/local/lib/ts
export LD_LIBRARY_PATH=/usr/local/lib

六、常用命令

# 查找设备
ts_finddev

# 打印事件
ts_print              # 过滤后
ts_print --raw        # 原始数据

# 校准
ts_calibrate

# 测试
ts_test_mt

# 编译
gcc app.c -lts

七、故障排除(30秒)

问题解决
ts_setup() = NULLchmod 666 /dev/input/event*
找不到 tslib.hexport C_INCLUDE_PATH=/usr/local/include
找不到 libts.soexport LD_LIBRARY_PATH=/usr/local/lib
坐标不准运行 ts_calibrate
无响应检查 /etc/ts.conf

八、重要文件路径

/etc/ts.conf           - 配置文件
/etc/pointercal        - 校准数据
/dev/input/event*      - 设备文件
/usr/local/lib/libts.so - 库文件
/usr/local/lib/ts/     - 插件目录

九、源码关键文件

src/tslib.h            - API 定义 ⭐⭐⭐⭐⭐
src/ts_setup.c         - 初始化
src/ts_read.c          - 读取
src/ts_config.c        - 配置解析

tests/ts_print.c       - 最简单示例 ⭐⭐⭐⭐⭐
tests/ts_calibrate.c   - 校准工具

plugins/input-raw.c    - 设备读取
plugins/median.c       - 中值滤波
plugins/dejitter.c     - 去抖动
plugins/linear.c       - 校准

十、学习路径

5分钟快速了解

  1. 读本参考卡
  2. 运行 ts_print

30分钟上手

  1. 读”tslib使用手册.md”前3章
  2. 运行 ts_calibrate
  3. 编写第一个程序

2小时深入

  1. 读”tslib学习手册.md”第1-2阶段
  2. 阅读 ts_print.c 源码
  3. 实践练习

8小时精通

  1. 完整读完两份手册
  2. 阅读核心源码
  3. 实现自定义项目

十一、数据结构速查

// 单点触摸
struct ts_sample {
    int x, y;
    unsigned int pressure;  // 0=松开
    struct timeval tv;
};

// 多点触摸
struct ts_sample_mt {
    int x, y;
    unsigned int pressure;
    int slot;           // 触摸点编号
    int tracking_id;    // -1=松开
    short valid;        // TSLIB_MT_VALID
    // ... 更多字段
};

十二、实用代码片段

检测按下/松开

int last_pressure = 0;
while (ts_read(ts, &samp, 1) == 1) {
    if (samp.pressure > 0 && last_pressure == 0)
        printf("按下\n");
    if (samp.pressure == 0 && last_pressure > 0)
        printf("松开\n");
    last_pressure = samp.pressure;
}

检测区域

if (samp.x < 100 && samp.y < 100)
    printf("左上角\n");

非阻塞读取

ts = ts_setup(NULL, 1);  // 非阻塞
int fd = ts_fd(ts);

fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
select(fd+1, &rfds, NULL, NULL, NULL);

打印此卡片,随时查阅!


版本:v1.0 | 日期:2026-07-06 | tslib:1.21

上一篇 Tslib分析与移植汇总

本文章将对tslib触摸屏库进行一些分析、移植与汇总工作

下一篇 tslib 使用手册1

tslib 使用手册 目录 简介 什么是 tslib? tslib 是一个 C 语言编写的触摸屏事件过滤库,主要功能包括...