| Wheelchair Control Diploma Thesis, Author: Ondrej Vozda, FEEC BUT, 2013

- Main Page
- Packages
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
USBJoystick.cs
1 //*******************************************************************************
2 //****** www.trossenrobotics.com ******
3 //****** Email: alexw@trossenrobotics.com ******
4 //****** Program: USB Joystick Controller using DirectX DirectInput ******
5 //****** Version: 1.1 ******
6 //****** Created: 3/20/05 ported from 7/07/05 vb Express version ******
7 //****** Author: Alex Ward ******
8 //****** Usage: Free to use whenever, wherever, however ******
9 //****** you want, just give credit where ******
10 //****** credit is due. ******
11 //*******************************************************************************
12
13 using System;
14 using System.Collections.Generic;
15 using System.Text;
16 using Microsoft.DirectX;
17 using Microsoft.DirectX.DirectInput;
18
19
20 namespace CsharpUSBJoystick
21 {
29 [System.Runtime.Remoting.Contexts.Synchronization]
30 public class Fake_USBJoystick : ContextBoundObject
31 {
32 private Int32 x, y, z; //Coordinates
33
37 public Int32 X
38 { get { return x; } }
39
43 public Int32 Y
44 { get { return y; } }
45
49 public Int32 Z
50 { get { return z; } }
51
58 public void SetCoordinates(Int32 X, Int32 Y, Int32 Z)
59 {
60 x = X;
61 y = Y;
62 z = Z;
63 }
64
71 public Fake_USBJoystick(Int32 X, Int32 Y, Int32 Z)
72 {
73 x = X;
74 y = Y;
75 z = Z;
76 }
77 }
78
82 public class USB_Joystick
83 {
84
85 #region Member Variables
86
90 public enum JoystickButton : byte
91 {
95 BUTTON_TRIGGER = 1,
96
100 BUTTON_UPPER_TRIGGER_1 = 2,
101
105 BUTTON_UPPER_TRIGGER_2 = 3
106 };
107
108 private JoystickState _state = new JoystickState();
109 private JoystickState _stateold = new JoystickState();
110 private Device _applicationDevice = null;
111 private int _intThresholdMin;
112 private int _intThresholdMax;
113 private double _dblThresholdPerc;
114 private int _intRangeMin;
115 private int _intRangeMax;
116
117 #endregion
118
119
120 #region Constructor
121
130 public USB_Joystick(int rangeMin, int rangeMax, double thresholdPerc, System.Windows.Forms.Form form)
131 {
132 if (thresholdPerc < 0 | thresholdPerc > 0.99)
133 {
134 throw new ArgumentOutOfRangeException("Value must be between 0.0 and .99!");
135 }
136
137 _dblThresholdPerc = thresholdPerc;
138 _intRangeMin = rangeMin;
139 _intRangeMax = rangeMax;
140
141 if (!(InitDirectInput(form)))
142 {
143 throw new DirectXException("Couldn't intilize Joystick");
144 }
145
146 PresetValues();
147 }
148
149
157 public USB_Joystick(int rangeMin, int rangeMax, System.Windows.Forms.Form form) :
158 this(rangeMin, rangeMax, .10, form) { }
159
160
166 public USB_Joystick(System.Windows.Forms.Form form) : this(0, 1000, .10, form) { }
167
168 #endregion
169
170
171 #region Properties
172
176 public double ThresholdPerc
177 {
178 get
179 {
180 return _dblThresholdPerc;
181 }
182 set
183 {
184 _dblThresholdPerc = value;
185 SetThreshold();
186 }
187 }
188
189
193 public int ThresholdMin
194 {
195 get
196 {
197 return _intThresholdMin;
198 }
199 }
200
201
205 public JoystickState State
206 {
207 get
208 {
209 return _state;
210 }
211 }
212
213
217 public int ThresholdMax
218 {
219 get
220 {
221 return _intThresholdMax;
222 }
223 }
224
225
229 public System.Drawing.PointF GetRawXYPerc
230 {
231 get
232 {
233 return new System.Drawing.PointF(((float)(_state.Y - _intRangeMin) / (_intRangeMax - _intRangeMin)),
234 ((float)(_state.Z - _intRangeMin) / (_intRangeMax - _intRangeMin)));
235 }
236 }
237
238
242 public int NumberOfButtons
243 {
244 get
245 {
246 return _applicationDevice.Caps.NumberButtons;
247 }
248 }
249
250 #endregion
251
252
257 public string ButtonValue(int index)
258 {
259 return _state.GetButtons().GetValue(index).ToString();
260 }
261
262
267 public bool ButtonPressed(int index)
268 {
269 byte[] buttons = _state.GetButtons();
270
271 index--;
272
273 if (index > buttons.Length)
274 return false;
275
276 return buttons[index] != 0;
277 }
278
284 public bool ButtonClicked(int index)
285 {
286 byte[] buttons = _state.GetButtons();
287 byte[] buttonsold = _stateold.GetButtons();
288
289 index--;
290
291 if (index > buttons.Length)
292 return false;
293
294 if (index > buttonsold.Length)
295 return false;
296
297 return (buttons[index] != 0) && (buttonsold[index] == 0);
298 }
299
300
304 public bool IsXOutsideThreshold()
305 {
306 if (((_state.X > _intThresholdMax) | (_state.X < _intThresholdMin)))
307 {
308 return true;
309 }
310 else
311 {
312 return false;
313 }
314 }
315
316
320 public bool IsYOutsideThreshold()
321 {
322 if (((_state.Y > _intThresholdMax) | (_state.Y < _intThresholdMin)))
323 {
324 return true;
325 }
326 else
327 {
328 return false;
329 }
330 }
331
332
336 public bool IsXYInsideThreshold
337 {
338 get
339 {
340 if ((!(IsXOutsideThreshold())) & (!(IsYOutsideThreshold())))
341 {
342 return true;
343 }
344 else
345 {
346 return false;
347 }
348 }
349 }
350
351
355 private void SetThreshold()
356 {
357 _intThresholdMax = Convert.ToInt32((((_intRangeMax - _intRangeMin) *
358 (_dblThresholdPerc / 2)) + (((_intRangeMax - _intRangeMin) / 2) + _intRangeMin)));
359
360 _intThresholdMin = Convert.ToInt32((((_intRangeMax - _intRangeMin) / 2) + _intRangeMin) -
361 ((_intRangeMax - _intRangeMin) * (_dblThresholdPerc / 2)));
362 }
363
364
369 public void GetData()
370 {
371 if (null == _applicationDevice)
372 {
373 return;
374 }
375 try
376 {
377 _applicationDevice.Poll();
378 }
379 catch (InputException inputex)
380 {
381 if (inputex is NotAcquiredException | inputex is InputLostException)
382 {
383 try
384 {
385 _applicationDevice.Acquire();
386 }
387 catch
388 {
389 return;
390 }
391 }
392 }
393 try
394 {
395 _stateold = _state;
396 _state = _applicationDevice.CurrentJoystickState;
397 }
398 catch
399 {
400 return;
401 }
402 }
403
404
411 private bool InitDirectInput(System.Windows.Forms.Form Form1)
412 {
413 foreach (DeviceInstance instance in
414 Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly))
415 {
416 _applicationDevice = new Device(instance.InstanceGuid);
417 break;
418 }
419
420 if ((_applicationDevice == null))
421 {
422 throw new DirectXException("Unable to initialize the joystick device.", new Exception(
423 "No joystick or compatible joystick was was found attached to the computer."));
424 }
425
426 _applicationDevice.SetDataFormat(DeviceDataFormat.Joystick);
427 _applicationDevice.SetCooperativeLevel(Form1,
428 CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Background);
429
430 foreach (DeviceObjectInstance d in _applicationDevice.Objects)
431 {
432 if (0 != (d.ObjectId & System.Convert.ToInt32(DeviceObjectTypeFlags.Axis)))
433 {
434 _applicationDevice.Properties.SetRange(ParameterHow.ById, d.ObjectId,
435 new InputRange(_intRangeMin, _intRangeMax));
436 }
437 }
438 return true;
439 }
440
441
445 public void PresetValues()
446 {
447 SetThreshold();
448 }
449
450
454 public void UnacquireAppDevice()
455 {
456 if (!(null == _applicationDevice))
457 {
458 _applicationDevice.Unacquire();
459 }
460 }
461
462
466 public void Dispose()
467 {
468 UnacquireAppDevice();
469 _applicationDevice.Dispose();
470 }
471 }
472 }