QT触摸屏


转载:http://blog.csdn.net/aifei7320/article/details/47975073

这个教程描述了在嵌入式linux下使用Qt如何设置一个支持多点触摸和单点触摸的输入系统。这里假定你已经有了对应的驱动程序,驱动可以从触摸屏的厂商那里获得或者使用一个linux 内核源码中已经存在的驱动。

第一要务,找到你的驱动程序(通常在/drivers/input/touchsreen/*)并且确认你已经定义好了tslib中所需要的每个事件类型。通常你需要EV-SYN, EV-ABS和EV_KEY.我自己使用的驱动中没有定义EV_KEY, 因此驱动不会发送这一个类型的事件。为了能够使tslib和这个驱动的输入系统好好工作,我需要在驱动代码中定义这个事件。

set_bit(EV_SYN, aura.input_dev->evbit);
set_bit(EV_ABS, aura.input_dev->evbit);
set_bit(EV_KEY, aura.input_dev->evbit); # I had to add this line so that tslib was happy

现在使用修改后的驱动源码重新构建内核,并且在开发板上运行起来。
我这里的输入设备名称是‘touchscreen’, 由于多种原因, 你们的输入设备名称可能是event1 或者event0 。
使用下面的命令查看设备

# ls -rlt /dev/input/touchscreen 
lrwxrwxrwx    1 root     root             6 Jan 17 21:06 /dev/input/touchscreen -> event1
# chmod 777 /dev/input/touchscreen 
# chmod 777 /dev/input/event1 

使用下面的命令,你可以看到更多的信息

# cat /sys/devices/virtual/input/input1/uevent 
PRODUCT=0/0/0/0
NAME="aura-touchscreen"
PROP=0
EV=9
ABS=650000 0
MODALIAS=input:b0000v0000p0000e0000-e0,3,kra30,32,35,36,mlsfw

使用下面的命令,然后再屏幕上移动你的手指以确认触摸屏驱动是否正在工作。
当你的手指在屏幕上移动时,你应该看到下面的现象。

# cat /dev/input/touchscreen  | hexdump
0000000 9011 3883 565f 0001 0003 0030 0001 0000
0000010 9011 3883 565f 0001 0003 0032 0001 0000
0000020 9011 3883 565f 0001 0003 0035 04c9 0000
0000030 9011 3883 565f 0001 0003 0036 0c3f 0000
0000040 9011 3883 565f 0001 0000 0002 0000 0000
0000050 9011 3883 565f 0001 0000 0000 0000 0000
0000060 9011 3883 90a9 0001 0003 0030 0001 0000
0000070 9011 3883 90a9 0001 0003 0032 0001 0000

返回你的主工作台, 从这里 https://github.com/kergoth/tslib  下载tslib的源码。
进入tslib的源码目录(cd tslib/plugins)并且编辑input-raw.c 文件。
使用ABS_MT_POSITION_X / Y替换所有的 ABS_X / Y, 这些是多点触摸驱动产生的多点触摸事件变量的名称。
现在剩下的事件已经很明朗了,使用下面的命令 构建tslib并且部署到板子使用的根文件系统中。

sudo ./autogen-clean.sh
sudo ./autogen.sh
sudo export ac_cv_func_malloc_0_nonnull=yes
sudo export PATH=`pwd`:$PATH
sudo ./configure CC=/home/user/toolchain/linaro/bin/arm-linux-gnueabi-gcc
CXX=/home/user/toolchain/linaro/bin/arm-linux-gnueabi-g++
--host=arm-linux
--prefix=/usr/local/tslib
--enable-shared=yes
--enable-static=yes
sudo make
sudo make install
sudo cp /usr/local/tslib/bin/* /home/user/exported-nfs/usr/bin/
sudo cp /usr/local/tslib/etc/ts.conf /home/user/exported-nfs/etc/
sudo cp -r /usr/local/tslib/lib/ts /home/user/exported-nfs/usr/lib
sudo cp /usr/local/tslib/lib/* /home/user/exported-nfs/lib/
sudo vim /home/user/exported-nfs/etc/ts.conf

打开ts.conf 准备编辑,删除'input raw'这一行的注释符。
现在回到嵌入式板子上, 导出下面这些环境变量。

export TSLIB_TSEVENTTYPE=INPUT
export TSLIB_TSDEVICE=/dev/input/touchscreen
export TSLIB_CALIBFILE=/etc/pointercal
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_PLUGINDIR=/usr/lib/ts
export TSLIB_FBDEVICE=/dev/fb0
export TSLIB_CONSOLEDEVICE=none
export TSTS_INFO_FILE=/sys/devices/virtual/input/input1/uevent
export QWS_MOUSE_PROTO=tslib:/dev/input/touchscreen
export PATH=$PATH:/usr/bin

现在我们准备校正,进入目录/usr/bin并且启动校正功能,你应该能成屏幕上看到一个校正图片,要求你点击一个十字。
按照屏幕上说明进行操作,直到校正结束。现在你的屏幕已经校正过了。校正的参数存储在/etc/pointercal 这个文件中。
使用下面的命令可以增加屏幕的亮度  echo 127 > /sys/class/backlight/generic-bl/brightness 。

# ts_calibrate 
xres = 640, yres = 480
Took 5 samples...
Top left : X = 3817 Y = 3912
Took 6 samples...
Top right : X =  269 Y = 3822
Took 5 samples...
Bot right : X =  356 Y =  550
Took 5 samples...
Bot left : X = 3732 Y =  614
Took 6 samples...
Center : X = 2202 Y = 2216
643.068298 -0.155621 -0.000056
491.792572 0.002567 -0.115674
Calibration constants: 42144124 -10198 -3 32230118 168 -7580 65536 

你也可以运行 ts_test 来测试触摸屏的鼠标输入功能。
到目前为止, 使用tslib的单点输入功能,已经可以使用了。
现在我们开始进行多点触摸的功能

下载下面的包,用来在Qt上支持多点触摸事件。

导出你的交叉编译工具和工具链

export CC=/home/user/toolchain/linaro/bin/arm-linux-gnueabi-gcc
export CXX=/home/user/toolchain/linaro/bin/arm-linux-gnueabi-g++

进入liblo的源码目录, 使用下面的命令构建和部署:

cd /home/user/Desktop/QTUIO/liblo-0.26
export SKIP_RMDIR_CHECK=yes
./configure --prefix=/usr/local/lib/liblo --host=arm
make clean
make
sudo make install
cd /usr/local/lib/liblo
sudo cp bin/* /home/user/exported-nfs/usr/bin/
sudo cp lib/liblo.a /home/user/exported-nfs/usr/bin/
sudo cp lib/liblo.la /home/user/exported-nfs/usr/bin/
进入mtdev的源码目录, 使用下面的命令构建和部署:
cd /home/user/Desktop/QTUIO/mtdev-1.1.2
./autogen.sh
./configure --prefix=/usr/local/lib/mtdev --host=arm
make clean
make
sudo make install
cd /usr/local/lib/mtdev
sudo cp bin/* /home/user/exported-nfs/usr/bin/
sudo cp lib/libmtdev.a /home/user/exported-nfs/usr/bin/
sudo cp lib/libmtdev.la /home/user/exported-nfs/usr/bin/

进入mtdev2tuio的源码目录, 使用下面的命令构建和部署:
cd /home/user/Desktop/QTUIO/mtdev2tuio
make clean
make
sudo cp mtdev2tuio /home/user/exported-nfs/usr/bin/

进入qTUIO的源码目录, 使用下面的命令构建和部署:
cd /home/user/Desktop/QTUIO/qTUIO
sudo rm -rf /home/user/Desktop/QTUIO/qTUIO/lib/*
cd src/
/home/user/output/buildroot/host/usr/bin/qmake
make clean
make
cd ../lib
mv libqTUIO_d.so.1.0.0 libqTUIO.so.1.0.0
sudo rm -rf *libqT*_d*so*
sudo ln -sf libqTUIO.so.1.0.0 libqTUIO.so.1.0
sudo ln -sf libqTUIO.so.1.0.0 libqTUIO.so.1
sudo ln -sf libqTUIO.so.1.0.0 libqTUIO.so
sudo mkdir -p /usr/local/lib/qTUIO
sudo cp -r ../lib/* /usr/local/lib/qTUIO
sudo cp -r /usr/local/lib/qTUIO/* /home/user/exported-nfs/usr/lib/


现在,启动服务进程,服务进程会从mtdev接收多点触摸事件,并以TUIO 包的形式转发给Qt。
# ./mtdev2tuio /dev/input/touchscreen osc.udp://127.0.0.1:3333/
Sending OSC/TUIO packets to osc.udp://127.0.0.1:3333/

当然,要保证你已经像下面那样,导出了Qt Mouse 指向的输入环境变量。
export QWS_MOUSE_PROTO=tslib:/dev/input/touchscreen

现在,可以使用QtCreator 或者其他的IDE 构建和部署qUIO examples 目录下的pinchzoom 到板子上。
在板子上(或者在QtCreator中) 像下面那样运行应用程序 

cd /home/test ; export QWS_MOUSE_PROTO=Tslib:/dev/input/touchscreen  ; ./pinchzoom -qws

应该得到类似下面的输出:
Connecting to device...
Killing remote process(es)...
Starting remote process ...
Remote process started.
graphicsview initialized 
listening to TUIO messages on UDP port 3333


应用程序将会显示在你的屏幕上,并且你可以使用你的手指作为鼠标使用及通过手势动作进行缩放的操作。


This tutorial describes how to set up multi-touch and single-touch touchscreen input
for Qt for embedded Linux. I assume that you received a driver from your touchscreen
manufacturer or there is an existing one you can use in the Kernel source.

First things first, locate your driver ( usually /drivers/input/touchscreen/* ), and 
ensure that you have every event type being defined that is required by tslib. So, 
specifically you need EV_SYN, EV_ABS, and EV_KEY. My driver did not define EV_KEY, 
and even though it does not report that event type, I still had to define it in the 
driver source for tslib to work with the input from the driver.

set_bit(EV_SYN, aura.input_dev->evbit);
set_bit(EV_ABS, aura.input_dev->evbit);
set_bit(EV_KEY, aura.input_dev->evbit); # I had to add this line so that tslib was happy

Now build the Kernel with your driver ( either as module or driver ) and fire up your
board.

My input device is called 'touchscreen'. Yours will probably be event1 or event0 depending
on serveral possible reasons.

See what the device is with:

# ls -rlt /dev/input/touchscreen 
lrwxrwxrwx    1 root     root             6 Jan 17 21:06 /dev/input/touchscreen -> event1
# chmod 777 /dev/input/touchscreen 
# chmod 777 /dev/input/event1 


You can see more information with:

# cat /sys/devices/virtual/input/input1/uevent 
PRODUCT=0/0/0/0
NAME="aura-touchscreen"
PROP=0
EV=9
ABS=650000 0
MODALIAS=input:b0000v0000p0000e0000-e0,3,kra30,32,35,36,mlsfw


To ensure that your driver is working do the following command and 
then move your finger around the screen. You should see output here
when you finger is touching the screen.
# cat /dev/input/touchscreen  | hexdump
0000000 9011 3883 565f 0001 0003 0030 0001 0000
0000010 9011 3883 565f 0001 0003 0032 0001 0000
0000020 9011 3883 565f 0001 0003 0035 04c9 0000
0000030 9011 3883 565f 0001 0003 0036 0c3f 0000
0000040 9011 3883 565f 0001 0000 0002 0000 0000
0000050 9011 3883 565f 0001 0000 0000 0000 0000
0000060 9011 3883 90a9 0001 0003 0030 0001 0000
0000070 9011 3883 90a9 0001 0003 0032 0001 0000


Go back to your host machine and download the tslib src from here 
--> https://github.com/kergoth/tslib.

Enter the tslib source directory ( cd tslib/plugins ) and edit the input-raw.c file.
Replace any occurences of ABS_X / Y with ABS_MT_POSITION_X / Y. These are the names
of the multi-touch event type variables produces by a multitouch driver.

Now that everything is in order, build tslib and deploy using the following commands
to your exported nfs ( root file system ) for the board:
sudo ./autogen-clean.sh
sudo ./autogen.sh
sudo export ac_cv_func_malloc_0_nonnull=yes
sudo export PATH=`pwd`:$PATH
sudo ./configure CC=/home/user/toolchain/linaro/bin/arm-linux-gnueabi-gcc
CXX=/home/user/toolchain/linaro/bin/arm-linux-gnueabi-g++
--host=arm-linux
--prefix=/usr/local/tslib
--enable-shared=yes
--enable-static=yes
sudo make
sudo make install
sudo cp /usr/local/tslib/bin/* /home/user/exported-nfs/usr/bin/
sudo cp /usr/local/tslib/etc/ts.conf /home/user/exported-nfs/etc/
sudo cp -r /usr/local/tslib/lib/ts /home/user/exported-nfs/usr/lib
sudo cp /usr/local/tslib/lib/* /home/user/exported-nfs/lib/
sudo vim /home/user/exported-nfs/etc/ts.conf


When ts.conf opens for editing, uncomment the 'input raw' line ( the very first 
commented out module ).

Now log back into your embedded machine and export the following environment
variables:

export TSLIB_TSEVENTTYPE=INPUT
export TSLIB_TSDEVICE=/dev/input/touchscreen
export TSLIB_CALIBFILE=/etc/pointercal
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_PLUGINDIR=/usr/lib/ts
export TSLIB_FBDEVICE=/dev/fb0
export TSLIB_CONSOLEDEVICE=none
export TSTS_INFO_FILE=/sys/devices/virtual/input/input1/uevent
export QWS_MOUSE_PROTO=tslib:/dev/input/touchscreen
export PATH=$PATH:/usr/bin


Now we are ready to calibrate. Go to /usr/bin and launch the calibration
utility. You should now see a calibration image on the screen requesting
you to touch a few crosshairs. Go ahead and do that until it stops. Then
your screen is calibrated! The calibration parameters are stored in the
/etc/pointercal file. (you may have to first increase the brightness
of your screen with --> echo 127 > /sys/class/backlight/generic-bl/brightness)


# ts_calibrate 
xres = 640, yres = 480
Took 5 samples...
Top left : X = 3817 Y = 3912
Took 6 samples...
Top right : X =  269 Y = 3822
Took 5 samples...
Bot right : X =  356 Y =  550
Took 5 samples...
Bot left : X = 3732 Y =  614
Took 6 samples...
Center : X = 2202 Y = 2216
643.068298 -0.155621 -0.000056
491.792572 0.002567 -0.115674
Calibration constants: 42144124 -10198 -3 32230118 168 -7580 65536 


You can also test the touchscreen mouse input by running:

ts_test

Great! Now you have single mouse input with tslib ready to go!

Now let's move on to multi-touch.

Download the following packages to enable multitouch events with Qt.

https://github.com/x29a/qTUIO 
https://github.com/olivopaolo/mtdev2tuio 
http://bitmath.org/code/mtdev/ 
http://liblo.sourceforge.net/ 

Export your cross-compiler and toolchain:

export CC=/home/user/toolchain/linaro/bin/arm-linux-gnueabi-gcc
export CXX=/home/user/toolchain/linaro/bin/arm-linux-gnueabi-g++


Enter the liblo source code directory and build and deploy with:


cd /home/user/Desktop/QTUIO/liblo-0.26
export SKIP_RMDIR_CHECK=yes
./configure --prefix=/usr/local/lib/liblo --host=arm
make clean
make
sudo make install
cd /usr/local/lib/liblo
sudo cp bin/* /home/user/exported-nfs/usr/bin/
sudo cp lib/liblo.a /home/user/exported-nfs/usr/bin/
sudo cp lib/liblo.la /home/user/exported-nfs/usr/bin/

Enter the mtdev source code directory and build and deploy with:

cd /home/user/Desktop/QTUIO/mtdev-1.1.2
./autogen.sh
./configure --prefix=/usr/local/lib/mtdev --host=arm
make clean
make
sudo make install
cd /usr/local/lib/mtdev
sudo cp bin/* /home/user/exported-nfs/usr/bin/
sudo cp lib/libmtdev.a /home/user/exported-nfs/usr/bin/
sudo cp lib/libmtdev.la /home/user/exported-nfs/usr/bin/


Enter the mtdev2tuio bridge source code directory and build and deploy with:

cd /home/user/Desktop/QTUIO/mtdev2tuio
make clean
make
sudo cp mtdev2tuio /home/user/exported-nfs/usr/bin/


Enter the qTUIO source directory and build and deploy with:

cd /home/user/Desktop/QTUIO/qTUIO
sudo rm -rf /home/user/Desktop/QTUIO/qTUIO/lib/*
cd src/
/home/user/output/buildroot/host/usr/bin/qmake
make clean
make
cd ../lib
mv libqTUIO_d.so.1.0.0 libqTUIO.so.1.0.0
sudo rm -rf *libqT*_d*so*
sudo ln -sf libqTUIO.so.1.0.0 libqTUIO.so.1.0
sudo ln -sf libqTUIO.so.1.0.0 libqTUIO.so.1
sudo ln -sf libqTUIO.so.1.0.0 libqTUIO.so
sudo mkdir -p /usr/local/lib/qTUIO
sudo cp -r ../lib/* /usr/local/lib/qTUIO
sudo cp -r /usr/local/lib/qTUIO/* /home/user/exported-nfs/usr/lib/


Now kick off the server that will take multi-touch events input from
mtdev and feed that as TUIO packets to Qt:

# ./mtdev2tuio /dev/input/touchscreen osc.udp://127.0.0.1:3333/
Sending OSC/TUIO packets to osc.udp://127.0.0.1:3333/


Also, ensure that you exported your Qt mouse pointer input environment
variable as follows:

export QWS_MOUSE_PROTO=tslib:/dev/input/touchscreen

Now use Qt Creator or some other IDE to build and deploy the pinchzoom
example found in the qTUIO examples directory to the board.

On the board ( or from Qt Creator ) run the application like:

cd /home/test ; export QWS_MOUSE_PROTO=Tslib:/dev/input/touchscreen  ; ./pinchzoom -qws

Output should look like:

Connecting to device...
Killing remote process(es)...
Starting remote process ...
Remote process started.
graphicsview initialized 
listening to TUIO messages on UDP port 3333


The application will launch on your screen and you should be able to use your finger
as a mouse but also pinch and zoom ( gestures ) at the same time!

Happy coding :)

Feel free to leave feedback - happy coding


  
xilinx:

sudo apt-get install automake
sudo apt-get install autogen
sudo apt-get install autoconf

sudo apt-get install libtool
 
sudo ./configure CC=/home/david/work/zynq/CodeSourcery-master/bin/arm-xilinx-linux-gnueabi-gcc CXX=/home/david/work/zynq/CodeSourcery- master/bin/arm-xilinx-linux-gnueabi-g++ --host=arm-linux --prefix=/home/david/QT5/lib  ac_cv_func_malloc_0_nonnull=yes --enable-shared=yes --enable-static=yes


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值