Preface
This board runs the Debian system, and you can build a development environment directly on the board and develop directly on the board.
The next few articles will start from the source code, based on C implementation, NES game console, and direct C development on the experience board.
Input devices
Insert the USB handle, and you can see the /dev/input folder and the event0 device.
Code
vi key.c add the following code
/* Single test: #define KEY_TEST 1
* As an interface: #define KEY_TEST 0
* Compile aarch64-linux-gnu-gcc key.c -o key -lpthread
* Run 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(value == 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 Upper right 5
* RIGHT 289 Right 8
* DOWN 290 Lower right 6
* LEFT 291 right left 7
* A 292 Left front upper 1
* B 294 Left front lower 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;
}
/* Create a function thread and specify the function to be executed by the function thread*/
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
Compile
gcc key.c -o key
test
Press different buttons to print as follows
Summarize
It can be seen that it is very convenient to develop directly on the board, eliminating the tediousness of cross-compilation.