irpas技术客

第十八篇,Linux系统IO应用--触摸屏_车水码浓_linux触摸屏事件

未知 3452

一、linux系统IO应用实例。 ? --> ?触摸屏 1、在linux下,一切都是文件。 连触摸屏也是文件,所以触摸屏也有对应的设备文件名。 设备文件名:/dev/input/event0

2、关于触摸屏专业术语。 1)事件。 当一些外接控制设备(鼠标、键盘、WIFI、触摸屏、按键)接入到嵌入式平台(GEC6818)时,这些外接设备的状态发生了改变(鼠标的左键被按下了、触摸屏被滑动了一下、有人连接到wifi模块上),这个动作过程称之为事件。

例如:小明按下鼠标左键并松开了,请问一共发生了几个事件? 2次,按下算一次,松开算一次。

2)输入子系统 当事件发生的时候,就是由输入子系统来计算这个事件中所产生的所有值。

例如:小明点击触摸屏后,小明并不知道自己点击的位置的坐标是多少,但是小明点击了触摸屏,就相当于产生了一个事件,这个事件系统是知道的,当这个事件产生的时候,输入子系统就会去计算小明点击的位置的坐标是多少,然后计算完之后,就会把这个结果保存到系统中。

二、既然我们触摸屏坐标结果是保存在event0这个文件中,那么能不能写一个程序,将这些结果数据读取出来? 1、访问触摸屏设备文件。 2、读取该文件上的数据。 3、将读取出来的数据打印出来。 4、关闭文件。

#include <stdio.h> ? //printf #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> ? //open #include <unistd.h> ?//read close

