[轉] Linux 下的手柄测试

出處:http://url.blog.163.com/blog/static/23197521201011703452914/

买了个手柄玩PS2,却不能震动,也不知道是手柄不支持还是模拟器不支持。。特别写了一个程序来测试:

/*
* jstest.c
*
* joystick test for linux, with force feedback.
* no copyright for this, u can free for use.
*
* Created on: 2010-10-3
* Author: ArLi Weng <url@163.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/joystick.h>
void fftest(char *dvc) { //kernel/Documentation/input/ff.txt
int fd_event = open(dvc, O_RDWR);
if (fd_event < 0) {
printf("can't open fd: %sn", dvc);
return;
}
unsigned long req[4];
if (ioctl(fd_event, EVIOCGBIT(EV_FF, sizeof(req)), req) == -1) {
printf("ff io failed: %sn", dvc);
close(fd_event);
} else {
struct ff_effect ffe;
ffe.type = FF_RUMBLE;
ffe.u.rumble.strong_magnitude = 0xffff; //强力度
ffe.u.rumble.weak_magnitude = 0xffff; //弱力度
ffe.replay.length = 3000; //keep time
ffe.replay.delay = 0;
ffe.id = -1;
if (ioctl(fd_event, EVIOCSFF, &ffe) == -1) {
close(fd_event);
printf("not ff supportn");
return;
}
struct input_event ie;
ie.type = EV_FF;
ie.code = ffe.id;
ie.value = 1;
if (write(fd_event, (const void*) &ie, sizeof(ie)) == -1) {
close(fd_event);
printf("ff start errorn");
} else {
usleep((ffe.replay.length + 1) * 1000);
close(fd_event);
}
}
}
void loopevent(char *dvc) { //kernel/Documentation/input/joystick-api.txt
int fd_js = open(dvc, O_RDONLY | O_NONBLOCK);
if (fd_js < 0) {
printf("can't open fd: %sn", dvc);
return;
}
struct js_event jse;
int sz_jse = sizeof(jse);
while (1) {
usleep(1000);
if (read(fd_js, &jse, sz_jse) == sz_jse) {
jse.type &= ~JS_EVENT_INIT;
if (jse.type == JS_EVENT_AXIS) {
/* incompatible, maybe my problem, drvier by dragonrise.
*
if ((jse.number & 1) == 1) {
printf("axis y%d releasen", jse.number / 2);
} else {
printf("axis x%d releasen", jse.number / 2);
}
*/
printf("axis: %d, value: %dn", jse.number, jse.value);
} else if (jse.type == JS_EVENT_BUTTON) {
if (0 == jse.value) {
printf("button: %d releasen", jse.number);
} else if (1 == jse.value) {
printf("button: %d pressn", jse.number);
}
}
}
}
close(fd_js);
}
int main(int argc, char *argv[]) {
int jsn = 0;
if (argc > 1) {
jsn = atoi(argv[1]);
}
printf("number: %in", jsn);
char dvc[128];
DIR *dir;
sprintf(dvc, "/sys/class/input/js%d", jsn);
dir = opendir(dvc);
if (dir == NULL) {
printf("not exist: %s", dvc);
return EXIT_FAILURE;
} else {
closedir(dir);
}
int ie;
for (ie = 0; ie <= 64; ie++) {
sprintf(dvc, "/sys/class/input/js%d/device/event%d", jsn, ie);
dir = opendir(dvc);
if (dir != NULL) {
closedir(dir);
break;
}
}
printf("found event: %sn", dvc);
sprintf(dvc, "/dev/input/event%d", ie);
fftest(dvc);
sprintf(dvc, "/dev/input/js%d", jsn);
loopevent(dvc);
return EXIT_SUCCESS;
}
未經允許不得轉載:GoMCU » [轉] Linux 下的手柄测试

評論 搶沙發

  • 昵稱 (必填)
  • 郵箱 (必填)
  • 網址