linux测试手柄,Linux Joystick 介绍
Linux手柄使用
Linux手柄驅(qū)動(dòng), 一般為joydev, 可使用modprobe -a joydev加載驅(qū)動(dòng)模塊。
手柄連接后的原始設(shè)備文件為/dev/hidraw*, 這個(gè)和具體手柄廠商的驅(qū)動(dòng)相關(guān)。需要針對(duì)特定手柄進(jìn)行操作優(yōu)化可能需要使用這個(gè)設(shè)備。
通常情況下joydev手柄驅(qū)動(dòng)提供一個(gè)通用的手柄接口供使用。joydev驅(qū)動(dòng)創(chuàng)建的設(shè)備文件一般為/dev/input/js*。joydev驅(qū)動(dòng)提供兩種不同操作: 搖桿axes和按鍵button。
一般使用joydev提供的通用驅(qū)動(dòng)。使用joydev一些手柄會(huì)將十字方向鍵當(dāng)作搖桿類型,而不是button類型,比如我的手柄北通蝙蝠D2(BTP-BD2F),因此在一些游戲模擬器軟件中不能識(shí)別十字按鍵。
實(shí)用工具
在Linux中提供幾個(gè)實(shí)用工具:
jscal: 按鍵校準(zhǔn)和按鍵重映射工具
jscal-restore
jscal-store
jstest: 讀取按鍵狀態(tài),測(cè)試按鍵是否正確
jscal 按鍵映射示例:
An example output of -q looks like this ./jscal -q /dev/input/js0:
-u
n_of_buttons,btnmap1,btnmap2,
...> --set-mappings Sets axis and button mappings to the
specified valuesjscal -u
10,0,1,2,5,6,16,17,40,41,42,13,288,289,290,291,292,293,294,295,296,297,298,299,300
/dev/input/js0
The joystick has 10 axes and 13 buttons. If now one is to switch axes 2jscal -u
10,0,1,5,2,6,16,17,40,41,42,13,288,289,290,291,292,293,294,295,296,297,298,299,300
/dev/input/js0
changing 2,5 to 5,2 on the line.
Remapping buttons is done the same way.
jstest示例:
jstest /dev/input/js0
手柄編程#include
#include
#include
typedef unsigned int __u32;
typedef short __s16;
typedef unsigned char __u8;
struct js_event {
__u32 time; /* event timestamp in milliseconds */
__s16 value; /* value */
__u8 type; /* event type */
__u8 number; /* axis/button number */
};
#define JS_EVENT_BUTTON 0x01 /* button pressed/released */
#define JS_EVENT_AXIS 0x02 /* joystick moved */
#define JS_EVENT_INIT 0x80 /* initial state of device */
int main() {
int fd = open("/dev/input/js0", O_RDONLY);
struct js_event e;
while(1) {
read(fd, &e, sizeof(e));
int type = JS_EVENT_BUTTON | JS_EVENT_INIT;
switch(e.type) {
case JS_EVENT_AXIS:
printf("axis number: %d, value: %d, time: %d\n", e.number, e.value, e.time);
break;
case JS_EVENT_BUTTON:
printf("btn: number: %d, value: %d, time: %d\n", e.number, e.value, e.time);
break;
}
}
close(fd);
return 0;
}
參考:
總結(jié)
以上是生活随笔為你收集整理的linux测试手柄,Linux Joystick 介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: io密集型和cpu密集型_一次说明白Py
- 下一篇: 房子大门朝东前面没有当自有湖房孑的当头有