買了一個支持 USB OTG, 藍牙 連接的 安卓手柄。
接到 ubunto 上 dmesg 可以看到識別出來的信息,內核已經支持了。
usb 2-2.2: new full-speed USB device number 5 using uhci_hcd usb 2-2.2: New USB device found, idVendor=20bc, idProduct=5500 usb 2-2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0 usb 2-2.2: Product: Android Gamepad usb 2-2.2: Manufacturer: ShanWan input: ShanWan Android Gamepad as /devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-2/2-2.2/2-2.2:1.0/input/input5 hid-generic 0003:20BC:5500.0002: input,hidraw1: USB HID v1.10 Gamepad [ShanWan Android Gamepad] on usb-0000:02:00.0-2.2/input0
根據 VID 20bc PID 5500 在 內核源碼中搜索
hid/hid-ids.h:196:#define USB_VENDOR_ID_BETOP_2185V2BFM 0x20bc
hid/hid-betopff.c:16: * 0x20bc:0x5500 "BTP2185 V2 BFM mode Joystick"
配置內核加上 hid-betopff.c
Device Drivers
Input device support
HID support
Special HID drivers
Betop Production Inc. force feedback support
使用新內核啟動后,接入 USB 手柄,有打印信息,有生成設備節點
寫測試程序,確定按鍵的對應碼。
1 /** 2 * 參考內核文檔 joystick-api.txt 3 * author: ningci dev date: 2017-06-02 18:00 4 */ 5 #include <stdio.h> 6 #include <sys/types.h> 7 #include <sys/stat.h> 8 #include <fcntl.h> 9 10 struct js_event { 11 unsigned int time; /* event timestamp in milliseconds */ 12 unsigned short value; /* value */ 13 unsigned char type; /* event type */ 14 unsigned char number; /* axis/button number */ 15 }; 16 17 int main(int argc, char **argv) 18 { 19 struct js_event e; 20 int fd = open("/dev/input/js0", O_RDONLY); 21 while(1) 22 { 23 if(0 < read (fd, &e, sizeof(e))) 24 { 25 printf("value:0x%x type:0x%x number:0x%x \n", e.value, e.type, e.number); 26 } 27 } 28 close(fd); 29 return 0; 30 }
測試后有用的按鍵的整理
value:0x8001 type:0x2 number:0x5 上
value:0x0 type:0x2 number:0x5 松開
value:0x7fff type:0x2 number:0x5 下
value:0x0 type:0x2 number:0x5 松開
value:0x8001 type:0x2 number:0x4 左
value:0x0 type:0x2 number:0x4 松開
value:0x7fff type:0x2 number:0x4 右
value:0x0 type:0x2 number:0x4 松開
value:0x1 type:0x1 number:0xa 選擇
value:0x0 type:0x1 number:0xa 松開
value:0x1 type:0x1 number:0xb 開始
value:0x0 type:0x1 number:0xb 松開
value:0x1 type:0x1 number:0x0 A
value:0x0 type:0x1 number:0x0 松開
value:0x1 type:0x1 number:0x1 B
value:0x0 type:0x1 number:0x1 松開
value:0x1 type:0x1 number:0x3 X
value:0x0 type:0x1 number:0x3 松開
value:0x1 type:0x1 number:0x4 Y
value:0x0 type:0x1 number:0x4 松開
因為USB 手柄在 read 時會阻塞,所以改用 多線程方式,進入輸入。
全整源碼,可以關注 github 。
暫未支持 連A 連B 的支持。