home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Drivers / joystick-0.0-I / joystick.next / test / jscal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-03  |  2.3 KB  |  112 lines

  1.  
  2. /*
  3.    Joystick calibration program
  4. */
  5.  
  6. #include "joystick.h"
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <sys/ioctl.h>
  11. #include <errno.h>
  12.  
  13. int main (int argc, char **argv)
  14. {
  15.     int fd, status, tmp;
  16.     long tmpl;
  17.     char *fname;
  18.     struct JS_DATA_TYPE js_data;
  19.  
  20.     /* should be one argument, and it should be "0" or "1" */
  21.     if (argc != 2 || (strcmp (argv[1], "0") && strcmp (argv[1], "1"))) {
  22.         fprintf (stderr, "usage: jscal 0|1\n");
  23.         exit (1);
  24.     }
  25.  
  26.     /* pick appropriate device file */
  27.     if (!strcmp (argv[1], "0"))
  28.         fname = "/dev/js0";
  29.     else if (!strcmp (argv[1], "1"))
  30.         fname = "/dev/js1";
  31.     else
  32.         fname = NULL;
  33.  
  34.     /* open device file */
  35.     fd = open (fname, O_RDONLY);
  36.     if (fd < 0) {
  37.         perror (fname);
  38.         exit (1);
  39.     }
  40.  
  41.     status = ioctl (fd, JS_GET_TIMEOUT, &tmp);
  42.     if (status == -1) {
  43.         perror ("jscal ioctl");
  44.         exit (1);
  45.     }
  46.  
  47.     printf ("Timeout value = %d\n", tmp);
  48.  
  49.     status = ioctl (fd, JS_GET_TIMELIMIT, &tmpl);
  50.     if (status == -1) {
  51.         perror ("jscal ioctl");
  52.         exit (1);
  53.     }
  54.  
  55.     printf ("Timelimit value = %ld ms\nSetting Timelimit = 100 ms\n", tmpl);
  56.  
  57.     tmpl = 100;
  58.  
  59.     status = ioctl (fd, JS_SET_TIMELIMIT, &tmpl);
  60.     if (status == -1) {
  61.         perror ("jscal ioctl");
  62.         exit (1);
  63.     }
  64.  
  65.     status = ioctl (fd, JS_GET_CAL, &js_data);
  66.     if (status == -1) {
  67.         perror ("jscal ioctl");
  68.         exit (1);
  69.     }
  70.  
  71.     printf ("Current correction: %d , %d\n", js_data.x, js_data.y);
  72.  
  73.     printf ("Move joystick to lower right corner and press either button\n");
  74.  
  75.     while ((read (fd, &js_data, JS_RETURN) > 0) && js_data.buttons == 0x00)
  76.         printf ("Got x = %04x, y = %04x\r", js_data.x, js_data.y);
  77.  
  78.     for (tmp = 0; js_data.x > 0xff; tmp++, js_data.x = js_data.x >> 1);
  79.     js_data.x = tmp;
  80.     for (tmp = 0; js_data.y > 0xff; tmp++, js_data.y = js_data.y >> 1);
  81.     js_data.y = tmp;
  82.  
  83.     printf ("Setting correction: %d , %d\n", js_data.x, js_data.y);
  84.  
  85.     status = ioctl (fd, JS_SET_CAL, &js_data);
  86.     if (status == -1) {
  87.         perror ("jscal ioctl");
  88.         exit (1);
  89.     }
  90.  
  91.     status = ioctl (fd, JS_GET_CAL, &js_data);
  92.     if (status == -1) {
  93.         perror ("jscal ioctl");
  94.         exit (1);
  95.     }
  96.  
  97.     printf ("Verify Correction (interrupt to exit): %d, %d\n",
  98.         js_data.x,
  99.         js_data.y);
  100.  
  101.     while (1)
  102.         if (read (fd, &js_data, JS_RETURN) != JS_RETURN)
  103.             perror ("jscal read");
  104.         else {
  105.             fprintf (stdout, "%x %x %x        \r",
  106.                 js_data.buttons, js_data.x, js_data.y);
  107.             fflush (stdout);
  108.             usleep(100);
  109.         }
  110.     close (fd);
  111. }
  112.