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

  1. /*
  2.    simple test program for joystick driver
  3.  
  4.    usage: js 0       (to test first joystick)
  5.    usage: js 1       (to test second joystick)
  6. */
  7.  
  8. // #include <linux/joystick.h>
  9. #include "joystick.h"
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <errno.h>
  15.  
  16. int main (int argc, char **argv)
  17. {
  18.     int fd, status;
  19.     char *fname;
  20.     struct JS_DATA_TYPE js;
  21.     int xmin, ymin, xmax, ymax;
  22.     int xcenter, ycenter;
  23.     float xpartition_low, ypartition_low;
  24.     float xpartition_high, ypartition_high;
  25.     int calibrating;
  26.  
  27.     /* should be one argument, and it should be "0" or "1" */
  28.     if (argc != 2 || (strcmp (argv[1], "0") && strcmp (argv[1], "1"))) {
  29.         fprintf (stderr, "usage: js 0|1\n");
  30.         exit (1);
  31.     }
  32.  
  33.     /* pick appropriate device file */
  34.     if (!strcmp (argv[1], "0"))
  35.         fname = "/dev/js0";
  36.     else if (!strcmp (argv[1], "1"))
  37.         fname = "/dev/js1";
  38.     else
  39.         fname = NULL;
  40.  
  41.     /* open device file */
  42.     fd = open (fname, O_RDONLY);
  43.     if (fd < 0) {
  44.         perror (fname);
  45.         exit (1);
  46.     }
  47.  
  48.     calibrating = 1;
  49.     while(calibrating)
  50.     {
  51.     printf ("Joystick test program (interrupt to exit)\n");
  52.  
  53.     printf ("Joystick calibration:\n");
  54.     printf ("Move joystick to upper left corner and press button one\n");
  55.     while ((read (fd, &js, JS_RETURN) > 0) && (!(js.buttons & 1))) 
  56.         ;
  57.  
  58.     status = read (fd, &js, JS_RETURN);
  59.     if (status != JS_RETURN)
  60.     {
  61.         int temperrno = errno;
  62.         perror ("js read");
  63.         printf ("js - status = %d, errno = %d\n",
  64.             status, temperrno);
  65.         exit (1);
  66.     }
  67.  
  68.     xmin = js.x;
  69.     ymin = js.y;
  70.  
  71.     printf ("Move joystick to lower right corner and press button two\n");
  72.     while ((read (fd, &js, JS_RETURN) > 0) && (!(js.buttons & 2)))
  73.         ;
  74.  
  75.     status = read (fd, &js, JS_RETURN);
  76.     if (status != JS_RETURN)
  77.     {
  78.         int temperrno = errno;
  79.         perror ("js read");
  80.         printf ("js - status = %d, errno = %d\n",
  81.             status, temperrno);
  82.         exit (1);
  83.     }
  84.  
  85.     xmax = js.x;
  86.     ymax = js.y;
  87.  
  88.     printf ("Center joystick and press button one\n");
  89.     while ((read (fd, &js, JS_RETURN) > 0) && (!(js.buttons & 1)))
  90.         ;
  91.  
  92.     status = read (fd, &js, JS_RETURN);
  93.     if (status != JS_RETURN)
  94.     {
  95.         int temperrno = errno;
  96.         perror ("js read");
  97.         printf ("js - status = %d, errno = %d\n",
  98.             status, temperrno);
  99.         exit (1);
  100.     }
  101.  
  102.     xcenter = js.x;
  103.     ycenter = js.y;
  104.  
  105.     if (((xmax - xcenter) < 128)
  106.     || ((xcenter - xmin) < 128))
  107.     {
  108.         printf ("X-axis calibration failed\n");
  109.         printf ("Recalibrate\n");
  110.         continue;
  111.     }
  112.  
  113.     if (((ymax - ycenter) < 128)
  114.     || ((ycenter - ymin) < 128))
  115.     {
  116.         printf ("Y-axis calibration failed\n");
  117.         printf ("Recalibrate\n");
  118.         continue;
  119.     }
  120.  
  121.     xpartition_low = ((float)(xcenter - xmin)) / 128.0;
  122.     ypartition_low = ((float)(ycenter - ymin)) / 128.0;
  123.  
  124.     xpartition_high = ((float)(xmax - xcenter)) / 128.0;
  125.     ypartition_high = ((float)(ymax - ycenter)) / 128.0;
  126.     
  127.     js.buttons = 0;
  128.     
  129.     while ((!(js.buttons & 1)) || (!(js.buttons & 2)))
  130.     {
  131.     
  132.         int my_x, my_y;
  133.         
  134.         status = read (fd, &js, JS_RETURN);
  135.         if (status != JS_RETURN)
  136.         {
  137.         int temperrno = errno;
  138.         perror ("js read");
  139.         printf ("js - status = %d, errno = %d\n",
  140.             status, temperrno);
  141.         exit (1);
  142.         }
  143.  
  144.         if (js.x <  xcenter)  my_x = (js.x - xmin) / xpartition_low;
  145.         else  my_x = 128 + (js.x - xcenter) / xpartition_high;
  146.  
  147.         if (js.y <  ycenter)  my_y = (js.y - ymin) / ypartition_low;
  148.         else  my_y = 128 + (js.y - ycenter) / ypartition_high;
  149.  
  150.         if (my_x < 0) my_x = 0;
  151.         if (my_x > 255) my_x = 255;
  152.         if (my_y < 0) my_y = 0;
  153.         if (my_y > 255) my_y = 255;
  154.  
  155.         fprintf (stdout, "button 0: %s  button 1: %s  X position: %4d  Y position: %4d\r",
  156.             (js.buttons & 1) ? "on " : "off",
  157.             (js.buttons & 2) ? "on " : "off",
  158.             my_x, my_y);
  159.             
  160.         fflush (stdout);
  161.  
  162.         /* give other processes a chance */
  163.         usleep (100);
  164.     }
  165.     printf("\nRecalibrating\n");
  166.     sleep(1);
  167.     }
  168.  
  169.     exit (0);
  170. }
  171.