home *** CD-ROM | disk | FTP | other *** search
/ Apollo 18: The Moon Missions / 990125_1647.ISO / Docking / CALIBRAT / JOYSTICK.BAS < prev    next >
BASIC Source File  |  1994-12-31  |  4KB  |  123 lines

  1. Option Explicit
  2. '-------------------------------------------------------
  3. ' JOYSTICK.BAS - Joystick support routines for
  4. '                Visual Basic.
  5. '-------------------------------------------------------
  6.  
  7. ' Joystick Device ID
  8. Global Const JOYSTICK1 = 0
  9. Global Const JOYSTICK2 = 1
  10.  
  11. ' Joystick error return values
  12. Global Const JOYERR_NOERROR = 0
  13. Global Const JOYERR_PARMS = 165
  14. Global Const MMSYSERR_NODRIVER = 6
  15. Global Const JOYERR_UNPLUGGED = 167
  16.  
  17. ' Joystick button bit-flags used by tJoyInfo.ButtonStates
  18. Global Const JOY_BUTTON1 = &H1
  19. Global Const JOY_BUTTON2 = &H2
  20. Global Const JOY_BUTTON3 = &H4
  21. Global Const JOY_BUTTON4 = &H8
  22.  
  23.  
  24. ' Joystick Position
  25. Type tJoyInfo
  26.     Xin As Integer
  27.     Yin As Integer
  28.     Zin As Integer
  29.     ButtonStates As Integer
  30.     
  31.     ' These values are determined by the fields above.
  32.     X As Long
  33.     Y As Long
  34.     Z As Long
  35.     ButtonDown(1 To 4) As Integer
  36. End Type
  37.  
  38. ' Joystick Capabilities
  39.  
  40. Const MAXPNAMELEN = 32
  41.  
  42. Type tJoyCaps
  43.     Mid As Integer
  44.     Pid As Integer
  45.     Pname As String * MAXPNAMELEN
  46.     XminIn As Integer
  47.     XmaxIn As Integer
  48.     YminIn As Integer
  49.     YmaxIn As Integer
  50.     ZminIn As Integer
  51.     ZmaxIn As Integer
  52.     NumButtons As Integer
  53.     PeriodMin As Integer
  54.     PeriodMax As Integer
  55.  
  56.     Xmin As Long
  57.     Xmax As Long
  58.     Ymin As Long
  59.     Ymax As Long
  60.     Zmin As Long
  61.     Zmax As Long
  62. End Type
  63.  
  64. Global JoyCaps As tJoyCaps
  65.  
  66. ' Joystick API Calls
  67. Declare Function joyGetDevCaps Lib "MMSystem" (ByVal IDDevice As Integer, JCaps As tJoyCaps, ByVal CapSize As Integer) As Integer
  68. Declare Function joyGetPos Lib "MMSystem" (ByVal IDDevice As Integer, JPos As tJoyInfo) As Integer
  69.  
  70. Function GetJoyStickPos (IDDevice As Integer, JoyInfo As tJoyInfo) As Integer
  71. '-------------------------------------------------------
  72. ' This function is a wrapper around the joyGetPos API
  73. ' call.  That call returns coordinates as unsigned
  74. ' long integers, which VB doesn't support.  We move
  75. ' these coordinates into long values so that they
  76. ' can be easily evaluated.
  77. '-------------------------------------------------------
  78. Dim rc As Integer
  79. Static NotFirstTime As Integer
  80.  
  81.     If Not NotFirstTime Then
  82.         NotFirstTime = False
  83.         rc = joyGetDevCaps(IDDevice, JoyCaps, Len(JoyCaps))
  84.  
  85.         If rc <> 0 Then
  86.             GetJoyStickPos = rc
  87.             Exit Function
  88.         End If
  89.  
  90.         JoyCaps.Xmax = uint_to_long(JoyCaps.XmaxIn)
  91.         JoyCaps.Xmin = uint_to_long(JoyCaps.XminIn)
  92.         JoyCaps.Ymax = uint_to_long(JoyCaps.YmaxIn)
  93.         JoyCaps.Ymin = uint_to_long(JoyCaps.YminIn)
  94.         JoyCaps.Zmax = uint_to_long(JoyCaps.ZmaxIn)
  95.         JoyCaps.Zmin = uint_to_long(JoyCaps.ZminIn)
  96.  
  97.     End If
  98.  
  99.     rc = joyGetPos(IDDevice, JoyInfo)
  100.     GetJoyStickPos = rc
  101.  
  102.     If rc <> 0 Then Exit Function
  103.  
  104.     JoyInfo.X = uint_to_long(JoyInfo.Xin)
  105.     JoyInfo.Y = uint_to_long(JoyInfo.Yin)
  106.     JoyInfo.Z = uint_to_long(JoyInfo.Zin)
  107.  
  108.     JoyInfo.ButtonDown(1) = (JoyInfo.ButtonStates And JOY_BUTTON1) = JOY_BUTTON1
  109.     JoyInfo.ButtonDown(2) = (JoyInfo.ButtonStates And JOY_BUTTON2) = JOY_BUTTON2
  110.     JoyInfo.ButtonDown(3) = (JoyInfo.ButtonStates And JOY_BUTTON3) = JOY_BUTTON3
  111.     JoyInfo.ButtonDown(4) = (JoyInfo.ButtonStates And JOY_BUTTON4) = JOY_BUTTON4
  112.  
  113. End Function
  114.  
  115. Function uint_to_long (uint As Integer) As Long
  116. '-------------------------------------------------------
  117. ' Convert and unsigned integer into a long integer.
  118. '-------------------------------------------------------
  119.     
  120.     uint_to_long = (CLng(uint) And &HFFFF&)
  121. End Function
  122.  
  123.