文章目录
采用触摸屏的移动终端中,触摸屏性能的调试是一个重要问题之一,因为电磁噪声的缘故,触摸屏容易存在点击不准确,有抖动等问题。
tslib是一个开源程序,能够为触摸屏驱动获得的采样提供诸如滤波、去抖动、校准等功能,通常作为触摸屏驱动的适配层,为上层的应用提供了一个统一的接口。
要想 Qt 支持触摸还需要编译 tslib,以生成触摸相关插件。Tslib 一般在嵌入式系统时用的多,基本的触摸插件都是它。当然还有 xinput 这样的插件,那是用于桌面系统的。嵌入式 Qt一般用的都是 Tslib。
第一步:获取tslib的源码
使用git获取tslib源码:
推荐:第三方 tslib-1.4:
sudo git clone https://github.com/15903016222/tslib.git
cd tslib
官方最新版:
sudo git clone https://github.com/libts/tslib.git
第二步:编译,安装tslib-1.4
注意:在编译tslib之前确认自己的电脑上已经安装了automake autoconf libtool libsysfs-dev软件
sudo apt-get install automake autoconf libtool libsysfs-dev
开始编译,安装
- clean源码
sudo ./autogen-clean.sh
liefyuan@ubuntu:/home/tslib$ sudo ./autogen-clean.sh
Removing autogenned files...
Done.
- 自动配置
sudo ./autogen.sh
liefyuan@ubuntu:/home/tslib$ ./autogen.sh
autom4te: cannot create autom4te.cache: No such file or directory
aclocal: error: echo failed with exit status: 1
autoreconf: aclocal failed with exit status: 1
liefyuan@ubuntu:/home/tslib$ sudo ./autogen.sh
libtoolize: putting auxiliary files in '.'.
libtoolize: copying file './ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4/internal'.
libtoolize: copying file 'm4/internal/libtool.m4'
libtoolize: copying file 'm4/internal/ltoptions.m4'
libtoolize: copying file 'm4/internal/ltsugar.m4'
libtoolize: copying file 'm4/internal/ltversion.m4'
libtoolize: copying file 'm4/internal/lt~obsolete.m4'
configure.ac:58: installing './compile'
configure.ac:24: installing './config.guess'
configure.ac:24: installing './config.sub'
configure.ac:7: installing './install-sh'
configure.ac:7: installing './missing'
Makefile.am: installing './INSTALL'
plugins/Makefile.am: installing './depcomp'
注意:如果没有安装automake autoconf,则这一步会报以下错误
如果没有安装libtool libsysfs-dev,则会报以下错误
ws@ws:~/project/tslib$ ./autogen.sh
configure.ac:24: error: possibly undefined macro: AC_DISABLE_STATIC
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
configure.ac:25: error: possibly undefined macro: AC_ENABLE_SHARED
configure.ac:26: error: possibly undefined macro: AC_LIBTOOL_DLOPEN
configure.ac:27: error: possibly undefined macro: AC_PROG_LIBTOOL
autoreconf: /usr/bin/autoconf failed with exit status: 1
configure.ac:17: error: possibly undefined macro: AC_PROG_LIBTOOL
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1
- 为configure做准备, 防止编译时出错
echo "ac_cv_func_malloc_0_nonnull=yes" > tmp.cache
失败!
liefyuan@ubuntu:/home/tslib$ sudo echo "ac_cv_func_malloc_0_nonnull=yes" > tmp.cache
bash: tmp.cache: 权限不够
- 配置
sudo ./configure --host=arm-linux --cache-file=tmp.cache --prefix=/opt/tslib CC=/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
-
–prefix=/opt/tslib // 安装路径 (注意一定要写自己的安装路径)
-
CC=/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gcc // 指定编译器的绝对路径(注意:必须是绝对路径,否则报错) (注意一定是你交叉编译器的arm-linux-gcc的绝对安装路径)
liefyuan@ubuntu:/home/tslib$ sudo ./configure --host=arm-linux --cache-file=tmp.cache --prefix=/opt/tslib CC=/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
configure: creating cache tmp.cache
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for arm-linux-strip... no
checking for strip... strip
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
...
config.status: creating etc/Makefile
config.status: creating plugins/Makefile
config.status: creating src/Makefile
config.status: creating tests/Makefile
config.status: creating tools/Makefile
config.status: creating doc/Makefile
config.status: creating tslib.pc
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands
- 编译 安装
sudo make -j16
如果你的安装路径需要权限,可以使用下边的命令
sudo make install
然后再目录/opt/
目录下就有了安装好的tslib文件夹了。
liefyuan@ubuntu:/opt/tslib$ ls
bin etc include lib share
查看编译的 tslib 文件类型,使用 file 指令。编译出来的 tslib 应为 ARM 格式,不能为 X86格式,如果是 X86 格式就不能用于 ARM 开发板上了。那么说明您上一章的 ARM 交叉编译器环境没生效。请删除重新编译!
liefyuan@ubuntu:/opt/tslib$ file bin/ts_calibrate
bin/ts_calibrate: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.31, BuildID[sha1]=b8d6bb074473c346ed75079e58e5cc36a328b2e1, not stripped
第三步:配置,测试移植的tslib
移植配置tslib
将安装路径下的整个tslib文件夹,下载至开发开发板上,我存放的路径为/opt/tslib
还需要在开发上配置tslib的环境变量:vi /etc/profile
$ vi /etc/profile
export TSLIB_ROOT=/opt/tslib
export TSLIB_TSDEVICE=/dev/input/event1
export QWS_MOUSE_PROTO=tslib:/dev/input/event1
export TSLIB_CALIBFILE=/etc/pointercal
export TSLIB_CONFFILE=$TSLIB_ROOT/etc/ts.conf
export TSLIB_PLUGINDIR=$TSLIB_ROOT/lib/ts
export TSLIB_FBDEVICE=/dev/fb0
export TSLIB_CONSOLEDEVICE=none
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TSLIB_ROOT/lib
其中,TSLIB_ROOT更换为自己实际存放的路径;
另外,TSLIB_TSDEVICE 和QWS_MOUSE_PROTO这两项需要查看自己的板子的触摸屏设备对应/dev/input/下那个文件
测试tslib
配置一下环境变量:
source /etc/profile
运行一下测试程序:
/opt/tslib/bin/ts_test // 运行 ts_test 测试触摸是否正常,点击界面的 Draw 测试
/opt/tslib/bin/ts_test_mt // 运行 ts_test 测试触摸是否正常,点击界面的 Draw 测试
测试一下屏幕
cat /dev/urandom > /dev/fb0
cat /dev/zero > /dev/fb0
1、使用第一条命令可以让屏幕出现雪花
2、使用第二条命令可以让屏幕变黑
说明屏幕移植是成功
应用说明
第一次上电触摸没有反应
测试有原始数据打印,一般需要运行./ts_calibrate
来校准一下触摸屏,才能使用的。下次上电就不用校准了,直接用了。
Tslib ts_calibrate校准原理
Tslib 是触摸屏驱动和应用层之间的适配层,它从触摸屏驱动处获得原始的设备坐标数据,通过一系列的去噪、去抖、坐标变换等操作,来去除噪声并将原始的设备坐标转换为相应的屏幕坐标。tslib为应用层提供了2个主要的接口ts_open(),ts_close();ts_read()和ts_read_raw(),其中ts_read()读取校正后的相对坐标数据,ts_read_raw()读取校正前的实际坐标。
从tslib默认的ts.conf文件中可以看出,tslib 从触摸屏驱动采样到的设备坐标进行处理再提供给应用端的过程大体如下:
raw device --> variance --> dejitter --> linear --> application
module module module module
校准情况下,tslib对驱动采样到的数据进行处理的一般过程如下:
1.读取屏上5个点的坐标(Top Left,Top Right,Bottom Left,Bottom Right,Center),在进行一系列的变换,取样的5个点,实际上是包含3个不同的X值,3个不同的Y值。和scaling 值一共7个值,一起保存到/etc/pointercal中.
2.这个/etc/pointercal文件主要是供linear插件使用。而我们每次的触摸的操作都进行多次触摸坐标变换。
反应太灵敏了,调ts.conf的参数
Tslib 的配置文件ts.conf 同样是个十分重要的部分,在ts.conf 中配置了需要加载的插件、插件加载顺序以及插件的一些约束参数,这些配置参数对触摸屏的触摸效果具有十分重要的影响。
其中:pthres 为Tslib 提供的触摸屏灵敏度门槛插件 默认参数为pmin=1;
variance 为Tslib提供的触摸屏滤波算法插件 默认参数为delta=30;
dejitter 为Tslib 提供的触摸屏去噪算法插件 默认参数为delta=100;
linear为Tslib 提供的触摸屏坐标变换插件。
由于各种因素的影响,在不同的硬件平台上,相关参数可能需要调整。以上参数的相互关系为:采样间隔越大,采样点越少,采样越失真,但因为信息量少,容易出现丢笔划等丢失信息情况,但表现出来的图形效果将会越好;去噪算法跟采样间隔应密切互动,采样间隔越大,去噪约束应越小,反之采样间隔越小,去噪约束应越大。去抖算法为相对独立的部分,去抖算法越复杂,带来的计算量将会变大,系统负载将会变重,但良好的去抖算法可以更好的去除抖动,在进行图形绘制时将会得到更好的效果;灵敏度和ts 门槛值为触摸屏的灵敏指标,一般不需要进行变动,参考参考值即可。
源文件:
# Uncomment if you wish to use the one-wire linux input layer S70/A70...
# module_raw one_wire_ts_input
# Uncomment if you wish to use the linux input layer event interface
module_raw input
# Uncomment if you're using a Sharp Zaurus SL-5500/SL-5000d
# module_raw collie
# Uncomment if you're using a Sharp Zaurus SL-C700/C750/C760/C860
# module_raw corgi
# Uncomment if you're using a device with a UCB1200/1300/1400 TS interface
# module_raw ucb1x00
# Uncomment if you're using an HP iPaq h3600 or similar
# module_raw h3600
# Uncomment if you're using a Hitachi Webpad
# module_raw mk712
# Uncomment if you're using an IBM Arctic II
# module_raw arctic2
module pthres pmin=1
module variance delta=30
module dejitter delta=100
module linear
我一般把variance调低一点就没那么灵敏了。
报错总结:
tslib: Selected device is not a touchscreen (must support ABS and KEY event types)解决方法
两种情况:一种内核未加入触摸屏的支持,另一种情况是将TSLIB_TSDEVICE=/dev/input/eventN
,改为 TSLIB_TSDEVICE=/dev/input/eventX
(N:原来的event号,X:相应的event号)