红外触摸框触摸时间度量
1、 查看红外触摸框设备
红外框触摸使用了input子系统为上层应用提供了统一的接口,我们使用getevent命令查看所有的input设备,红色图标指向的/dev/input/event4就是红外触摸框设备文件,设备文件描述名为MTOUC Touch Computer INC。
2、 查看红外设备触摸信息
查看红外触摸信息之前得先了解下input_event结构体:
1 struct input_event {
2 struct timeval time;
3 __u16 type;
4 __u16 code;
5 __s32 value;
6 };
time表示事件发生那一刻的时间。
type表示事件类型
code 根据type不同而表示意思不同,当type为触摸事件时EV_ABS
value 根据type以及code的不同而不同,当type为EV_KEY按键事件时,code为按键键值,value为按下还是抬起。
使用命令 getevent /dev/input/event4,然后触摸以下触摸屏,在终端中打印信息如下图所示:
第一列:触摸类型input_event.type,在linux/include/uapi/linux/input.h里面有:
表示触摸类型,03代表绝对坐标类型 01代表按键事件。
1 /*
2 * Event types
3 */
4
5 #define EV_SYN 0x00
6 #define EV_KEY 0x01
7 #define EV_REL 0x02
8 #define EV_ABS 0x03
9 #define EV_MSC 0x04
10 #define EV_SW 0x05
11 #define EV_LED 0x11
12 #define EV_SND 0x12
13 #define EV_REP 0x14
14 #define EV_FF 0x15
15 #define EV_PWR 0x16
16 #define EV_FF_STATUS 0x17
17 #define EV_MAX 0x1f
18 #define EV_CNT (EV_MAX+1)
第二列:input_event.code的值,在这红外触摸类型下表示X或者Y的绝对坐标。
第三列:在红外触摸下表示X或者Y坐标的值。
3、 查看点击触摸屏以下:getevent -l /dev/input/event4
和没加 “-l”选项没什么差别,只是这里用宏替代了数字,更清晰每一个事件是什么意思。
触摸按以下所触发的事件类型实践类型的按键值,现在我们仔细分析下。
根据事件type分析,此次触摸事件触发了EV_ABS、EV_KEY、EV_SYN等三类事件,这三类分别是绝对坐标、按键、同步等。红外触摸支持该三类事件。该事件类型实在驱动编写时指定的。
我们现在需要计算触摸事件所持续的时间长短,所以此时重点关注的是触摸的EV_KEY事件,该事件表示触摸按下或抬起。捕获按下的时间点,再捕获抬起的时间 点,将时间相减就得到了触摸移动的时间长短。好在input_event提供的time给结构体,没有time结构体也可一计算,此处不做过多演示。具体 代码如下。
1 /*************************************************************************
2 > File Name: testInput.c
3 > Author: winfu
4 > Mail: wenfuandyou@163.com
5 > Created Time: Mon 30 May 2016 04:40:40 PM CST
6 ************************************************************************/
7
8 #include<stdio.h>
9 #include <time.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <linux/input.h> //input_event,标准键值等
16 #include <stdbool.h>
17
18 #define DOWN 1
19 #define UP 0
20
21 int main(int argc, char *argv[])
22 {
23 int fd;
24 long int num = 0;
25 bool flagTime = false;
26 struct timeval tv_start,tv_end; // 记录实际触摸按下和抬起的时间
27
28 struct input_event inputV;
29 fd = open(argv[1], O_RDWR); //./testInput /dev/input/event4
30 if (fd < 0) {
31 printf("open event failed.\n");
32 return -1;
33 }
34
35 while(1) {
36
37 read(fd, &inputV, sizeof(inputV));
38
39
40 // printf("\033[31m type = %#x, code = %#x, value = %#x \033[0m \n",
41 // inputV.type, inputV.code, inputV.value);
42
43 //if (EV_ABS == inputV.type) //读取按键内容
44 //{
45 // printf("\033[1;32;40m event=%s,value=%d \033[0m \r\n",
46 // inputV.code == ABS_X ? "ABS_X":
47 // inputV.code == ABS_Y ? "ABS_Y":
48 // inputV.code == ABS_PRESSURE ? "ABS_PRESSURE" : "ABS_DISTANCE",inputV.value);
49 //}
50
51 if(inputV.type == EV_KEY && inputV.code == BTN_TOUCH && inputV.value == DOWN) //按下
52 {
53 flagTime = true;
54 tv_start = inputV.time;
55 }
56 if(flagTime && inputV.type == EV_KEY && inputV.code == BTN_TOUCH && inputV.value == UP) //抬起
57 {
58 tv_end = inputV.time;
59
60 printf("-------------------------> \e[0;31m %d times ,run time : %lf ms \033[0m <-------------------\n",num++
61 , (tv_end.tv_sec - tv_start.tv_sec)*1000.0 /*将秒转换为毫秒*/
62 + (tv_end.tv_usec - tv_start.tv_usec)/1000.0 );/*将微妙转换为毫秒*/
63 flagTime = false;
64 }
65 num=num%50000;
66 }
67 close(fd);
68 return 0;
69 }
测试运行结果如下:在屏幕上写一个“大”字
写“大”字用了3笔,每一笔都有显示时间。