home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TANK11.ZIP / SOURCE.ZIP / JOY.C < prev    next >
C/C++ Source or Header  |  1993-01-18  |  4KB  |  160 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.     Changes: 1/93 Kevin Dahlhausen ap0962po.cwru.edu
  8.         thresholds, JoyStickx()
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <dos.h>
  14. #include <bios.h>
  15. #include "joy.h"
  16.  
  17. /*
  18. From: uunet!netxcom!jallen (John Allen)
  19.  
  20. 1.  Trigger the joystick oneshots with an 'out' to 0x201.
  21.     This will set all of the joystick bits on.
  22.  
  23. 2.  Read (in) 0x201, finding:
  24.  
  25.     Bit        Contents
  26.     0        Joystick A X coordinate
  27.     1        Joystick A Y coordinate
  28.     2        Joystick B X coordinate
  29.     3        Joystick B Y coordinate
  30.     4        Button A 1
  31.     5        Button A 2
  32.     6        Button B 1
  33.     7        Button B 2
  34.  
  35. 3.  Continue reading 0x201 until all oneshots return to zero,
  36.     recording the loop during which each bit falls to zero.
  37.     The duration of the pulse from each oneshot may be used to
  38.     determine the resistive load (from 0 to 100K) from each
  39.     Joystick, as: Time = 24.2msec. + .011 (r) msec.
  40.  
  41. 4.  To do this correctly, I recommend calibrating the joystick;
  42.     have the user move the stick to each corner, then center it,
  43.     while recording the resulting values.
  44. */
  45.  
  46. void read_stick(int stick,joy_stick *joy)
  47. {
  48.     int k,jx,jy,i;
  49.     int c,m1,m2,m3,m4,m5;
  50.  
  51. /* Define masks for the chosen joystick */
  52.     if (stick == 1) m4 = 1; else
  53.     if (stick == 2) m4 = 4; else
  54.     printf("Invalid stick %d\n",stick);
  55.  
  56.     m5 = m4 << 1;
  57.     m1 = m4 << 4;
  58.     m2 = m5 << 4;
  59.     m3 = m4 + m5;
  60.  
  61. /* Trigger joystick */
  62.     outportb(0x201,0xff);
  63.     c = inportb(0x201);
  64.  
  65. /* Read switch settings */
  66.     joy -> sw1 = (c & m1) == 0;
  67.     joy -> sw2 = (c & m2) == 0;
  68.  
  69. /* Get X and Y positions */
  70.     for (k = 0; (c & m3) != 0; k++) {
  71.     if ((c & m4) != 0) jx = k;
  72.     else i=k;
  73.     if ((c & m5) != 0) jy = k;
  74.     else i=k;
  75.     c = inportb(0x201);
  76.     }
  77.     
  78.     joy -> x = jx; // - (joy -> cenx);
  79.     joy -> y = jy; // - (joy -> ceny);
  80.  
  81.     for (jx=k; jx<300; jx++) {
  82.         if (k || i) jy=k;
  83.         if (k || i) jy=k;
  84.         c=inportb(0x201);
  85.     }
  86. }
  87.  
  88. int choose_stick()
  89. {
  90.     int init_swa,init_swb,swa,swb;
  91.     int c,retval;
  92.  
  93.     printf("Press fire.\n");
  94.     outportb(0x201,0xff);
  95.     c = inportb(0x201);
  96.     init_swa = c & 0x30;
  97.     init_swb = c & 0xc0;
  98.     do {
  99.        outportb(0x201,0xff);
  100.        c = inportb(0x201);
  101.        swa = c & 0x30;
  102.        swb = c & 0xc0;
  103.     } while ((swa == init_swa) && (swb == init_swb));
  104.     if (swa != init_swa) {
  105.        printf("Joystick 1 selected\n");
  106.        retval = 1;
  107.     } else if (swb != init_swb) {
  108.        printf("Joystick 2 selected\n");
  109.        retval = 2;
  110.     } else {
  111.        printf("Keyboard selected\n");
  112.        retval = 0;
  113.     }
  114.  
  115.     return(retval);
  116. }
  117.  
  118. void set_thresh(int stick, joy_stick *joy)
  119. /* requires: joy initialized
  120.  * ensures:  thresholds set
  121.  */
  122. {
  123.  joy->cenx=joy->ceny=0;
  124.  while(joy->sw1||joy->sw2) read_stick(stick, joy);
  125.  printf("\nCenter joystick and press one of the buttons.");
  126.  do {
  127.     read_stick(stick, joy);
  128.  } while (!(joy->sw1) && !(joy->sw2));
  129.  joy->ceny=joy->y;
  130.  joy->cenx=joy->x;
  131.  
  132.  
  133.  while(joy->sw1||joy->sw2) read_stick(stick, joy);
  134.  printf("\nMove joystick to upper left and press one of the buttons.");
  135.  do {
  136.     read_stick(stick, joy);
  137.  } while (!(joy->sw1) && !(joy->sw2));
  138.  joy->tu=joy->ceny-((joy->ceny-joy->y)/2);
  139.  joy->tl=joy->cenx-((joy->cenx-joy->x)/2);
  140.  
  141.  while(joy->sw1||joy->sw2) read_stick(stick, joy);
  142.  printf("\nMove joystick to lower right and press one of the buttons.");
  143.  do {
  144.     read_stick(stick, joy);
  145.  } while (!(joy->sw1) && !(joy->sw2));
  146.  joy->td=joy->ceny+((joy->y-joy->ceny)/2);
  147.  joy->tr=joy->cenx+((joy->x-joy->cenx)/2);
  148. }
  149.  
  150. int joy_state(int stick, joy_stick *j)
  151. /* bitmap:    xxx4321 ->  4=left, 2=right, 3=up 1=down */
  152. {
  153.     int rval=0;
  154.     read_stick(stick, j);
  155.     if (j->x <= j->tl) rval=8;
  156.     if (j->y <= j->tu) rval=rval+4;
  157.     if (j->x >= j->tr) rval=rval+2;
  158.     if (j->y >= j->td) rval=rval+1;
  159.     return rval;
  160. }