捡到h3开发板,做了个视频小车(二),御游追风plus做遥控器
一、前言:现在需要做一个遥控器,来遥控小车。因为之前有买过 30RMB的 御游追风plus手柄,所以打算拿它来试试。
将它插上天嵌h3仅有的一个USB口,发现/dev/input/event0有补枚举出来,来看看输出是什么样子的:
xxd /dev/input/event0
16字节,和usb键盘是一样的。
二:手动检测所有按键,于是得到如下键码图:
三、写了一个程序来检测对不对,要求按下按键就可以输出对应的按键和状态。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>#define TYPE_KEY 0X1
#define TYPE_MISC 0X4
#define TYPE_MOVE 0X3#define CODE_KEY_X 0x134
#define CODE_KEY_B 0x130
#define CODE_KEY_A 0x131
#define CODE_KEY_Y 0x133#define CODE_KEY_UP_DOWN 0x11
#define CODE_KEY_LEFT_RIGHT 0x10#define CODE_KEY_L 0x136
#define CODE_KEY_R 0x137
#define CODE_KEY_ZL 0x138
#define CODE_KEY_ZR 0x139
#define CODE_KEY_VOL_DEC 0x13A //音量减
#define CODE_KEY_VOL_INC 0x13B //音量加
#define CODE_KEY_HOME 0xAC#define CODE_MOVE_LEFT_X 0x0
#define CODE_MOVE_LEFT_Y 0x1
#define CODE_MOVE_RIGHT_X 0x2
#define CODE_MOVE_RIGHT_Y 0x5//小端存储,交换 short 顺序。
struct yuyou_plus{//unsigned short timestamp1;unsigned short timestamp1;unsigned short timestamp2;unsigned short timestamp3;unsigned short timestamp4;unsigned short type;unsigned short code;unsigned short value1;unsigned short value2;
};int main(int argc, char *argv[])
{struct yuyou_plus in_ev = {0};int fd = -1;unsigned char type = 0;unsigned char code = 0;unsigned short val = 0;/* 校验传参 */if (2 != argc) {fprintf(stderr, "usage: %s <input-dev>\n", argv[0]);exit(-1);}/* 打开文件 */if (0 > (fd = open(argv[1], O_RDONLY))) {perror("open error");exit(-1);}for ( ; ; ) {/* 循环读取数据 *///printf ("\nstruct size:%d\n", sizeof(struct yuyou_plus) );//int i = read(fd, &in_ev, sizeof(struct yuyou_plus));//int i = read(fd, &in_ev, 16);//printf ("\n read size:%d \n", i);if (16 != read(fd, &in_ev, 16)) {perror("read error");exit(-1);}switch(in_ev.type){case TYPE_KEY:{ //printf("type:KEY code:%x value:%d\n", in_ev.code, in_ev.value1);switch(in_ev.code){case CODE_KEY_X:{if(in_ev.value1){printf("KEY X pressdown\n");}else{printf("KEY X pressup\n");}}break;case CODE_KEY_B:{if(in_ev.value1){printf("KEY B pressdown\n");}else{printf("KEY B pressup\n");}}break;case CODE_KEY_Y:{if(in_ev.value1){printf("KEY Y pressdown\n");}else{printf("KEY Y pressup\n");}}break;case CODE_KEY_A:{if(in_ev.value1){printf("KEY A pressdown\n");}else{printf("KEY A pressup\n");}}break;case CODE_KEY_ZL:{if(in_ev.value1){printf("KEY ZL pressdown\n");}else{printf("KEY ZL pressup\n");}}break;case CODE_KEY_ZR:{if(in_ev.value1){printf("KEY ZR pressdown\n");}else{printf("KEY ZR pressup\n");}}break;case CODE_KEY_L:{if(in_ev.value1){printf("KEY L pressdown\n");}else{printf("KEY L pressup\n");}}break;case CODE_KEY_R:{if(in_ev.value1){printf("KEY R pressdown\n");}else{printf("KEY R pressup\n");}}break;case CODE_KEY_VOL_DEC:{if(in_ev.value1){printf("KEY VOLUME DEC pressdown\n");}else{printf("KEY VOLUME DEC pressup\n");}}break;case CODE_KEY_VOL_INC:{if(in_ev.value1){printf("KEY VOLUME INC pressdown\n");}else{printf("KEY VOLUME INC pressup\n");}}break;case CODE_KEY_HOME:{if(in_ev.value1){printf("KEY HOME pressdown\n");}else{printf("KEY HOME pressup\n");}}break;default:{}break;}}break;case TYPE_MISC:{//printf("type:MISC code:%d value:%d\n", in_ev.type, in_ev.code, in_ev.value);}break;case TYPE_MOVE:{//printf("type:MOVE code:%x value:%d\n", in_ev.code, in_ev.value1);switch(in_ev.code){case CODE_MOVE_LEFT_X: {printf("MOVE LEFT X (val:%d)\n", in_ev.value1);}break;case CODE_MOVE_LEFT_Y: {printf("MOVE LEFT Y (val:%d)\n", in_ev.value1);}break;case CODE_MOVE_RIGHT_X:{printf("MOVE RIGHT X (val:%d)\n", in_ev.value1);}break;case CODE_MOVE_RIGHT_Y:{printf("MOVE RIGHT Y (val:%d)\n", in_ev.value1);}break;case CODE_KEY_UP_DOWN:{static int flag_up_down = 0; // 0: none 1: up 2: downif(0xffff==in_ev.value1){flag_up_down = 1;printf("KEY DIR UP pressdown\n");}else if(1==in_ev.value1){flag_up_down = 2;printf("KEY DIR DOWN pressdown\n");}else if(0==in_ev.value1){if(1==flag_up_down){printf("KEY DIR UP pressup\n");}else if(2==flag_up_down){printf("KEY DIR DOWN pressup\n");}flag_up_down = 0;}}break;case CODE_KEY_LEFT_RIGHT:{static int flag_left_right = 0; // 0: none 1: left 2: rightif(0xffff==in_ev.value1){flag_left_right = 1;printf("KEY DIR LEFT pressdown\n");}else if(1==in_ev.value1){flag_left_right = 2;printf("KEY DIR RIGHT pressdown\n");}else if(0==in_ev.value1){if(1==flag_left_right){printf("KEY DIR LEFT pressup\n");}else if(2==flag_left_right){printf("KEY DIR RIGHT pressup\n");}flag_left_right = 0;}}break;default:{}break;}}break;default:{printf("type:%x\n", in_ev.type); }break;}}
}
四、使用方式:
1、h3开发板上编译:
gcc read_input.c
2、运行:
./a.out /dev/input/event0