Skip to content

Commit 1f6559f

Browse files
committed
Make X, Y, Z, Rx, Ry, and Rz Axis optional / configurable.
1 parent 3aa05a7 commit 1f6559f

File tree

3 files changed

+93
-72
lines changed

3 files changed

+93
-72
lines changed

Joystick/examples/MultipleJoystickTest/MultipleJoystickTest.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ void setup() {
108108
for (int index = 0; index < JOYSTICK_COUNT; index++)
109109
{
110110
Joystick[index].setXAxisRange(-127, 127);
111+
Joystick[index].setYAxisRange(-127, 127);
111112

112113
if (testAutoSendMode)
113114
{

Joystick/src/Joystick.cpp

Lines changed: 87 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222

2323
#if defined(_USING_DYNAMIC_HID)
2424

25-
//#define JOYSTICK_STATE_SIZE 13
26-
//#define JOYSTICK_STATE_SIZE 4
27-
2825
#define JOYSTICK_REPORT_ID_INDEX 7
2926
#define JOYSTICK_AXIS_MINIMUM -32767
3027
#define JOYSTICK_AXIS_MAXIMUM 32767
3128

29+
/*
3230
static const uint8_t _hidReportDescriptor[] PROGMEM = {
3331
3432
// Joystick
@@ -101,6 +99,7 @@ static const uint8_t _hidReportDescriptor[] PROGMEM = {
10199
102100
0xc0 // END_COLLECTION
103101
};
102+
*/
104103

105104
Joystick_::Joystick_(uint8_t hidReportId,
106105
uint8_t buttonCount,
@@ -124,13 +123,23 @@ Joystick_::Joystick_(uint8_t hidReportId,
124123
_includeRzAxis = includeRzAxis;
125124

126125
// Build Joystick HID Report Description
126+
127+
// Button Calculations
127128
uint8_t buttonsInLastByte = _buttonCount % 8;
128129
uint8_t buttonPaddingBits = 0;
129130
if (buttonsInLastByte > 0)
130131
{
131132
buttonPaddingBits = 8 - buttonsInLastByte;
132133
}
133-
134+
135+
// Axis Calculations
136+
uint8_t axisCount = (_includeXAxis == true)
137+
+ (_includeYAxis == true)
138+
+ (_includeZAxis == true)
139+
+ (_includeRxAxis == true)
140+
+ (_includeRyAxis == true)
141+
+ (_includeRzAxis == true);
142+
134143
// TODO: Figure out what the max for this could be and set it here...
135144
uint8_t *customHidReportDescriptor = new uint8_t[100];
136145
int hidReportDescriptorSize = 0;
@@ -211,66 +220,82 @@ Joystick_::Joystick_(uint8_t hidReportId,
211220

212221
} // Buttons
213222

214-
// USAGE_PAGE (Generic Desktop)
215-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
216-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
217-
218-
// USAGE (Pointer)
219-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
220-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
221-
222-
// LOGICAL_MINIMUM (-32767)
223-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x16;
224-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
225-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x80;
226-
227-
// LOGICAL_MAXIMUM (+32767)
228-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
229-
customHidReportDescriptor[hidReportDescriptorSize++] = 0xFF;
230-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x7F;
231-
232-
// REPORT_SIZE (16)
233-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
234-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x10;
235-
236-
// REPORT_COUNT (6)
237-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
238-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x06;
239-
240-
// COLLECTION (Physical)
241-
customHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
242-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
223+
if (axisCount > 0) {
224+
225+
// USAGE_PAGE (Generic Desktop)
226+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
227+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
243228

244-
// USAGE (X)
245-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
246-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x30;
229+
// USAGE (Pointer)
230+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
231+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
247232

248-
// USAGE (Y)
249-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
250-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x31;
233+
// LOGICAL_MINIMUM (-32767)
234+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x16;
235+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
236+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x80;
251237

252-
// USAGE (Z)
253-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
254-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x32;
238+
// LOGICAL_MAXIMUM (+32767)
239+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
240+
customHidReportDescriptor[hidReportDescriptorSize++] = 0xFF;
241+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x7F;
255242

256-
// USAGE (Rx)
257-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
258-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x33;
243+
// REPORT_SIZE (16)
244+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
245+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x10;
259246

260-
// USAGE (Ry)
261-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
262-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x34;
247+
// REPORT_COUNT (axisCount)
248+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
249+
customHidReportDescriptor[hidReportDescriptorSize++] = axisCount;
250+
251+
// COLLECTION (Physical)
252+
customHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
253+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
263254

264-
// USAGE (Rz)
265-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
266-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x35;
255+
if (_includeXAxis == true) {
256+
// USAGE (X)
257+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
258+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x30;
259+
}
267260

268-
// INPUT (Data,Var,Abs)
269-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
270-
customHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
271-
272-
// END_COLLECTION (Physical)
273-
customHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
261+
if (_includeYAxis == true) {
262+
// USAGE (Y)
263+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
264+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x31;
265+
}
266+
267+
if (_includeZAxis == true) {
268+
// USAGE (Z)
269+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
270+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x32;
271+
}
272+
273+
if (_includeRxAxis == true) {
274+
// USAGE (Rx)
275+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
276+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x33;
277+
}
278+
279+
if (_includeRyAxis == true) {
280+
// USAGE (Ry)
281+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
282+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x34;
283+
}
284+
285+
if (_includeRzAxis == true) {
286+
// USAGE (Rz)
287+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
288+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x35;
289+
}
290+
291+
// INPUT (Data,Var,Abs)
292+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
293+
customHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
294+
295+
// END_COLLECTION (Physical)
296+
customHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
297+
298+
} // X, Y, Z, Rx, Ry, and Rz Axis
274299

275300
// END_COLLECTION
276301
customHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
@@ -291,7 +316,10 @@ Joystick_::Joystick_(uint8_t hidReportId,
291316

292317
// Calculate HID Report Size
293318
_hidReportSize = _buttonValuesArraySize;
294-
_hidReportSize += (6 * 2);
319+
_hidReportSize += (axisCount * 2);
320+
321+
Serial.print("_hidReportSize: ");
322+
Serial.println(_hidReportSize);
295323

296324
// Initalize Joystick State
297325
_xAxis = 0;
@@ -541,20 +569,7 @@ void Joystick_::sendState()
541569
data[index++] = highByte;
542570
}
543571

544-
/*
545-
Serial.print("About to send report. _hidReportSize = ");
546-
Serial.print(_hidReportSize);
547-
Serial.print("; index = ");
548-
Serial.print(index);
549-
Serial.print("; xAxis = ");
550-
Serial.print(_xAxis);
551-
Serial.print("; _zAxisRotation = ");
552-
Serial.print(_zAxisRotation);
553-
Serial.print("; _zAxisRotation Mapped Value = ");
554-
Serial.println(axisValue);
555-
*/
556-
557572
DynamicHID().SendReport(_hidReportId, data, _hidReportSize);
558573
}
559574

560-
#endif
575+
#endif

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ Constructor used to initialize and setup the Joystick. The following optional pa
6262
- `bool includeRxAxis` - Default: `true` - Indicates if the X Axis Rotation (in some situations this is the right Y Axis) is available on the joystick.
6363
- `bool includeRyAxis` - Default: `true` - Indicates if the Y Axis Rotation is available on the joystick.
6464
- `bool includeRzAxis` - Default: `true` - Indicates if the Z Axis Rotation is available on the joystick.
65+
66+
The following constants define the default values for the constructor parameter's listed above:
67+
68+
- `JOYSTICK_DEFAULT_REPORT_ID` is set to `0x03`
69+
- `JOYSTICK_DEFAULT_BUTTON_COUNT` is set to `32`.
6570

6671
### Joystick.begin(bool initAutoSendState)
6772
Starts emulating a game controller connected to a computer. By default all methods update the game controller state immediately. If initAutoSendState is set to false, the Joystick.sendState method must be called to update the game controller state.

0 commit comments

Comments
 (0)