home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / joystick / joy / joy.c next >
Encoding:
C/C++ Source or Header  |  1994-07-24  |  3.8 KB  |  168 lines

  1. /* Demonstration of reading joy stick postition
  2.     Written March 24, 1988
  3.     Written by : ajmyrvold@violet.waterloo.edu (Alan J. Myrvold)
  4.     Technical assistance from : uunet!netxcom!jallen (John Allen)
  5.     Turbo C Version 1.0
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <dos.h>
  10. #include <bios.h>
  11.  
  12. typedef struct {
  13.     int sw1,sw2;
  14.     int x,y;
  15.     int cenx,ceny;
  16.     } joy_stick;
  17.  
  18. joy_stick joy;
  19.  
  20. #define keypressed (bioskey(1) != 0)
  21. #define kb_clear() while keypressed bioskey(0);
  22.  
  23. void GotoXY(int x,int y)
  24. {
  25.     union REGS r;
  26. /* Set XY position */
  27.     r.h.ah = 2;
  28.     r.h.bh = 0;            /* Assume Video Page 0 */
  29.     r.h.dh = (char) y;
  30.     r.h.dl = (char) x;
  31.     int86(16,&r,&r);
  32. }
  33.  
  34. void ClrScr()
  35. {
  36.     union REGS r;
  37.  
  38. /* Get video mode */
  39.     r.h.ah = 15;
  40.     int86(16,&r,&r);
  41.  
  42. /* Set video mode */
  43.     r.h.ah = 0;
  44.     int86(16,&r,&r);
  45. }
  46.  
  47.  
  48. /*
  49. From: uunet!netxcom!jallen (John Allen)
  50.  
  51. 1.  Trigger the joystick oneshots with an 'out' to 0x201.
  52.     This will set all of the joystick bits on.
  53.  
  54. 2.  Read (in) 0x201, finding:
  55.  
  56.     Bit        Contents
  57.     0        Joystick A X coordinate
  58.     1        Joystick A Y coordinate
  59.     2        Joystick B X coordinate
  60.     3        Joystick B Y coordinate
  61.     4        Button A 1
  62.     5        Button A 2
  63.     6        Button B 1
  64.     7        Button B 2
  65.  
  66. 3.  Continue reading 0x201 until all oneshots return to zero,
  67.     recording the loop during which each bit falls to zero.
  68.     The duration of the pulse from each oneshot may be used to
  69.     determine the resistive load (from 0 to 100K) from each
  70.     Joystick, as: Time = 24.2msec. + .011 (r) msec.
  71.  
  72. 4.  To do this correctly, I recommend calibrating the joystick;
  73.     have the user move the stick to each corner, then center it,
  74.     while recording the resulting values.
  75. */
  76.  
  77. void disp_stick(int line,joy_stick *joy)
  78. {
  79.     GotoXY(0,line);
  80.     printf("sw1 %d sw2 %d",joy -> sw1,joy -> sw2);
  81.     GotoXY(0,line+1);
  82.     printf("x %4d y %4d",joy -> x,joy -> y);
  83. }
  84.  
  85. void read_stick(int stick,joy_stick *joy)
  86. {
  87.     int k,jx,jy;
  88.     int c,m1,m2,m3,m4,m5;
  89.  
  90. /* Define masks for the chosen joystick */
  91.     if (stick == 1) m4 = 1; else
  92.     if (stick == 2) m4 = 4; else
  93.     printf("Invalid stick %d\n",stick);
  94.  
  95.     m5 = m4 << 1;
  96.     m1 = m4 << 4;
  97.     m2 = m5 << 4;
  98.     m3 = m4 + m5;
  99.  
  100. /* Trigger joystick */
  101.     outportb(0x201,0xff);
  102.     c = inportb(0x201);
  103.  
  104. /* Read switch settings */
  105.     joy -> sw1 = (c & m1) == 0;
  106.     joy -> sw2 = (c & m2) == 0;
  107.  
  108. /* Get X and Y positions */
  109.     for (k = 0; (c & m3) != 0; k++) {
  110.         if ((c & m4) != 0) jx = k;
  111.         if ((c & m5) != 0) jy = k;
  112.         c = inportb(0x201);
  113.     }
  114.     joy -> x = jx - (joy -> cenx);
  115.     joy -> y = jy - (joy -> ceny);
  116. }
  117.  
  118. int choose_stick(joy_stick *joy)
  119. {
  120.     int init_swa,init_swb,swa,swb;
  121.     int c,retval;
  122.  
  123.     printf("Center joystick and press fire, or press any key\n");
  124.     kb_clear();
  125.     outportb(0x201,0xff);
  126.     c = inportb(0x201);
  127.     init_swa = c & 0x30;
  128.     init_swb = c & 0xc0;
  129.     do {
  130.        outportb(0x201,0xff);
  131.        c = inportb(0x201);
  132.        swa = c & 0x30;
  133.        swb = c & 0xc0;
  134.     } while ((swa == init_swa) && (swb == init_swb) && !keypressed);
  135.     if (swa != init_swa) {
  136.        printf("Joystick 1 selected\n");
  137.        retval = 1;
  138.     } else if (swb != init_swb) {
  139.        printf("Joystick 2 selected\n");
  140.        retval = 2;
  141.     } else {
  142.        printf("Keyboard selected\n");
  143.        kb_clear();
  144.        retval = 0;
  145.     }
  146.  
  147.     if (retval != 0) { /* Determine Center */
  148.        joy -> cenx = joy -> ceny = 0;
  149.        read_stick(retval,joy);
  150.        joy -> cenx = joy -> x;
  151.        joy -> ceny = joy -> y;
  152.     }
  153.  
  154.     return(retval);
  155. }
  156.  
  157. main()
  158. {
  159.    int k;
  160.  
  161.    k = choose_stick(&joy);
  162.    ClrScr();
  163.    if (k != 0) while (!keypressed) {
  164.         read_stick(k,&joy);
  165.         disp_stick(0,&joy);
  166.    }
  167. }
  168.