981 views|1 replies

501

Posts

4

Resources
The OP
 

[VisionFive 2: Integrated 3D GPU RISC-V single-board computer] Build an NES game console - USB controller button collection [Copy link]

 

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.

This post is from Domestic Chip Exchange

Latest reply

Good sharing, learn the ideas of the residents in the building about developing and writing programs.  Details Published on 2023-7-21 09:45
 
 

1462

Posts

0

Resources
2
 
Good sharing, learn the ideas of the residents in the building about developing and writing programs.
This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list