linux ir驱动学习笔记
ir(infrared remote)红外协议协议主要有REC、RC5/RC6、SONY等。
代码路径:/drivers/media/rc

keymaps是一些rc_map_table的按键映射,每个遥控器都不一样。
ir模块整体框架如下

进行设备读取信息操作时,对象是/dev/input/eventX,原因是ir属于input子系统。
编译配置
make menuconfig
选择如下,选择编码方式和具体的驱动设备。

event事件结构如下
struct input_event { struct timeval time; __u16 type; __u16 code; __s32 value; };
测试代码如下
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/ioctl.h> 5 #include <sys/time.h> 6 #include <sys/types.h> 7 #include <linux/rtc.h> 8 #include <fcntl.h> 9 #include <unistd.h> 10 #include <errno.h> 11 #include <time.h> 12 #include <linux/input.h> 13 14 #define DEVICE_NAME "Power Button" 15 16 /* get the path of the input device */ 17 int get_event_num(char *path_name) 18 { 19 char dev_name[32]; 20 char path[32]; 21 int i, fd; 22 23 for (i = 0; i < 32; i++) { 24 sprintf(path, "/dev/input/event%d", i); 25 if ((fd = open(path, O_RDONLY, 0)) >= 0) { 26 ioctl(fd, EVIOCGNAME(sizeof(dev_name)), dev_name); 27 printf("dev_name is %s\n", dev_name); 28 printf("path is %s\n", path); 29 } 30 if (strcmp(dev_name, DEVICE_NAME) == 0) { 31 printf("find device\n"); 32 strcpy(path_name, path); 33 break; 34 } 35 } 36 close(fd); 37 38 return 0; 39 } 40 41 /* Get key events of input device */ 42 int keyscan(char *path) 43 { 44 int ret = -1; 45 int i = 0; 46 int key_fd = -1; 47 struct input_event key_event = {0}; 48 49 key_fd = open(path, O_RDONLY); 50 if(key_fd < 0) 51 { 52 printf("---open device error!---\n"); 53 return -1; 54 } 55 printf("---open device success!---\n"); 56 57 while(1) 58 { 59 ret = lseek(key_fd, 0, SEEK_SET); 60 ret = read(key_fd, &key_event, sizeof(key_event)); 61 62 if(ret) { 63 if(key_event.type == EV_KEY && (key_event.value == 0 || key_event.value == 1)) { 64 printf("key %d %s\n", key_event.code, (key_event.value) ? "pressed" : "released"); 65 if(key_event.code == KEY_ESC) 66 break; 67 } 68 } 69 } 70 close(key_fd); 71 return ret; 72 } 73 74 int main(int arg, char *arc[]) 75 { 76 printf("---This is a key event test!---\n"); 77 char path[32]; 78 get_event_num(path); 79 keyscan(path); 80 printf("---end!---\n"); 81 82 return 0; 83 }
查看具体的ir设备命令如下
cat /proc/bus/input/devices

安卓命令
dumpsys input
参考:
https://blog.csdn.net/lanmanck/article/details/8423669
https://blog.csdn.net/piaomiaoju/article/details/35288273

浙公网安备 33010602011771号