Skip to content

Commit c91f721

Browse files
committed
implement USB-api from diff
1 parent 0ae9e3a commit c91f721

File tree

4 files changed

+385
-4
lines changed

4 files changed

+385
-4
lines changed

hardware/arduino/avr/cores/arduino/HID.cpp

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,27 @@
1616
** SOFTWARE.
1717
*/
1818

19+
#include "Platform.h"
1920
#include "USBAPI.h"
21+
#include "USBDesc.h"
2022

2123
#if defined(USBCON)
2224
#ifdef HID_ENABLED
2325

26+
// *** This used to be RAWHID_ENABLED
2427
//#define RAWHID_ENABLED
28+
//#define KBAM_ENABLED
29+
#define JOYHID_ENABLED
2530

2631
// Singletons for mouse and keyboard
2732

2833
Mouse_ Mouse;
2934
Keyboard_ Keyboard;
3035

36+
// *** Add a joystick too
37+
38+
Joystick_ Joystick;
39+
3140
//================================================================================
3241
//================================================================================
3342

@@ -123,6 +132,91 @@ const u8 _hidReportDescriptor[] = {
123132
0x09, 0x02, // usage
124133
0x91, 0x02, // Output (array)
125134
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+
126220
#endif
127221
};
128222

@@ -143,6 +237,7 @@ u8 _hid_idle = 1;
143237

144238
#define WEAK __attribute__ ((weak))
145239

240+
146241
int WEAK HID_GetInterface(u8* interfaceNum)
147242
{
148243
interfaceNum[0] += 1; // uses 1
@@ -195,12 +290,63 @@ bool WEAK HID_Setup(Setup& setup)
195290
return false;
196291
}
197292

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+
198343
//================================================================================
199344
//================================================================================
200345
// Mouse
201346

202347
Mouse_::Mouse_(void) : _buttons(0)
203348
{
349+
204350
}
205351

206352
void Mouse_::begin(void)

hardware/arduino/avr/cores/arduino/USBAPI.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@ class Serial_ : public Stream
6969
{
7070
private:
7171
int peek_buffer;
72+
ring_buffer *_cdc_rx_buffer;
7273
public:
7374
Serial_() { peek_buffer = -1; };
7475
void begin(unsigned long);
7576
void begin(unsigned long, uint8_t);
77+
void begin(uint16_t baud_count);
7678
void end(void);
7779

7880
virtual int available(void);
81+
virtual void accept(void);
7982
virtual int peek(void);
8083
virtual int read(void);
8184
virtual void flush(void);
@@ -90,7 +93,42 @@ class Serial_ : public Stream
9093
};
9194
extern Serial_ Serial;
9295

93-
#define HAVE_CDCSERIAL
96+
//================================================================================
97+
//================================================================================
98+
// Joystick
99+
// Implemented in HID.cpp
100+
// The list of parameters here needs to match the implementation in HID.cpp
101+
102+
103+
typedef struct JoyState // Pretty self explanitory. Simple state to store all the joystick parameters
104+
{
105+
uint8_t xAxis;
106+
uint8_t yAxis;
107+
uint8_t zAxis;
108+
109+
uint8_t xRotAxis;
110+
uint8_t yRotAxis;
111+
uint8_t zRotAxis;
112+
113+
uint8_t throttle;
114+
uint8_t rudder;
115+
116+
uint8_t hatSw1;
117+
uint8_t hatSw2;
118+
119+
uint32_t buttons; // 32 general buttons
120+
121+
} JoyState_t;
122+
123+
class Joystick_
124+
{
125+
public:
126+
Joystick_();
127+
128+
void setState(JoyState_t *joySt);
129+
130+
};
131+
extern Joystick_ Joystick;
94132

95133
//================================================================================
96134
//================================================================================
@@ -111,7 +149,7 @@ class Mouse_
111149
void begin(void);
112150
void end(void);
113151
void click(uint8_t b = MOUSE_LEFT);
114-
void move(signed char x, signed char y, signed char wheel = 0);
152+
void move(signed char x, signed char y, signed char wheel = 0);
115153
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
116154
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
117155
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default

0 commit comments

Comments
 (0)