home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code1 / joystick / calibrat.bas next >
BASIC Source File  |  1993-09-05  |  6KB  |  236 lines

  1. DefInt A-Z
  2.  
  3. Sub calibrate (thisform As Form)
  4. '***********************************
  5. ' calibrate the joystick to a form
  6. '***********************************
  7. Static busy As Integer
  8.  
  9. On Error GoTo errorhandler
  10. If busy = True Then Exit Sub
  11. busy = True
  12. FORM2.Show
  13. restart:
  14. ' lower right
  15. FORM2.Label1.Caption = "Move the joystick to the lower right position and press a button"
  16. FORM2.Picture1(1).Visible = False
  17. FORM2.Picture1(3).Visible = True
  18. 'wait for button
  19. Do
  20.     dummy = DoEvents()
  21. Loop While button_mark = False
  22. button_mark = False
  23. ymax = 20000
  24. xmax = 20000
  25.  
  26. '*******************************************
  27. ' If the x and y values do not convert to  *
  28. ' values that exactly match the current    *
  29. ' corner, adjust xmax and ymax so that     *
  30. ' x and y values convert to correct value  *
  31. '*******************************************
  32.  
  33.  
  34. Do
  35.     
  36.     convert calx, ymax, thisform
  37.     If winpoint.x > winxmax Then
  38.         If winpoint.x - winxmax > 100 Then
  39.             xmax = xmax + 100
  40.         Else
  41.             xmax = xmax + 1
  42.         End If
  43.     ElseIf winpoint.x < winxmax Then
  44.         If winxmax - winpoint.x > 100 Then
  45.             xmax = xmax - 100
  46.         Else
  47.             xmax = xmax - 1
  48.         End If
  49.     End If
  50. Loop While winpoint.x <> winxmax
  51.  
  52. Do
  53.     convert xmax, caly, thisform
  54.     'Debug.Print caly, ymax, winpoint.y, winymax
  55.     If winpoint.y > winymax Then
  56.         If winpoint.y - winymax > 100 Then
  57.             ymax = ymax + 100
  58.         Else
  59.             ymax = ymax + 1
  60.         End If
  61.     ElseIf winpoint.y < winymax Then
  62.         If winymax - winpoint.y > 100 Then
  63.             ymax = ymax - 100
  64.         Else
  65.             ymax = ymax - 1
  66.         End If
  67.     End If
  68. Loop While winpoint.y <> winymax
  69.  
  70. ' lower left
  71. FORM2.Label1.Caption = "Move the joystick to the lower left position and press a button"
  72. FORM2.Picture1(3).Visible = False
  73. FORM2.Picture1(2).Visible = True
  74. Do
  75.     dummy = DoEvents()
  76. Loop While button_mark = False
  77. button_mark = False
  78. xmin = 0
  79. Do
  80.     convert calx, ymax, thisform
  81.     If winpoint.x < winxmin Then
  82.         If winxmax - winpoint.x > 100 Then
  83.             xmax = xmax - 100
  84.         Else
  85.             xmax = xmax - 1
  86.         End If
  87.         
  88.     ElseIf winpoint.x > winxmin Then
  89.         If winpoint.x - winxmin > 100 Then
  90.             xmax = xmax + 100
  91.         Else
  92.             xmax = xmax + 1
  93.         End If
  94.     End If
  95. Loop While winpoint.x <> winxmin
  96.  
  97. If caly < ymax Then
  98.     Do
  99.         convert xmin, caly, thisform
  100.         If winpoint.y > winymax Then
  101.             If winpoint.y - winymax > 100 Then
  102.                 ymax = ymax + 100
  103.             Else
  104.                 ymax = ymax + 1
  105.             End If
  106.         ElseIf winpoint.y < winymax Then
  107.             If winymax - winpoint.y > 100 Then
  108.                 ymax = ymax - 100
  109.             Else
  110.                 ymax = ymax - 1
  111.             End If
  112.             
  113.         End If
  114.     Loop While winpoint.y <> winymax
  115. End If
  116.  
  117. button_mark = False
  118.  
  119.  
  120. ' upper left
  121. FORM2.Label1.Caption = "Move the joystick to the upper left position and press a button"
  122. FORM2.Picture1(2).Visible = False
  123. FORM2.Picture1(0).Visible = True
  124. Do
  125.     dummy = DoEvents()
  126. Loop While button_mark = False
  127. button_mark = False
  128. ymin = 0
  129. Do
  130.     convert calx, ymin, thisform
  131.     If winpoint.x < winxmin Then
  132.         If winxmin - winpoint.x > 100 Then
  133.             xmax = xmax - 100
  134.         Else
  135.             xmax = xmax - 1
  136.         End If
  137.     ElseIf winpoint.x > winxmin Then
  138.         If winpoint.x - winxmin > 100 Then
  139.             xmax = xmax + 100
  140.         Else
  141.             xmax = xmax + 1
  142.         End If
  143.     End If
  144. Loop While winpoint.x <> winxmin
  145. If caly < ymin Then
  146.     Do
  147.         convert xmin, caly, thisform
  148.         Debug.Print caly, ymin, winpoint.y, winymin
  149.         If winpoint.y > winymin Then
  150.             If winpoint.y - winymin > 100 Then
  151.                 ymin = ymin - 100
  152.             Else
  153.                 ymin = ymin - 1
  154.             End If
  155.         ElseIf winpoint.y < winymin Then
  156.             If winymin - winpoint.y > 100 Then
  157.                 ymin = ymin + 100
  158.             Else
  159.                 ymin = ymin + 1
  160.             End If
  161.         End If
  162.     Loop While winpoint.y <> winymin
  163. End If
  164.  
  165. ' upper right
  166. FORM2.Label1.Caption = "Move the joystick to the upper right position and press a button"
  167. FORM2.Picture1(0).Visible = False
  168. FORM2.Picture1(1).Visible = True
  169.  
  170. Do
  171.     dummy = DoEvents()
  172. Loop While button_mark = False
  173. button_mark = False
  174. If calx < xmax Then
  175.     Do
  176.         convert calx, ymin, thisform
  177.         If winpoint.x > winxmax Then
  178.             If winpoint.x - winxmax > 100 Then
  179.                 xmax = xmax + 100
  180.             Else
  181.                 xmax = xmax + 1
  182.             End If
  183.         ElseIf winpoint.x < winxmax Then
  184.             If winxmax - winpoint.x > 100 Then
  185.                 xmax = xmax - 100
  186.             Else
  187.                 xmax = xmax - 1
  188.             End If
  189.         End If
  190.     Loop While winpoint.x <> winxmax
  191. End If
  192.  
  193. If caly < ymin Then
  194.     Do
  195.         convert xmax, caly, thisform
  196.         If winpoint.y > winymin Then
  197.             If winpoint.y - winymin > 100 Then
  198.                 ymin = ymin - 100
  199.             Else
  200.                 ymin = ymin - 1
  201.             End If
  202.         ElseIf winpoint.y < winymin Then
  203.             If winymin - winpoint.y > 100 Then
  204.                 ymin = ymin + 100
  205.             Else
  206.                 ymin = ymin + 1
  207.             End If
  208.         End If
  209.     Loop While winpoint.y <> winymin
  210. End If
  211. FORM2.Picture1(3).Visible = False
  212.  
  213.  
  214.  
  215. busy = False
  216. need_calibrate = False
  217. On Error GoTo 0' disable error handler
  218. Unload FORM2
  219. Exit Sub
  220.  
  221. errorhandler:
  222. If Err = 6 Then 'Overflow-button pressed in wrong place
  223.     msg$ = "An error has occurred during Calibration." + Chr$(13) + Chr$(10)
  224.     msg$ = msg$ + "Calibration will now restart"
  225.     MsgBox msg$, 49, "Calibrating Joystick"
  226.     Resume restart
  227. Else
  228.     msg$ = "An unexpected error has occurred:" + Chr$(13) + Chr$(10)
  229.     msg$ = msg$ + Error$ + Chr$(13) + Chr$(10)
  230.     msg$ = msg$ + "The program will now terminate"
  231.     MsgBox msg$, 16, "Calibrating Joystick"
  232.     End
  233. End If
  234. End Sub
  235.  
  236.