home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / JOYGA.ZIP / JOYSTK.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-02-13  |  2.5 KB  |  83 lines

  1. {$S-,R-,V-,D-,T-}
  2.  
  3. unit JoyStk;                {Joystick routines for Turbo Pascal 4.0}
  4.                             {John Haluska, CIS 74000,1106}
  5. interface
  6.  
  7. const
  8.   BtnA1 = 16;  BtnA2 = 32;   {for joystick buttons }
  9.   BtnB1 = 64;  BtnB2 = 128;
  10. var
  11.   JoyMaxCt : word;           {max count for joystick postion output }
  12.                              {set by procedure InitJoyMaxCt }
  13.  
  14. procedure JoyStkPos (N : byte; var X,Y : integer);
  15.  
  16. function JoyStkBtn (Num:byte) : boolean;
  17.  
  18. function JoyStkPresent : boolean;
  19.  
  20. function GetClkTicks : longint;
  21.  
  22. procedure JoyStkCal (N : byte; var X,Y : integer);
  23.  
  24. implementation
  25.  
  26. {$L JOYSTK.OBJ}
  27. {$F+}
  28. procedure JoyStkPos (N : byte; var X,Y : integer); external;
  29.          {Return X, Y position of joystick 0 or 1}
  30.  
  31. function JoyStkBtn (Num:byte) : boolean; external;
  32.         {Return position (true-button pressed or false-button not pressed)
  33.          for button 16, 32, 64 or 128 corresponding to bit positions 4 - 7.
  34.          No contact debounce is provided.}
  35.  
  36. function JoyStkPresent : boolean; external;
  37.         {Returns true if a game adapter interface is present.}
  38.  
  39. function GetClkTicks : longint; external;
  40.         {Return DOS time in ticks (18.2/sec).  The ticks/second is
  41.          1193180/65535.  The midnight value is 1573040.
  42.          Ref: Programer's Guide To PC, Peter Norton, ISBN 0-914845-46-2, p223}
  43. {$F-}
  44. procedure InitJoyMaxCt;
  45.         {Determine JoyMaxCt as a function of processor speed.}
  46.   var
  47.     I,J,T3   : word;
  48.     T1,T2    : longint;
  49.   begin
  50.     repeat
  51.       I := 0;
  52.       T1 := GetClkTicks;
  53.       for J := 1 to 65535 do I := I + 1;
  54.       T2 := GetClkTicks;
  55.     until T2 > T1;    {prevent error if counter rollover at midnight}
  56.     T3 := T2 - T1;
  57.     JoyMaxCt := 5000 div T3;
  58.   end;
  59.  
  60. procedure JoyStkCal (N : byte; var X,Y : integer);
  61.          {Return X, Y position of joystick N (0 or 1) when any joystick
  62.           button is pressed.  Joystick button is contact debounced by waiting
  63.           55 - 110 milliseconds.}
  64.   var
  65.     Ts, Tl   : longint;
  66.   begin
  67.     repeat
  68.       JoyStkPos(N, X, Y);
  69.     until JoyStkBtn(BtnA1) or JoyStkBtn(BtnA2) or JoyStkBtn(BtnB1)
  70.           or JoyStkBtn(BtnB2);
  71.     Ts := GetClkTicks;
  72.     repeat
  73.       repeat
  74.         Tl := GetClkTicks - Ts;
  75.       until Tl >= 2;            {55 -110 ms delay for switch contact bounce}
  76.     until not(JoyStkBtn(BtnA1) or JoyStkBtn(BtnA2) or JoyStkBtn(BtnB1)
  77.           or JoyStkBtn(BtnB2));
  78.   end;
  79.  
  80. begin
  81.   InitJoyMaxCt;     {Intialize max count}
  82. end.
  83.