Skip to content

Commit 7194e1b

Browse files
committed
Build HID Report Dynamically
Currently only creates 32 buttons.
1 parent 46390fc commit 7194e1b

File tree

1 file changed

+82
-19
lines changed

1 file changed

+82
-19
lines changed

Joystick/src/Joystick.cpp

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
#if defined(_USING_DYNAMIC_HID)
2424

25-
#define JOYSTICK_STATE_SIZE 13
25+
//#define JOYSTICK_STATE_SIZE 13
26+
#define JOYSTICK_STATE_SIZE 4
2627

2728
#define JOYSTICK_REPORT_ID_INDEX 7
2829

@@ -101,16 +102,76 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
101102

102103
Joystick_::Joystick_(uint8_t hidReportId)
103104
{
104-
// Customize HID report
105-
int hidReportDescriptorSize = sizeof(_hidReportDescriptor);
106-
105+
// Set the USB HID Report ID
106+
_hidReportId = hidReportId;
107+
108+
// Customize HID report
109+
//int hidReportDescriptorSize = sizeof(_hidReportDescriptor);
110+
111+
int hidReportDescriptorSize = 29;
112+
uint8_t *customHidReportDescriptor = new uint8_t[hidReportDescriptorSize];
113+
114+
// USAGE_PAGE (Generic Desktop)
115+
customHidReportDescriptor[0] = 0x05;
116+
customHidReportDescriptor[1] = 0x01;
117+
118+
// USAGE (Joystick)
119+
customHidReportDescriptor[2] = 0x09;
120+
customHidReportDescriptor[3] = 0x04;
121+
122+
// COLLECTION (Application)
123+
customHidReportDescriptor[4] = 0xa1;
124+
customHidReportDescriptor[5] = 0x01;
125+
126+
// REPORT_ID (Default: 3)
127+
customHidReportDescriptor[6] = 0x85;
128+
customHidReportDescriptor[7] = _hidReportId;
129+
130+
// USAGE_PAGE (Button)
131+
customHidReportDescriptor[8] = 0x05;
132+
customHidReportDescriptor[9] = 0x09;
133+
134+
// USAGE_MINIMUM (Button 1)
135+
customHidReportDescriptor[10] = 0x19;
136+
customHidReportDescriptor[11] = 0x01;
137+
138+
// USAGE_MAXIMUM (Button 32)
139+
customHidReportDescriptor[12] = 0x29;
140+
customHidReportDescriptor[13] = 0x20;
141+
142+
// LOGICAL_MINIMUM (0)
143+
customHidReportDescriptor[14] = 0x15;
144+
customHidReportDescriptor[15] = 0x00;
145+
146+
// LOGICAL_MAXIMUM (1)
147+
customHidReportDescriptor[16] = 0x25;
148+
customHidReportDescriptor[17] = 0x01;
149+
150+
// REPORT_SIZE (1)
151+
customHidReportDescriptor[18] = 0x75;
152+
customHidReportDescriptor[19] = 0x01;
153+
154+
// REPORT_COUNT (32)
155+
customHidReportDescriptor[20] = 0x95;
156+
customHidReportDescriptor[21] = 0x20;
157+
158+
// UNIT_EXPONENT (0)
159+
customHidReportDescriptor[22] = 0x55;
160+
customHidReportDescriptor[23] = 0x00;
161+
162+
// UNIT (None)
163+
customHidReportDescriptor[24] = 0x65;
164+
customHidReportDescriptor[25] = 0x00;
165+
166+
// INPUT (Data,Var,Abs)
167+
customHidReportDescriptor[26] = 0x81;
168+
customHidReportDescriptor[27] = 0x02;
169+
170+
// END_COLLECTION
171+
customHidReportDescriptor[28] = 0xc0;
172+
107173
// Create a copy of the HID Report Descriptor template
108-
uint8_t *customHidReportDescriptor = new uint8_t[hidReportDescriptorSize];
109-
memcpy_P(customHidReportDescriptor, _hidReportDescriptor, hidReportDescriptorSize);
110-
111-
// Set the USB HID Report ID
112-
_hidReportId = hidReportId;
113-
customHidReportDescriptor[JOYSTICK_REPORT_ID_INDEX] = _hidReportId;
174+
// memcpy_P(customHidReportDescriptor, _hidReportDescriptor, hidReportDescriptorSize);
114175

115176
// Setup HID report structure
116177
_node = new DynamicHIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize, false);
@@ -225,10 +286,11 @@ void Joystick_::sendState()
225286
buttonTmp >>= 8;
226287
data[3] = buttonTmp & 0xFF;
227288

228-
data[4] = _throttle;
229-
data[5] = _rudder;
289+
// data[4] = _throttle;
290+
// data[5] = _rudder;
230291

231292
// Calculate hat-switch values
293+
/*
232294
uint8_t convertedHatSwitch[2];
233295
for (int hatSwitchIndex = 0; hatSwitchIndex < 2; hatSwitchIndex++)
234296
{
@@ -241,17 +303,18 @@ void Joystick_::sendState()
241303
convertedHatSwitch[hatSwitchIndex] = (_hatSwitch[hatSwitchIndex] % 360) / 45;
242304
}
243305
}
306+
*/
244307

245308
// Pack hat-switch states into a single byte
246-
data[6] = (convertedHatSwitch[1] << 4) | (B00001111 & convertedHatSwitch[0]);
309+
//data[6] = (convertedHatSwitch[1] << 4) | (B00001111 & convertedHatSwitch[0]);
247310

248-
data[7] = _xAxis + 127;
249-
data[8] = _yAxis + 127;
250-
data[9] = _zAxis + 127;
311+
//data[7] = _xAxis + 127;
312+
//data[8] = _yAxis + 127;
313+
//data[9] = _zAxis + 127;
251314

252-
data[10] = (_xAxisRotation % 360) * 0.708;
253-
data[11] = (_yAxisRotation % 360) * 0.708;
254-
data[12] = (_zAxisRotation % 360) * 0.708;
315+
//data[10] = (_xAxisRotation % 360) * 0.708;
316+
//data[11] = (_yAxisRotation % 360) * 0.708;
317+
//data[12] = (_zAxisRotation % 360) * 0.708;
255318

256319
// HID().SendReport(Report number, array of values in same order as HID descriptor, length)
257320
DynamicHID().SendReport(_hidReportId, data, JOYSTICK_STATE_SIZE);

0 commit comments

Comments
 (0)