int main(int argc,char *argv[]) { ?? ?//1. 访问触摸屏设备 ?? ?int fd = open("/dev/input/event0",O_RDONLY); ?? ?if(fd < 0) ?? ??? ?printf("open event0 error!\n"); ?? ? ?? ?//2. 读取设备上的数据 ?? ?char buf[50] = {0}; ?? ?while(1) ?? ?{ ?? ??? ?read(fd,buf,sizeof(buf)); ?? ? ?? ??? ?//3. 打印读取出来的数据 ?? ??? ?printf("buf = %s\n",buf);?? ? ?? ?} ?? ? ?? ?//4. 关闭文件 ?? ?close(fd); ?? ? ?? ?return 0; }

运行结果: 当触摸屏幕时,结果都是乱码。

三、究竟触摸屏数据类型是什么? 如果想知道从文件event0读取出来的数据是什么类型的,首先必须要先知道输入子系统计算完结果之后,这个结果是以什么形式保存到event0中。

1、在linux下,是如何来描述一个事件? ? --> 以结构体形式来描述一个事件。 该结构体已经定义好了,是被定义在一个输入事件头文件中: ? /usr/include/linux/input.h

//事件结构体 ?--> 专门用于描述一个事件的 struct input_event { ?? ?struct timeval time; ?//发生事件的时间 ?? ?__u16 type; ? ? ? ? ? //事件类型 ? ? ? --> ?鼠标发生事件/ ? ? 键盘发生事件 ? ?/触摸屏发生事件? ?? ?__u16 code; ? ? ? ? ? //事件的编码 ? ? --> 左键/右键/滚轮 ? ? ? A键/B键 ? ? ? ?x轴/y轴/压力 ?? ?__s32 value; ? ? ? ? ?//事件的值 ? ? ? --> 专门用于描述状态 };

总结:每当发生一次事件、输入子系统都会返回一个结构体给我们,该结构体就是来描述刚才发生的事件。

2、既然知道输入子系统放进去的是结构体,那么修改代码为:

#include <stdio.h> ? //printf #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> ? //open #include <unistd.h> ?//read close #include <linux/input.h>

int main(int argc,char *argv[]) { ?? ?//1. 访问触摸屏设备 ?? ?int fd = open("/dev/input/event0",O_RDONLY); ?? ?if(fd < 0) ?? ??? ?printf("open event0 error!\n"); ?? ? ?? ?//2. 读取设备上的数据 ?? ?struct input_event buf; ?? ?while(1) ?? ?{ ?? ??? ?read(fd,&buf,sizeof(buf)); ?? ? ?? ??? ?//3. 打印读取出来的数据 ?? ??? ?printf("buf.type = %d\n",buf.type); ?? ??? ?printf("buf.code = %d\n",buf.code); ?? ??? ?printf("buf.value = %d\n",buf.value); ?? ??? ? ?? ??? ?printf("-------------------------------\n"); ?? ?} ?? ? ?? ?//4. 关闭文件 ?? ?close(fd); ?? ? ?? ?return 0; }

运行结果: [root@GEC6818 /]#./ts buf.type = 3 ? ? ? ? ?--> 触摸屏坐标事件 ? ?? buf.code = 0 ? ? ? ? ?--> x轴坐标 buf.value = 505 ? ? ? --> x轴坐标的值 ------------------------------- buf.type = 3 ? ? ? ? ?--> 触摸屏坐标事件 ?? buf.code = 1 ? ? ? ? ?--> y轴坐标 buf.value = 215 ? ? ? --> y轴坐标的值 ------------------------------- buf.type = 1 ? ? ? ? ?--> 按键事件 ? ? buf.code = 330 ? ? ? ?--> 触摸屏压力 ? ? ? ? ? ? ? ? ? ? ? ? ? ? buf.value = 1 ? ? ? ? --> 按下 ------------------------------- buf.type = 1 ? ? ? ? ?--> 按键事件 buf.code = 330 ? ? ? ?--> 触摸屏压力? buf.value = 0 ? ? ? ? --> 松开 -------------------------------

分析结果: 这些结果分析都是在#include "input-event-codes.h"这个头文件中。 分析input-event-codes.h头文件内容:

/* ?* Event types ?*/

#define EV_KEY?? ? ?--> 按键事件(键盘/触摸屏压力)?? ??? ?0x01 #define EV_REL?? ? ?--> 相对位移事件(鼠标)?? ??? ? ? ? ? ?0x02 #define EV_ABS?? ? ?--> 绝对位移事件(触摸屏坐标)?? ??? ?0x03

#define BTN_TOUCH ? --> 触摸屏压力?? ??? ?0x14a ? #define ABS_X?? ? ? ?--> x轴?? ??? ?0x00 #define ABS_Y?? ? ? ?--> y轴?? ??? ?0x01 ?? 四、使用触摸屏知识点,来写一个简单的应用。

? ? 例子: 写一个程序,当我们的手点击触摸屏的松手时,就打印"your hand leave lcd!"

#include <stdio.h> ? //printf #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> ? //open #include <unistd.h> ?//read close #include <linux/input.h> ?//for ts

int main(int argc,char *argv[]) { ?? ?//1. 打开触摸屏设备文件 ?? ?int fd = open("/dev/input/event0",O_RDONLY); ?? ?if(fd < 0) ?? ??? ?printf("open event0 error!\n"); ?? ? ?? ?struct input_event buf; ?? ?while(1) ?? ?{ ?? ??? ?//2. 将文件的数据读取到结构体中 ?? ??? ?read(fd,&buf,sizeof(buf)); ?? ??? ? ?? ??? ?//会发生很多的事件,但是只有松手事件我才要。 ?? ??? ?//3. 判断这些数据 ?? ??? ?//如果以下三个条件都成立,说明发生了松手事件 ?? ??? ?if(buf.type == EV_KEY && buf.code == BTN_TOUCH && buf.value == 0) ?? ??? ?{ ?? ??? ??? ?//4. 做出反应 ?? ??? ??? ?printf("your hand leave lcd!\n"); ?? ??? ?} ?? ?} ?? ? ?? ?//5. 关闭文件。 ?? ?close(fd); ?? ? ?? ?return 0; }

? ? 练习1: 写一个程序,当我们的手在屏幕上滑动的时候,就把手所在的位置的x轴坐标打印出来。

? ? 练习2: 写一个程序,当我们的手在屏幕上滑动的时候,就把手所在的位置的y轴坐标打印出来。

? ? 练习3: 写一个程序,当我们的手在屏幕上滑动的时候,就把手所在的位置的坐标打印出来。

(100 , 200) (100 , 201) (100 , 202) .... ....

? ? ?帮我查两个东西: ? ? ?1、原点在哪里? ? --> 左上方 ? ? ?2、x轴坐标最大值是多少? ?y轴坐标最大值是多少? ?? ??? ?1024 ? ? ? ? ? ? ? ?600

? ? 练习4: 写一个程序,实现当手点击屏幕左边区域松手时,就打印一次"left" ?? ??? ??? ? ? 当手点击屏幕右边区域松手时,就打印一次"right" ? ?--> 是一次,不是很多次。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #linux触摸屏事件 #Linux系统IO触摸屏