- // main.c
- #include "bmp.h"
- int main()
- {
- get_xy();
- }
-
- // lcd.c
- #include "bmp.h"
- int fd = 0;
- void get_xy ()
- {
- int fd = open ("/dev/input/event0",O_RDWR);
- if(fd == -1 )
- {
- perror("open error\n");
- return ;
- }
- struct input_event ev;
- int x,y;
- while(1)
- {
- int r =read(fd,&ev,sizeof(ev));
- if(r != sizeof(ev))
- {
- perror("read ev error\n");
- return ;
- }
- if(ev.type == EV_ABS && ev.code ==ABS_X)
- {
- x = ev.value ;
- }
- if(ev.type == EV_ABS && ev.code ==ABS_Y)
- {
- y = ev.value ;
- }
- printf("(%d , %d)\n",x,y);
-
- }
- }
-
-
- // bmp.h
- #ifndef __BMP_H__
- #define __BMP_H__
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/mman.h>
- #include <linux/input.h>
- void get_xy ();
- #endif
-
linux的输入子系统:
- 把所有的输入设备 都归结到输入子系统中去。
- 输入设备: 鼠标 键盘 触摸屏
-
- 他有一个专门的结构体来保存这些事件。
- 这个结构体在哪里呢? /dev/input/event0
-
- struct input_event 用来描述一盒输入事件 定义<linux/input.h>
- {
- _u16 type : 表示我这个输入事件的类型
- EV_KEY :按键事件
- EV_REL: 相对事件 : 鼠标事件
- EV_ABS : 绝对事件 : 触摸屏事件
-
- _16 code : code的含义 根据我type的不同 而不同
- if ( type == EV_KEY)
- code == 按键的键值
- KEY_A
- KEY_B
- ...
- #define BTN_TOUCH 0X14A ; //触摸屏按键
-
- if (type ==EV_REL)
- code = 相对坐标轴
- REL_X 相对坐标轴的X轴
- REL_Y 相对坐标轴的Y轴
-
- if (type == EV_ABS)
- code =绝对坐标轴 (触摸屏)
- ABS_X 绝对坐标轴的X轴
- ABS_Y 绝对坐标轴的Y轴
-
- _s32 value: 根据我type的不同 而不同
-
- if(type == EV_KEY)
- {
- if(code ==BTN_TOUCH)
- {
- value = 1 : 说明我这个按键是按下状态
- Value = 0 : 说明我这个按键是松开状态
- }
- }
-
- if(type == EV_ABS )
- {
- if(code == ABS_X)
- value = 绝对坐标轴的X轴的值
- if(code == ABS_Y)
- value = 绝对坐标轴的Y轴的值
- }
-
- }
- 我们的应用 就是不停地去读取我这个 结构体地信息。
-
- 练习: 获取手指在屏幕上点击的坐标
- 第一步: 打开我这个linux输入子系统 open(“/dev/input/event0 ”,。。。)
-
- 第二步: 去读取这个结构体里面的信息。 自定义一个结构体 struct input_event ev
- read(fd,&ev,sizeof(ev)); while(1);
-
- 第三步: 分析我上面讲解地这三项 ,获得触摸地绝对坐标轴值
-
- 第四步: 关闭输入子设备
-