本板运行debian系统,可以直接在板上构建开发环境,直接在板上进行开发。
从现在开始的几篇就以从源码开始,基于C实现,NES游戏机,体验板上直接C开发。
插入USB手柄,可以看到多了/dev/input文件夹,多了event0设备
vi key.c添加如下代码
/* 单独测试:#define KEY_TEST 1
* 作为接口:#define KEY_TEST 0
* 编译 aarch64-linux-gnu-gcc key.c -o key -lpthread
* 运行 chmod +x key
* ./key /dev/input/event8
*/
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define KEY_TEST 1
int s_keys_fd = -1;
uint32_t s_keys_state = 0;
void key_setstate(int code , int vaule, uint32_t* key)
{
if(vaule == 0)
{
switch(code)
{
case 296:
*key &= ~(1u<<3);
break;
case 297:
*key &= ~(1u<<4);
break;
case 288:
*key &= ~(1u<<5);
break;
case 289:
*key &= ~(1u<<8);
break;
case 290:
*key &= ~(1u<<6);
break;
case 291:
*key &= ~(1u<<7);
break;
case 292:
*key &= ~(1u<<1);
break;
case 294:
*key &= ~(1u<<2);
break;
default:
break;
}
}
else
{
switch(code)
{
case 296:
*key |= (1u<<3);
break;
case 297:
*key |= (1u<<4);
break;
case 288:
*key |= (1u<<5);
break;
case 289:
*key |= (1u<<8);
break;
case 290:
*key |= (1u<<6);
break;
case 291:
*key |= (1u<<7);
break;
case 292:
*key |= (1u<<1);
break;
case 294:
*key |= (1u<<2);
break;
default:
break;
}
}
}
/*
* SELECT 296 SELECT 3
* START 297 START 4
* UP 288 右上 5
* RIGHT 289 右右 8
* DOWN 290 右下 6
* LEFT 291 右左 7
* A 292 左前上 1
* B 294 左前下 2
*/
int key_getstate(int key)
{
if(s_keys_state & (1u<<key))
{
return 1;
}
else
{
return 0;
}
}
void* key_poll(void* arg)
{
char ret[2];
struct input_event t;
while(1)
{
if(read(s_keys_fd, &t, sizeof(t)) == sizeof(t))
{
if(t.type==EV_KEY)
{
if(t.value==0 || t.value==1)
{
key_setstate(t.code, t.value, &s_keys_state);
printf("key %d %s\n", t.code, (t.value) ? "Pressed" : "Released");
//if(t.code == KEY_ESC)
// break;
}
}
else
{
///printf("type %d code %d value %d\n", t.type, t.code, t.value);
}
}
usleep(10000);
}
return 0;
}
void key_init(void* arg)
{
pthread_t id;
s_keys_fd = open((char*)arg, O_RDONLY);
if(s_keys_fd <= 0)
{
printf("open %s device error!\n",(char*)arg);
//return 0;
}
/* 创建函数线程,并且指定函数线程要执行的函数 */
int res = pthread_create(&id,NULL,key_poll,arg);
if(res !=0 )
{
printf("pthread_create err\r\n");
}
}
#if KEY_TEST
int main(int argc, char* argv[])
{
key_init(argv[1]);
while(1);
}
#endif
gcc key.c -o key
按不同的按键,打印如下
可以看到直接在板上开发非常方便,免去了交叉编译的繁琐。