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;
};
测试代码如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <linux/rtc.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <linux/input.h>
#define DEVICE_NAME "Power Button"
/* get the path of the input device */
int get_event_num(char *path_name)
{
char dev_name[32];
char path[32];
int i, fd;
for (i = 0; i < 32; i++) {
sprintf(path, "/dev/input/event%d", i);
if ((fd = open(path, O_RDONLY, 0)) >= 0) {
ioctl(fd, EVIOCGNAME(sizeof(dev_name)), dev_name);
printf("dev_name is %s\n", dev_name);
printf("path is %s\n", path);
}
if (strcmp(dev_name, DEVICE_NAME) == 0) {
printf("find device\n");
strcpy(path_name, path);
break;
}
}
close(fd);
return 0;
}
/* Get key events of input device */
int keyscan(char *path)
{
int ret = -1;
int i = 0;
int key_fd = -1;
struct input_event key_event = {0};
key_fd = open(path, O_RDONLY);
if(key_fd < 0)
{
printf("---open device error!---\n");
return -1;
}
printf("---open device success!---\n");
while(1)
{
ret = lseek(key_fd, 0, SEEK_SET);
ret = read(key_fd, &key_event, sizeof(key_event));
if(ret) {
if(key_event.type == EV_KEY && (key_event.value == 0 || key_event.value == 1)) {
printf("key %d %s\n", key_event.code, (key_event.value) ? "pressed" : "released");
if(key_event.code == KEY_ESC)
break;
}
}
}
close(key_fd);
return ret;
}
int main(int arg, char *arc[])
{
printf("---This is a key event test!---\n");
char path[32];
get_event_num(path);
keyscan(path);
printf("---end!---\n");
return 0;
}
查看具体的ir设备命令如下
cat /proc/bus/input/devices
安卓命令
dumpsys input
显示和input相关的设备,ir是挂在input下面的。
参考:
https://blog.csdn.net/lanmanck/article/details/8423669
https://blog.csdn.net/piaomiaoju/article/details/35288273