USBJoystick
USBJoystick.h
1 // USBJoystick.h
2 //
3 // USB Interface to USB joystick/game pad, such as Logitech 'Dual Action' game pad
4 ///
5 /// \mainpage Arduino USB Interface to USB joystick/game pad
6 ///
7 /// This is the USBJoystick library.
8 /// It provides an Arduino library and class for reading input from a
9 /// USB joystick/game pad, such as Logitech 'Dual Action' game pad.
10 /// It can be subclassed to get control when new input values are read, or when they actually change.
11 /// Alternatively, you can set up callback functions to be called
12 /// when new input values are read, or when they actually change.
13 ///
14 /// The version of the package that this documentation refers to can be downloaded
15 /// from http://www.airspayce.com/mikem/arduino/USBJoystick/USBJoystick-1.2.zip
16 /// You can find the latest version at http://www.airspayce.com/mikem/arduino/USBJoystick
17 ///
18 /// \par Prerequisites
19 ///
20 /// Requires:
21 /// - SparkFun USB Host Shield
22 /// - USB_Host_Shield library. Install this in your libraries folder with a command like:
23 /// \code
24 /// git clone git://github.com/felis/USB_Host_Shield.git
25 /// \endcode
26 ///
27 /// This library does not steal any timers or add any interrupt handlers.
28 ///
29 /// \par Example programs
30 ///
31 /// Example Arduino programs are included to show the main modes of use.
32 ///
33 /// The following example programs are provided:
34 /// - usbjoystick_test: Simple demonstration that prints to Serial when input changes
35 ///
36 /// \par Installation
37 ///
38 /// Install in the usual way: unzip the distribution zip file to the libraries
39 /// sub-folder of your sketchbook.
40 ///
41 /// If you are using the older Sparkfun DEV-09628 USB Host Shield, you will need to alter the
42 /// pin definitions in Max3421e_constants.h in the USB_Host_Shield library as discussed in
43 /// http://www.sparkfun.com/products/9628
44 ///
45 /// If you require the USB Host Shield to be installed with WiShield, expect pin conflicts. These can be resolved by
46 /// Bending the USB Host Shield pins D8, D9 and D10 out of the way and then jumpering Host Shield D10 to D6,
47 /// Host Shield D9 to D3, Host Shield D8 to D5, then changing the settings in Max3421e_constants.h like this
48 /// (for the older Sparkfun DEV-09628 USB Host Shield). See <a href="IMG_8837.JPG">this photo</a> showing Duemilanove,
49 /// WiShield and USB Host Shield stack.
50 /// \code
51 /// // For USB Host Shield DEV-09628 interop with WiShield
52 /// #define MAX_SS 6
53 /// #define MAX_INT 3
54 /// #define MAX_GPX 7
55 /// #define MAX_RESET 5
56 /// \endcode
57 ///
58 /// This library has been tested with Duemilanove and SparkFun USB Host Shield DEV-09628, with the
59 /// Logitech 'Dual Action' USB Game Pad P/N 863247-0010 compiled with arduino-0021
60 /// on OpenSuSE 11.1 and avr-libc-1.6.1-1.15,
61 /// cross-avr-binutils-2.19-9.1, cross-avr-gcc-4.1.3_20080612-26.5.
62 ///
63 /// \author Mike McCauley (mikem@airspayce.com)
64 ///
65 /// This software is Copyright (C) 2011 Mike McCauley. Use is subject to license
66 /// conditions. The main licensing options available are GPL V2 or Commercial:
67 ///
68 /// \par Open Source Licensing GPL V2
69 /// This is the appropriate option if you want to share the source code of your
70 /// application with everyone you distribute it to, and you also want to give them
71 /// the right to share who uses it. If you wish to use this software under Open
72 /// Source Licensing, you must contribute all your source code to the open source
73 /// community in accordance with the GPL Version 2 when your application is
74 /// distributed. See http://www.gnu.org/copyleft/gpl.html
75 ///
76 /// \par Commercial Licensing
77 /// This is the appropriate option if you are creating proprietary applications
78 /// and you are not prepared to distribute and share the source code of your
79 /// application. Contact info@airspayce.com for details.
80 ///
81 /// \par Revision History
82 ///
83 /// \version 1.0 Initial release
84 /// \version 1.1 Compiles under Arduino 1.0 PROVIDED: you modify (or update)
85 /// the USB_Host_Shield library so it builds under 1.0 too. At the time of writing there
86 /// was no update to USB_Host_Shield available for 1.0.
87 /// \version 1.2 Updated author and distribution location details to airspayce.com
88 
89 // Copyright (C) 2010 Mike McCauley
90 // $Id: USBJoystick.h,v 1.2 2012/01/19 00:09:54 mikem Exp mikem $
91 
92 #ifndef USBJoystick_h
93 #define USBJoystick_h
94 
95 #include <Usb.h>
96 #include <inttypes.h>
97 #include "Max3421e_constants.h"
98 
99 /////////////////////////////////////////////////////////////////////
100 /// \class USBJoystick USBJoystick.h <USBJoystick.h>
101 /// \brief USB Interface to USB joystick/game pad, such as Logitech 'Dual Action' game pad
102 ///
103 /// \par Overview
104 ///
105 /// A class that interfaces with a USB Joystick or game pad.
106 /// It can be subclassed to get control when new input values are read, or when they actually change.
107 /// Alternatively, you can set up callback functions to be called
108 /// when new input values are read, or when they actually change.
109 ///
110 /// Input data such as stick positions, button presses and hat-switch directions are read from the device
111 /// and delivered to the calling
112 /// application.
113 ///
114 /// USBJoystick uses the USB_Host_Shield library to initialise and interrogate a USB joystick or game pad.
115 /// When the task() function is called, an attempt to read new values from the joystick is made every
116 /// USBJOYSTICK_POLL_INTERVAL milliseconds.
117 /// If successful the NewValue
118 /// virtual functions are called. These in turn call the ValueDidChange virtual functions if the value
119 /// changed since last poll. After the poll the new values can also be read with the data accessor functions.
120 ///
121 /// The virtual functions will call their respective callbacks if callback functions have been set up.
122 ///
123 /// Therefore developers have several choices for getting input data from a device:
124 /// - subclass USBJoystick and override the *NewValue() and/or *ValueDidChange() functions.
125 /// - set callback functions with set*NewValueCallback() and set*ValueDidChangeCallback() functions
126 /// - Asynchronously read current values with the *Value() functions.
127 ///
128 /// \par Input indexes
129 ///
130 /// The joystick/game pad is considered to have a number of joysticks, buttons and hat switches. In order
131 /// to identifiy which joysticks, buttons or hat switch is being referred to in callbacks etc, an index number is used.
132 /// Macros can be used to refer to specific items by name, see the USBJOYSTICK_STICK_*, USBJOYSTICK_BUTTON_* and
133 /// USBJOYSTICK_HAT_* macros. The Logitech Dula Action game pad has 4 sticks
134 /// (really 2 physical sticks with vertical and horizontal movement), 13 buttons and 1 hat switch.
135 ///
136 /// \par Input Device Values
137 ///
138 /// Stick positions are represented as 8 bit unsigned integers:
139 /// - 0 is fully left or up
140 /// - 255 is fully right or down
141 ///
142 /// Button positions are represented by booleans:
143 /// - true - button is pressed (in the case of MODE button, mode is enabled and the red light is on)
144 /// - false - otherwise
145 ///
146 /// Hat switch positions are represented as 8 bit unsigned integers.
147 /// The current hat position is one of USBJOYSTICK_HAT_POS_*.
149 {
150 
151 #define USBJOYSTICK_NUM_STICKS 4
152 #define USBJOYSTICK_NUM_BUTTONS 13
153 #define USBJOYSTICK_NUM_HATS 1
154 
155 /// These are valid values for the stick index in callbacks etc
156 #define USBJOYSTICK_STICK_LEFT_HORIZ 0
157 #define USBJOYSTICK_STICK_LEFT_VERT 1
158 #define USBJOYSTICK_STICK_RIGHT_HORIZ 2
159 #define USBJOYSTICK_STICK_RIGHT_VERT 3
160 
161 /// These are valid values for the button index in callbacks etc
162 #define USBJOYSTICK_BUTTON_MODE 0
163 #define USBJOYSTICK_BUTTON_1 1
164 #define USBJOYSTICK_BUTTON_2 2
165 #define USBJOYSTICK_BUTTON_3 3
166 #define USBJOYSTICK_BUTTON_4 4
167 #define USBJOYSTICK_BUTTON_5 5
168 #define USBJOYSTICK_BUTTON_6 6
169 #define USBJOYSTICK_BUTTON_7 7
170 #define USBJOYSTICK_BUTTON_8 8
171 #define USBJOYSTICK_BUTTON_9 9
172 #define USBJOYSTICK_BUTTON_10 10
173 #define USBJOYSTICK_BUTTON_LEFT_STICK 11
174 #define USBJOYSTICK_BUTTON_RIGHT_STICK 12
175 
176 // These are valid values for the hat index in callbacks etc
177 #define USBJOYSTICK_HAT_1 0
178 
179 // These are valid values for the hat value in callbacks etc
180 #define USBJOYSTICK_HAT_POS_N 0
181 #define USBJOYSTICK_HAT_POS_NE 1
182 #define USBJOYSTICK_HAT_POS_E 2
183 #define USBJOYSTICK_HAT_POS_SE 3
184 #define USBJOYSTICK_HAT_POS_S 4
185 #define USBJOYSTICK_HAT_POS_SW 5
186 #define USBJOYSTICK_HAT_POS_W 6
187 #define USBJOYSTICK_HAT_POS_NW 7
188 #define USBJOYSTICK_HAT_POS_IDLE 8
189 
190 // Milliseconds
191 #define USBJOYSTICK_POLL_INTERVAL 50
192 
193 public:
194  /// Constructor.
195  /// Stick positions are initialised to 0x80, buttons to false and hat switches to idle
196  USBJoystick();
197 
198  /// Library initialisation.
199  /// Must be called once during setup()
200  /// Enables the USB hardware and delays 200msec
201  void init();
202 
203  /// USB Device regular poll function.
204  /// Attempts to read current data from the USB device.
205  /// If successful, calls the *NewValue() and/or *ValueDidChange() functions, and any calbacks if so configured.
206  /// This must be called as often as possible in your main loop().
207  /// Calls the intenal poll() functi0on every USBJOYSTICK_POLL_INTERVAL milliseconds
208  void run();
209 
210  /// Read the current value of a joystick
211  /// \param[in] stick The stick index. One of USBJOYSTICK_STICK_*
212  /// \return the most recently read stick position value
213  uint8_t stickValue(uint8_t stick);
214 
215  /// Read the current value of a button
216  /// param[in] button The button index. One of USBJOYSTICK_BUTTON_*
217  /// \return the most recently read button position value
218  boolean buttonValue(uint8_t button);
219 
220  /// Read the current value of a joystick
221  /// \param[in] hat The stick index. One of USBJOYSTICK_HAT_*
222  /// \return the most recently read hat position value, one of USBJOYSTICK_HAT_POS_*
223  uint8_t hatValue(uint8_t hat);
224 
225  /// Set a callback to be called when a stick value is read from
226  /// the usb device. Overrides any previously set callback. May be called at any time
227  /// to set or change the callback.
228  /// \param[in] cb The new callback function pointer
229  void setStickNewValueCallback(void (*cb)(uint8_t stick, uint8_t value));
230 
231  /// Set a callback to be called when a button value is read from
232  /// the usb device. Overrides any previously set callback. May be called at any time
233  /// to set or change the callback.
234  /// \param[in] cb The new callback function pointer
235  void setButtonNewValueCallback(void (*cb)(uint8_t button, uint8_t value));
236 
237  /// Set a callback to be called when a hat value is read from
238  /// the usb device. Overrides any previously set callback. May be called at any time
239  /// to set or change the callback.
240  /// \param[in] cb The new callback function pointer
241  void setHatNewValueCallback(void (*cb)(uint8_t hat, uint8_t value));
242 
243  /// Set a callback to be called when a stick value is read from
244  /// the usb device and the value has changed since the last poll.
245  /// Overrides any previously set callback. May be called at any time
246  /// to set or change the callback.
247  /// \param[in] cb The new callback function pointer
248  void setStickValueDidChangeCallback(void (*cb)(uint8_t stick, uint8_t value));
249 
250  /// Set a callback to be called when a button value is read from
251  /// the usb device and the value has changed since the last poll.
252  /// Overrides any previously set callback. May be called at any time
253  /// to set or change the callback.
254  /// \param[in] cb The new callback function pointer
255  void setButtonValueDidChangeCallback(void (*cb)(uint8_t button, uint8_t value));
256 
257  /// Set a callback to be called when a hat value is read from
258  /// the usb device and the value has changed since the last poll.
259  /// Overrides any previously set callback. May be called at any time
260  /// to set or change the callback.
261  /// \param[in] cb The new callback function pointer
262  void setHatValueDidChangeCallback(void (*cb)(uint8_t hat, uint8_t value));
263 
264 protected:
265 
266  /// Initialises the USB hardware.
267  /// Internal use: should not be called directly.
268  /// \return true if initialisation was successful
269  boolean device_init();
270 
271  /// Polls the USB device for a new reading.
272  /// Internal use: should not be called directly.
273  void poll();
274 
275  /// This virtual function is called when a new stick value is read from the USB device
276  /// Default implementation calls the apropriate callback (if set) then calls
277  /// stickValueDidChange() if the value changed since the last poll.
278  /// Subclasses may override this to change get control for each value read and change
279  /// the default behaviour
280  /// \param[in] stick The stick index, one of USBJOYSTICK_STICK_*
281  /// \param[in] value The new stick value
282  virtual void stickNewValue(uint8_t stick, uint8_t value);
283 
284  /// This virtual function is called when a new button value is read from the USB device
285  /// Default implementation calls the apropriate callback (if set) then calls
286  /// buttonValueDidChange() if the value changed since the last poll.
287  /// Subclasses may override this to change get control for each value read and change
288  /// the default behaviour
289  /// \param[in] button The button index, one of USBJOYSTICK_BUTTON_*
290  /// \param[in] value The new button value
291  virtual void buttonNewValue(uint8_t button, boolean value);
292 
293  /// This virtual function is called when a new hat switch value is read from the USB device
294  /// Default implementation calls the apropriate callback (if set) then calls
295  /// hatValueDidChange() if the value changed since the last poll.
296  /// Subclasses may override this to change get control for each value read and change
297  /// the default behaviour
298  /// \param[in] hat The hat switch index, one of USBJOYSTICK_HAT_*
299  /// \param[in] value The new hat position value
300  virtual void hatNewValue(uint8_t hat, uint8_t value);
301 
302  /// This virtual function is called when a stick value changes
303  /// Default implementation calls the apropriate callback (if set)
304  /// \param[in] stick The stick index, one of USBJOYSTICK_STICK_*
305  /// \param[in] value The new stick value
306  virtual void stickValueDidChange(uint8_t stick, uint8_t value);
307 
308  /// This virtual function is called when a button value changes
309  /// Default implementation calls the apropriate callback (if set)
310  /// \param[in] button The button index, one of USBJOYSTICK_BUTTON_*
311  /// \param[in] value The new button value
312  virtual void buttonValueDidChange(uint8_t button, boolean value);
313 
314  /// This virtual function is called when a hat switch value changes
315  /// Default implementation calls the apropriate callback (if set)
316  /// \param[in] hat The hat index, one of USBJOYSTICK_HAT_*
317  /// \param[in] value The new hat switch value
318  virtual void hatValueDidChange(uint8_t hat, uint8_t value);
319 
320 private:
321  // Stores current (last read) value for each stick and button
322  uint8_t _stick_value[USBJOYSTICK_NUM_STICKS];
323  boolean _button_value[USBJOYSTICK_NUM_BUTTONS];
324  uint8_t _hat_value[USBJOYSTICK_NUM_HATS];
325 
326  // Callback pointers
327  void (*_stick_new_value_cb)(uint8_t stick, uint8_t value);
328  void (*_button_new_value_cb)(uint8_t button, boolean value);
329  void (*_hat_new_value_cb)(uint8_t hat, uint8_t value);
330  void (*_stick_value_did_change_cb)(uint8_t stick, uint8_t value);
331  void (*_button_value_did_change_cb)(uint8_t button, boolean value);
332  void (*_hat_value_did_change_cb)(uint8_t hat, uint8_t value);
333 
334  // USB interface data
335  EP_RECORD ep_record[2];
336  uint8_t buf[8];
337 };
338 
339 #endif