以下内容源于朱有鹏《物联网大讲堂》课程的学习,如有侵权,请告知删除。
一、input设备应用层编程实践1
1、确定设备文件名
(1)应用层操作驱动有2条路:/dev目录下的设备文件,/sys目录下的属性文件
(2)input子系统用的/dev目录下的设备文件,具体一般都是在 /dev/input/eventn
(3)用cat命令来确认某个设备文件名对应哪个具体设备。
实测的键盘是event1,而鼠标是event3。
2、标准接口打开并读取文件
3、解析struct input_event
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <linux/input.h>#include <string.h>#define DEVICE_KEY"/dev/input/event1"#define DEVICE_MOUSE"/dev/input/event3"int main(void){int fd = -1, ret = -1;struct input_event ev;// 第1步:打开设备文件fd = open(DEVICE_KEY, O_RDONLY);if (fd < 0){perror("open");return -1;}while (1){// 第2步:读取一个event事件包memset(&ev, 0, sizeof(struct input_event));ret = read(fd, &ev, sizeof(struct input_event));if (ret != sizeof(struct input_event)){perror("read");close(fd);return -1;}// 第3步:解析event包,才知道发生了什么样的输入事件printf("%s.\n", (unsigned char *)&ev);}// 第4步:关闭设备close(fd);return 0;}
二、input设备应用层编程实践2
1、解析键盘事件数据、鼠标事件数据
即更换设备文件,第三步换成下面代码,运行查看。
// 第3步:解析event包,才知道发生了什么样的输入事件printf("-------------------------\n");printf("type: %hd\n", ev.type);printf("code: %hd\n", ev.code);printf("value: %d\n", ev.value);printf("\n");
2、事件类型分析(type)