16
16
** SOFTWARE.
17
17
*/
18
18
19
+ #include " Platform.h"
19
20
#include " USBAPI.h"
21
+ #include " USBDesc.h"
20
22
21
23
#if defined(USBCON)
22
24
#ifdef HID_ENABLED
23
25
26
+ // *** This used to be RAWHID_ENABLED
24
27
// #define RAWHID_ENABLED
28
+ // #define KBAM_ENABLED
29
+ #define JOYHID_ENABLED
25
30
26
31
// Singletons for mouse and keyboard
27
32
28
33
Mouse_ Mouse;
29
34
Keyboard_ Keyboard;
30
35
36
+ // *** Add a joystick too
37
+
38
+ Joystick_ Joystick;
39
+
31
40
// ================================================================================
32
41
// ================================================================================
33
42
@@ -123,6 +132,91 @@ const u8 _hidReportDescriptor[] = {
123
132
0x09 , 0x02 , // usage
124
133
0x91 , 0x02 , // Output (array)
125
134
0xC0 // end collection
135
+ #endif
136
+ // *** Here is where the RAW_HID has been converted to a Joystick device
137
+ // *** Inspired by helmpcb.com/electronics/usb-joystick
138
+ // *** Check out www.usb.org/developers/hidpage/ for more than you'll ever need to know about USB HID
139
+ // *** HID descriptor created using the HID descriptor tool from www.usb.org/developers/hidpage/dt2_4.zip (win32)
140
+
141
+ #ifdef JOYHID_ENABLED
142
+
143
+ // 32 buttons (and a throttle - just in case the game doesn't recognise a joystick with no analog axis)
144
+
145
+ 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
146
+ 0x09 , 0x04 , // USAGE (Joystick)
147
+ 0xa1 , 0x01 , // COLLECTION (Application)
148
+ 0x85 , 0x03 , // REPORT_ID (3) (This is important when HID_SendReport() is called)
149
+
150
+ // Buttons:
151
+ 0x05 , 0x09 , // USAGE_PAGE (Button)
152
+ 0x19 , 0x01 , // USAGE_MINIMUM (Button 1)
153
+ 0x29 , 0x20 , // USAGE_MAXIMUM (Button 32)
154
+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
155
+ 0x25 , 0x01 , // LOGICAL_MAXIMUM (1)
156
+ 0x75 , 0x01 , // REPORT_SIZE (1)
157
+ 0x95 , 0x20 , // REPORT_COUNT (32)
158
+ 0x55 , 0x00 , // UNIT_EXPONENT (0)
159
+ 0x65 , 0x00 , // UNIT (None)
160
+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
161
+
162
+ // 8 bit Throttle and Steering
163
+ 0x05 , 0x02 , // USAGE_PAGE (Simulation Controls)
164
+
165
+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
166
+ 0x26 , 0xff , 0x00 , // LOGICAL_MAXIMUM (255)
167
+ 0xA1 , 0x00 , // COLLECTION (Physical)
168
+ 0x09 , 0xBB , // USAGE (Throttle)
169
+ 0x09 , 0xBA , // USAGE (Steering)
170
+ 0x75 , 0x08 , // REPORT_SIZE (8)
171
+ 0x95 , 0x02 , // REPORT_COUNT (2)
172
+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
173
+
174
+ 0xc0 , // END_COLLECTION
175
+ // Two Hat switches
176
+
177
+ 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
178
+
179
+ 0x09 , 0x39 , // USAGE (Hat switch)
180
+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
181
+ 0x25 , 0x07 , // LOGICAL_MAXIMUM (7)
182
+ 0x35 , 0x00 , // PHYSICAL_MINIMUM (0)
183
+ 0x46 , 0x3B , 0x01 , // PHYSICAL_MAXIMUM (315)
184
+ 0x65 , 0x14 , // UNIT (Eng Rot:Angular Pos)
185
+ 0x75 , 0x04 , // REPORT_SIZE (4)
186
+ 0x95 , 0x01 , // REPORT_COUNT (1)
187
+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
188
+
189
+ 0x09 , 0x39 , // USAGE (Hat switch)
190
+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
191
+ 0x25 , 0x07 , // LOGICAL_MAXIMUM (7)
192
+ 0x35 , 0x00 , // PHYSICAL_MINIMUM (0)
193
+ 0x46 , 0x3B , 0x01 , // PHYSICAL_MAXIMUM (315)
194
+ 0x65 , 0x14 , // UNIT (Eng Rot:Angular Pos)
195
+ 0x75 , 0x04 , // REPORT_SIZE (4)
196
+ 0x95 , 0x01 , // REPORT_COUNT (1)
197
+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
198
+
199
+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
200
+ 0x26 , 0xff , 0x00 , // LOGICAL_MAXIMUM (255)
201
+ 0x75 , 0x08 , // REPORT_SIZE (8)
202
+
203
+ 0x09 , 0x01 , // USAGE (Pointer)
204
+ 0xA1 , 0x00 , // COLLECTION (Physical)
205
+ 0x09 , 0x30 , // USAGE (x)
206
+ 0x09 , 0x31 , // USAGE (y)
207
+ 0x09 , 0x32 , // USAGE (z)
208
+ 0x09 , 0x33 , // USAGE (rx)
209
+ 0x09 , 0x34 , // USAGE (ry)
210
+ 0x09 , 0x35 , // USAGE (rz)
211
+ 0x95 , 0x06 , // REPORT_COUNT (2)
212
+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
213
+ 0xc0 , // END_COLLECTION
214
+
215
+ 0xc0 // END_COLLECTION
216
+
217
+
218
+
219
+
126
220
#endif
127
221
};
128
222
@@ -143,6 +237,7 @@ u8 _hid_idle = 1;
143
237
144
238
#define WEAK __attribute__ ((weak))
145
239
240
+
146
241
int WEAK HID_GetInterface(u8* interfaceNum)
147
242
{
148
243
interfaceNum[0 ] += 1 ; // uses 1
@@ -195,12 +290,63 @@ bool WEAK HID_Setup(Setup& setup)
195
290
return false ;
196
291
}
197
292
293
+ // ================================================================================
294
+ // ================================================================================
295
+ // Joystick
296
+ // Usage: Joystick.move(inputs go here)
297
+ //
298
+ // The report data format must match the one defined in the descriptor exactly
299
+ // or it either won't work, or the pc will make a mess of unpacking the data
300
+ //
301
+
302
+ Joystick_::Joystick_ ()
303
+ {
304
+ }
305
+
306
+
307
+ #define joyBytes 13 // should be equivalent to sizeof(JoyState_t)
308
+
309
+ void Joystick_::setState (JoyState_t *joySt)
310
+ {
311
+ uint8_t data[joyBytes];
312
+ uint32_t buttonTmp;
313
+ buttonTmp = joySt->buttons ;
314
+
315
+ data[0 ] = buttonTmp & 0xFF ; // Break 32 bit button-state out into 4 bytes, to send over USB
316
+ buttonTmp >>= 8 ;
317
+ data[1 ] = buttonTmp & 0xFF ;
318
+ buttonTmp >>= 8 ;
319
+ data[2 ] = buttonTmp & 0xFF ;
320
+ buttonTmp >>= 8 ;
321
+ data[3 ] = buttonTmp & 0xFF ;
322
+
323
+ data[4 ] = joySt->throttle ; // Throttle
324
+ data[5 ] = joySt->rudder ; // Steering
325
+
326
+ data[6 ] = (joySt->hatSw2 << 4 ) | joySt->hatSw1 ; // Pack hat-switch states into a single byte
327
+
328
+ data[7 ] = joySt->xAxis ; // X axis
329
+ data[8 ] = joySt->yAxis ; // Y axis
330
+ data[9 ] = joySt->zAxis ; // Z axis
331
+ data[10 ] = joySt->xRotAxis ; // rX axis
332
+ data[11 ] = joySt->yRotAxis ; // rY axis
333
+ data[12 ] = joySt->zRotAxis ; // rZ axis
334
+
335
+ // HID_SendReport(Report number, array of values in same order as HID descriptor, length)
336
+ HID_SendReport (3 , data, joyBytes);
337
+ // The joystick is specified as using report 3 in the descriptor. That's where the "3" comes from
338
+ }
339
+
340
+
341
+
342
+
198
343
// ================================================================================
199
344
// ================================================================================
200
345
// Mouse
201
346
202
347
Mouse_::Mouse_ (void ) : _buttons(0 )
203
348
{
349
+
204
350
}
205
351
206
352
void Mouse_::begin (void )
0 commit comments