|
前言 前面我们已经按计划完成了性能测试,主要接口的测试。现在开始我们完成一些实际的有意思的项目,按照计划第一个是掌上游戏机的实现。 从现在开始我们一步一步来移植NES游戏模拟器,制作一个掌上游戏机。移植NES游戏模拟器主要需要实现按键输入,显示,音频输出三个部分。今天实现第一个部分按键输入。
USB输入设备
USB输入设备连接连接USB手柄之前查看输入设备 root@g2uliot:~# ls /dev/input/ by-path event0 touchscreen0 连接USB手柄到USB,串口打印如下信息
root@g2uliot:~# [ 1904.533449] usb 1-1.2: new low-speed USB device number 5 using ehci-platform
[ 1904.782050] input: USB Gamepad as /devices/platform/soc/11c70100.usb/usb1/1-1/1-1.2/1-1.2:1.0/0003:0810:0001.0002/input/input2
[ 1904.814048] hid-generic 0003:0810:0001.0002: input: USB HID v1.10 Joystick [ USB Gamepad ] on usb-11c70100.usb-1.2/input0
查看输入设备多了event1和js0 by-id root@g2uliot:~# ls /dev/input/ by-id by-path event0 event1 js0 touchscreen0 root@g2uliot:~# cat /proc/bus/input/devices查看设备信息
root@g2uliot:~# cat /proc/bus/input/devices
I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="ADS7846 Touchscreen"
P: Phys=spi1.0/input0
S: Sysfs=/devices/platform/soc/1004ac00.spi/spi_master/spi1/spi1.0/input/input0
U: Uniq=
H: Handlers=event0
B: PROP=0
B: EV=b
B: KEY=400 0 0 0 0 0 0 0 0 0 0
B: ABS=1000003
I: Bus=0003 Vendor=0810 Product=0001 Version=0110
N: Name=" USB Gamepad "
P: Phys=usb-11c70100.usb-1.2/input0
S: Sysfs=/devices/platform/soc/11c70100.usb/usb1/1-1/1-1.2/1-1.2:1.0/0003:0810:0001.0002/input/input2
U: Uniq=
H: Handlers=js0 event1
B: PROP=0
B: EV=1b
B: KEY=fff 0 0 0 0 0 0 0 0 0
B: ABS=30027
B: MSC=10
root@g2uliot:~#
输入设备测试cat /dev/input/event1 | hexdump -C 按手柄的不同按键 打印如下
root@g2uliot:~# cat /dev/input/event1 | hexdump -C
00000000 8c 3b 67 5f 8c 55 05 00 04 00 04 00 03 00 09 00 |.;g_.U..........|
00000010 8c 3b 67 5f 8c 55 05 00 01 00 22 01 01 00 00 00 |.;g_.U....".....|
00000020 8c 3b 67 5f 8c 55 05 00 00 00 00 00 00 00 00 00 |.;g_.U..........|
00000030 8c 3b 67 5f 8a 3d 09 00 04 00 04 00 03 00 09 00 |.;g_.=..........|
00000040 8c 3b 67 5f 8a 3d 09 00 01 00 22 01 00 00 00 00 |.;g_.=....".....|
00000050 8c 3b 67 5f 8a 3d 09 00 00 00 00 00 00 00 00 00 |.;g_.=..........|
00000060 8d 3b 67 5f 07 f9 09 00 03 00 00 00 00 00 00 00 |.;g_............|
00000070 8d 3b 67 5f 07 f9 09 00 00 00 00 00 00 00 00 00 |.;g_............|
00000080 8d 3b 67 5f 4b 89 0c 00 03 00 00 00 7f 00 00 00 |.;g_K...........|
00000090 8d 3b 67 5f 4b 89 0c 00 00 00 00 00 00 00 00 00 |.;g_K...........|
000000a0 8e 3b 67 5f c8 0f 01 00 03 00 01 00 00 00 00 00 |.;g_............|
000000b0 8e 3b 67 5f c8 0f 01 00 00 00 00 00 00 00 00 00 |.;g_............|
000000c0 8e 3b 67 5f c9 03 03 00 03 00 01 00 7f 00 00 00 |.;g_............|
000000d0 8e 3b 67 5f c9 03 03 00 00 00 00 00 00 00 00 00 |.;g_............|
000000e0 8e 3b 67 5f 07 94 05 00 03 00 00 00 ff 00 00 00 |.;g_............|
000000f0 8e 3b 67 5f 07 94 05 00 00 00 00 00 00 00 00 00 |.;g_............|
00000100 8e 3b 67 5f 08 88 07 00 03 00 00 00 7f 00 00 00 |.;g_............|
00000110 8e 3b 67 5f 08 88 07 00 00 00 00 00 00 00 00 00 |.;g_............|
00000120 8e 3b 67 5f cb 50 0b 00 03 00 01 00 ff 00 00 00 |.;g_.P..........|
00000130 8e 3b 67 5f cb 50 0b 00 00 00 00 00 00 00 00 00 |.;g_.P..........|
00000140 8e 3b 67 5f c7 44 0d 00 03 00 01 00 7f 00 00 00 |.;g_.D..........|
00000150 8e 3b 67 5f c7 44 0d 00 00 00 00 00 00 00 00 00 |.;g_.D..........|
00000160 8f 3b 67 5f 4a 9b 09 00 03 00 00 00 ff 00 00 00 |.;g_J...........|
00000170 8f 3b 67 5f 4a 9b 09 00 00 00 00 00 00 00 00 00 |.;g_J...........|
00000180 8f 3b 67 5f 8a 2b 0c 00 03 00 00 00 7f 00 00 00 |.;g_.+..........|
00000190 8f 3b 67 5f 8a 2b 0c 00 00 00 00 00 00 00 00 00 |.;g_.+..........|
000001a0 90 3b 67 5f d0 15 00 00 04 00 04 00 09 00 09 00 |.;g_............|
000001b0 90 3b 67 5f d0 15 00 00 01 00 28 01 01 00 00 00 |.;g_......(.....|
000001c0 90 3b 67 5f d0 15 00 00 00 00 00 00 00 00 00 00 |.;g_............|
000001d0 90 3b 67 5f 89 ea 01 00 04 00 04 00 09 00 09 00 |.;g_............|
000001e0 90 3b 67 5f 89 ea 01 00 01 00 28 01 00 00 00 00 |.;g_......(.....|
000001f0 90 3b 67 5f 89 ea 01 00 00 00 00 00 00 00 00 00 |.;g_............|
00000200 90 3b 67 5f 4a 42 03 00 04 00 04 00 0a 00 09 00 |.;g_JB..........|
00000210 90 3b 67 5f 4a 42 03 00 01 00 29 01 01 00 00 00 |.;g_JB....).....|
00000220 90 3b 67 5f 4a 42 03 00 00 00 00 00 00 00 00 00 |.;g_JB..........|
00000230 90 3b 67 5f 89 d2 05 00 04 00 04 00 0a 00 09 00 |.;g_............|
00000240 90 3b 67 5f 89 d2 05 00 01 00 29 01 00 00 00 00 |.;g_......).....|
00000250 90 3b 67 5f 89 d2 05 00 00 00 00 00 00 00 00 00 |.;g_............|
00000260 90 3b 67 5f 87 c6 07 00 03 00 01 00 00 00 00 00 |.;g_............|
00000270 90 3b 67 5f 87 c6 07 00 00 00 00 00 00 00 00 00 |.;g_............|
00000280 90 3b 67 5f 49 9b 09 00 03 00 01 00 7f 00 00 00 |.;g_I...........|
00000290 90 3b 67 5f 49 9b 09 00 00 00 00 00 00 00 00 00 |.;g_I...........|
000002a0 90 3b 67 5f c8 56 0a 00 04 00 04 00 01 00 09 00 |.;g_.V..........|
000002b0 90 3b 67 5f c8 56 0a 00 01 00 20 01 01 00 00 00 |.;g_.V.... .....|
000002c0 90 3b 67 5f c8 56 0a 00 00 00 00 00 00 00 00 00 |.;g_.V..........|
000002d0 90 3b 67 5f 8b 2b 0c 00 04 00 04 00 01 00 09 00 |.;g_.+..........|
000002e0 90 3b 67 5f 8b 2b 0c 00 01 00 20 01 00 00 00 00 |.;g_.+.... .....|
000002f0 90 3b 67 5f 8b 2b 0c 00 00 00 00 00 00 00 00 00 |.;g_.+..........|
按键也会有相应的变化 输入设备代码操作由于游戏需要至少A,B,SELECT,START,UP,DOWN,LEFT,RIGHT 8个按键, 刚好手里有一个USB手柄,我们就以该手柄进行测试。 需要实现以下获取按键的函数
/* Query a button's state.
Returns 1 if button #b is pressed. */
int nes_key_state(int b)
{
ALLEGRO_KEYBOARD_STATE state;
al_get_keyboard_state(&state);
switch (b)
{
case 0: // On / Off
return 1;
case 1: // A
return al_key_down(&state, ALLEGRO_KEY_K);
case 2: // B
return al_key_down(&state, ALLEGRO_KEY_J);
case 3: // SELECT
return al_key_down(&state, ALLEGRO_KEY_U);
case 4: // START
return al_key_down(&state, ALLEGRO_KEY_I);
case 5: // UP
return al_key_down(&state, ALLEGRO_KEY_W);
case 6: // DOWN
return al_key_down(&state, ALLEGRO_KEY_S);
case 7: // LEFT
return al_key_down(&state, ALLEGRO_KEY_A);
case 8: // RIGHT
return al_key_down(&state, ALLEGRO_KEY_D);
default:
return 1;
}
}
先编写按键测试代码如下 struct input_event参见input.h的定义
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int s_keys_fd = -1;
uint32_t s_keys_state = 0;
void* key_poll(void* arg)
{
char ret[2];
struct input_event t;
s_keys_fd = open((char*)arg, O_RDONLY);
if(s_keys_fd <= 0)
{
printf("open %s device error!n",(char*)arg);
return 0;
}
while(1)
{
if(read(s_keys_fd, &t, sizeof(t)) == sizeof(t))
{
if(t.type==EV_KEY)
{
if(t.value==0 || t.value==1)
{
printf("key %d %sn", t.code, (t.value) ? "Pressed" : "Released");
//if(t.code == KEY_ESC)
// break;
}
}
else
{
///printf("type %d code %d value %dn", t.type, t.code, t.value);
}
}
}
return 0;
}
void key_init(void* arg)
{
pthread_t id;
/* 创建函数线程,并且指定函数线程要执行的函数 */
int res = pthread_create(&id,NULL,key_poll,arg);
assert(res == 0);
while(1)
{
}
exit(0);
}
int key_getstate(int key)
{
}
int main(int argc, char* argv[])
{
key_init(argv[1]);
while(1);
}
编译 aarch64-linux-gnu-gcc key.c -o key -lpthread chmod +x key 运行./key /dev/input/event1 测试 按键SELECT key 296 Pressed key 296 Released 按键START key 297 Pressed key 297 Released 右边按键上 key 288 Pressed key 288 Released 右边按键下 key 290 Pressed key 290 Released 右边按键左 key 291 Pressed key 291 Released 右边按键右 key 289 Pressed key 289 Released 右边 前面 上按键 key 293 Pressed key 293 Released 右边 前面 下按键 key 295 Pressed key 295 Released 左边 前面 上按键 key 292Pressed key 292 Released 左边 前面 下按键 key 294 Pressed key 294 Released 左边的上下左右四个按键无响应。
总结我们现在实现了游戏手柄按键输入的采集,完成了第一部分,后面继续显示和音频部分。
|