"Fossies" - the Fresh Open Source Software Archive

Member "alec64-1.13/util/joycal.c" (28 Sep 1996, 2105 Bytes) of package /linux/misc/old/alec64-1.13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* To compile                */
    2 /*  gcc joycal.c -o joycal   */
    3 
    4 #include <linux/joystick.h>
    5 #include <stdio.h>
    6 #include <fcntl.h>
    7 #include <unistd.h>
    8 #include <sys/ioctl.h>
    9 #include <errno.h>
   10 #include <malloc.h>
   11 
   12 static int JoyDevice;
   13 
   14 struct JS_DATA_TYPE JoyPosition ()
   15 {
   16     struct JS_DATA_TYPE js_data;
   17     
   18     while ((read (JoyDevice, &js_data, JS_RETURN) > 0) && js_data.buttons != 0x00);
   19     while ((read (JoyDevice, &js_data, JS_RETURN) > 0) && js_data.buttons == 0x00)
   20     printf ("    x = %5d, y = %5d\r", js_data.x, js_data.y);
   21     
   22     return (js_data);
   23 }
   24 
   25 void Calibrate (int Sensibility)
   26 {
   27     
   28     long tmpl;
   29     int status;
   30     char fname[] = "/dev/js0";
   31     struct JS_DATA_TYPE js_data, JoyUpperLeft, JoyCenter, JoyLowerRight;
   32     
   33     JoyDevice = open (fname, O_RDONLY);
   34     if (JoyDevice < 0) {
   35     perror ("jscal");
   36     exit (1);
   37     }
   38     
   39     tmpl = 2;
   40     status = ioctl (JoyDevice, JS_SET_TIMELIMIT, &tmpl);
   41     if (status == -1) {
   42     perror ("jscal");
   43     exit (1);
   44     }
   45     
   46     js_data.x = 0;
   47     js_data.y = 0;
   48     status = ioctl (JoyDevice, JS_SET_CAL, &js_data);
   49     if (status == -1) {
   50     perror ("jscal");
   51     exit (1);
   52     }
   53  
   54     printf ("Center Joystick and push button.\n");
   55     JoyCenter = JoyPosition ();
   56     
   57     printf ("Move Joystick to upper left and push button.\n");
   58     JoyUpperLeft = JoyPosition ();
   59     
   60     printf ("Move Joystick to lower right and push button.\n");
   61     JoyLowerRight = JoyPosition ();
   62     
   63     printf ("                                              \n");
   64     
   65     printf ("Up:    %5d\n", JoyCenter.y - 
   66                           (JoyCenter.y - JoyUpperLeft.y) * Sensibility / 100);
   67     printf ("Down:  %5d\n", JoyCenter.y + 
   68                          (JoyLowerRight.y - JoyCenter.y) * Sensibility / 100);
   69     printf ("Left:  %5d\n", JoyCenter.x - 
   70                           (JoyCenter.x - JoyUpperLeft.x) * Sensibility / 100);
   71     printf ("Right: %5d\n", JoyCenter.x + 
   72                          (JoyLowerRight.x - JoyCenter.x) * Sensibility / 100);
   73     close (JoyDevice);
   74 }
   75 
   76 
   77 void main ()
   78 {
   79     printf("\nCalibrating Joystick:\n\n");
   80     Calibrate (50);
   81 }
   82