home *** CD-ROM | disk | FTP | other *** search
- /*
- 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 "joystick.h"
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
-
- int main (int argc, char **argv)
- {
- int fd, status;
- char *fname;
- struct JS_DATA_TYPE js;
- int xmin, ymin, xmax, ymax;
- int xcenter, ycenter;
- float xpartition_low, ypartition_low;
- float xpartition_high, ypartition_high;
- int calibrating;
-
- /* 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 (fname);
- exit (1);
- }
-
- calibrating = 1;
- while(calibrating)
- {
- printf ("Joystick test program (interrupt to exit)\n");
-
- printf ("Joystick calibration:\n");
- printf ("Move joystick to upper left corner and press button one\n");
- while ((read (fd, &js, JS_RETURN) > 0) && (!(js.buttons & 1)))
- ;
-
- status = read (fd, &js, JS_RETURN);
- if (status != JS_RETURN)
- {
- int temperrno = errno;
- perror ("js read");
- printf ("js - status = %d, errno = %d\n",
- status, temperrno);
- exit (1);
- }
-
- xmin = js.x;
- ymin = js.y;
-
- printf ("Move joystick to lower right corner and press button two\n");
- while ((read (fd, &js, JS_RETURN) > 0) && (!(js.buttons & 2)))
- ;
-
- status = read (fd, &js, JS_RETURN);
- if (status != JS_RETURN)
- {
- int temperrno = errno;
- perror ("js read");
- printf ("js - status = %d, errno = %d\n",
- status, temperrno);
- exit (1);
- }
-
- xmax = js.x;
- ymax = js.y;
-
- printf ("Center joystick and press button one\n");
- while ((read (fd, &js, JS_RETURN) > 0) && (!(js.buttons & 1)))
- ;
-
- status = read (fd, &js, JS_RETURN);
- if (status != JS_RETURN)
- {
- int temperrno = errno;
- perror ("js read");
- printf ("js - status = %d, errno = %d\n",
- status, temperrno);
- exit (1);
- }
-
- xcenter = js.x;
- ycenter = js.y;
-
- if (((xmax - xcenter) < 128)
- || ((xcenter - xmin) < 128))
- {
- printf ("X-axis calibration failed\n");
- printf ("Recalibrate\n");
- continue;
- }
-
- if (((ymax - ycenter) < 128)
- || ((ycenter - ymin) < 128))
- {
- printf ("Y-axis calibration failed\n");
- printf ("Recalibrate\n");
- continue;
- }
-
- xpartition_low = ((float)(xcenter - xmin)) / 128.0;
- ypartition_low = ((float)(ycenter - ymin)) / 128.0;
-
- xpartition_high = ((float)(xmax - xcenter)) / 128.0;
- ypartition_high = ((float)(ymax - ycenter)) / 128.0;
-
- js.buttons = 0;
-
- while ((!(js.buttons & 1)) || (!(js.buttons & 2)))
- {
-
- int my_x, my_y;
-
- status = read (fd, &js, JS_RETURN);
- if (status != JS_RETURN)
- {
- int temperrno = errno;
- perror ("js read");
- printf ("js - status = %d, errno = %d\n",
- status, temperrno);
- exit (1);
- }
-
- if (js.x < xcenter) my_x = (js.x - xmin) / xpartition_low;
- else my_x = 128 + (js.x - xcenter) / xpartition_high;
-
- if (js.y < ycenter) my_y = (js.y - ymin) / ypartition_low;
- else my_y = 128 + (js.y - ycenter) / ypartition_high;
-
- if (my_x < 0) my_x = 0;
- if (my_x > 255) my_x = 255;
- if (my_y < 0) my_y = 0;
- if (my_y > 255) my_y = 255;
-
- 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",
- my_x, my_y);
-
- fflush (stdout);
-
- /* give other processes a chance */
- usleep (100);
- }
- printf("\nRecalibrating\n");
- sleep(1);
- }
-
- exit (0);
- }
-