1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
/*
simple test program for joystick driver
usage: js 0 (to test first joystick)
usage: js 1 (to test second joystick)
*/
#include <linux/joystick.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main (int argc, char **argv)
{
int fd, status;
char *fname;
struct JS_DATA_TYPE js;
/* should be one argument, and it should be "0" or "1" */
if (argc != 2 || (strcmp (argv[1], "0") && strcmp (argv[1], "1"))) {
fprintf (stderr, "usage: js 0|1\n");
exit (1);
}
/* pick appropriate device file */
if (!strcmp (argv[1], "0"))
fname = "/dev/js0";
else if (!strcmp (argv[1], "1"))
fname = "/dev/js1";
else
fname = NULL;
/* open device file */
fd = open (fname, O_RDONLY);
if (fd < 0) {
perror ("js");
exit (1);
}
printf ("Joystick test program (interrupt to exit)\n");
while (1) {
status = read (fd, &js, JS_RETURN);
if (status != JS_RETURN) {
perror ("js");
exit (1);
}
fprintf (stdout, "button 0: %s button 1: %s X position: %4d Y position: %4d\r",
(js.buttons & 1) ? "on " : "off",
(js.buttons & 2) ? "on " : "off",
js.x,
js.y);
fflush (stdout);
/* give other processes a chance */
usleep (100);
}
exit (0);
}
|