11/*
22 Joystick2.cpp
33
4- Copyright (c) 2015, Matthew Heironimus
4+ Copyright (c) 2015-2025 Matthew Heironimus
55
66 This library is free software; you can redistribute it and/or
77 modify it under the terms of the GNU Lesser General Public
2727#define JOYSTICK_STATE_SIZE 4
2828
2929static const uint8_t _hidReportDescriptor[] PROGMEM = {
30-
31- // Joystick #1
32- 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
33- 0x09 , 0x04 , // USAGE (Joystick)
34- 0xa1 , 0x01 , // COLLECTION (Application)
35- 0x85 , JOYSTICK_REPORT_ID, // REPORT_ID (3)
36-
37- // 16 Buttons
38- 0x05 , 0x09 , // USAGE_PAGE (Button)
39- 0x19 , 0x01 , // USAGE_MINIMUM (Button 1)
40- 0x29 , 0x10 , // USAGE_MAXIMUM (Button 16)
41- 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
42- 0x25 , 0x01 , // LOGICAL_MAXIMUM (1)
43- 0x75 , 0x01 , // REPORT_SIZE (1)
44- 0x95 , 0x10 , // REPORT_COUNT (16)
45- 0x55 , 0x00 , // UNIT_EXPONENT (0)
46- 0x65 , 0x00 , // UNIT (None)
47- 0x81 , 0x02 , // INPUT (Data,Var,Abs)
48-
49- // X and Y Axis
50- 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
51- 0x09 , 0x01 , // USAGE (Pointer)
52- 0x15 , 0x81 , // LOGICAL_MINIMUM (-127)
53- 0x25 , 0x7f , // LOGICAL_MAXIMUM (127)
54- 0xA1 , 0x00 , // COLLECTION (Physical)
55- 0x09 , 0x30 , // USAGE (x)
56- 0x09 , 0x31 , // USAGE (y)
57- 0x75 , 0x08 , // REPORT_SIZE (8)
58- 0x95 , 0x02 , // REPORT_COUNT (2)
59- 0x81 , 0x02 , // INPUT (Data,Var,Abs)
60- 0xc0 , // END_COLLECTION
61- 0xc0 , // END_COLLECTION
62-
63- // Joystick #2
64- 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
65- 0x09 , 0x04 , // USAGE (Joystick)
66- 0xa1 , 0x01 , // COLLECTION (Application)
67- 0x85 , JOYSTICK2_REPORT_ID, // REPORT_ID (4)
68-
69- // 16 Buttons
70- 0x05 , 0x09 , // USAGE_PAGE (Button)
71- 0x19 , 0x01 , // USAGE_MINIMUM (Button 1)
72- 0x29 , 0x10 , // USAGE_MAXIMUM (Button 16)
73- 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
74- 0x25 , 0x01 , // LOGICAL_MAXIMUM (1)
75- 0x75 , 0x01 , // REPORT_SIZE (1)
76- 0x95 , 0x10 , // REPORT_COUNT (16)
77- 0x55 , 0x00 , // UNIT_EXPONENT (0)
78- 0x65 , 0x00 , // UNIT (None)
79- 0x81 , 0x02 , // INPUT (Data,Var,Abs)
80-
81- // X and Y Axis
82- 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
83- 0x09 , 0x01 , // USAGE (Pointer)
84- 0x15 , 0x81 , // LOGICAL_MINIMUM (-127)
85- 0x25 , 0x7f , // LOGICAL_MAXIMUM (127)
86- 0xA1 , 0x00 , // COLLECTION (Physical)
87- 0x09 , 0x30 , // USAGE (x)
88- 0x09 , 0x31 , // USAGE (y)
89- 0x75 , 0x08 , // REPORT_SIZE (8)
90- 0x95 , 0x02 , // REPORT_COUNT (2)
91- 0x81 , 0x02 , // INPUT (Data,Var,Abs)
92- 0xc0 , // END_COLLECTION
93- 0xc0 // END_COLLECTION
30+
31+ // Joystick #1
32+ 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
33+ 0x09 , 0x04 , // USAGE (Joystick)
34+ 0xa1 , 0x01 , // COLLECTION (Application)
35+ 0x85 , JOYSTICK_REPORT_ID, // REPORT_ID (3)
36+
37+ // 16 Buttons
38+ 0x05 , 0x09 , // USAGE_PAGE (Button)
39+ 0x19 , 0x01 , // USAGE_MINIMUM (Button 1)
40+ 0x29 , 0x10 , // USAGE_MAXIMUM (Button 16)
41+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
42+ 0x25 , 0x01 , // LOGICAL_MAXIMUM (1)
43+ 0x75 , 0x01 , // REPORT_SIZE (1)
44+ 0x95 , 0x10 , // REPORT_COUNT (16)
45+ 0x55 , 0x00 , // UNIT_EXPONENT (0)
46+ 0x65 , 0x00 , // UNIT (None)
47+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
48+
49+ // X and Y Axis
50+ 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
51+ 0x09 , 0x01 , // USAGE (Pointer)
52+ 0x15 , 0x81 , // LOGICAL_MINIMUM (-127)
53+ 0x25 , 0x7f , // LOGICAL_MAXIMUM (127)
54+ 0xA1 , 0x00 , // COLLECTION (Physical)
55+ 0x09 , 0x30 , // USAGE (x)
56+ 0x09 , 0x31 , // USAGE (y)
57+ 0x75 , 0x08 , // REPORT_SIZE (8)
58+ 0x95 , 0x02 , // REPORT_COUNT (2)
59+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
60+ 0xc0 , // END_COLLECTION
61+ 0xc0 , // END_COLLECTION
62+
63+ // Joystick #2
64+ 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
65+ 0x09 , 0x04 , // USAGE (Joystick)
66+ 0xa1 , 0x01 , // COLLECTION (Application)
67+ 0x85 , JOYSTICK2_REPORT_ID, // REPORT_ID (4)
68+
69+ // 16 Buttons
70+ 0x05 , 0x09 , // USAGE_PAGE (Button)
71+ 0x19 , 0x01 , // USAGE_MINIMUM (Button 1)
72+ 0x29 , 0x10 , // USAGE_MAXIMUM (Button 16)
73+ 0x15 , 0x00 , // LOGICAL_MINIMUM (0)
74+ 0x25 , 0x01 , // LOGICAL_MAXIMUM (1)
75+ 0x75 , 0x01 , // REPORT_SIZE (1)
76+ 0x95 , 0x10 , // REPORT_COUNT (16)
77+ 0x55 , 0x00 , // UNIT_EXPONENT (0)
78+ 0x65 , 0x00 , // UNIT (None)
79+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
80+
81+ // X and Y Axis
82+ 0x05 , 0x01 , // USAGE_PAGE (Generic Desktop)
83+ 0x09 , 0x01 , // USAGE (Pointer)
84+ 0x15 , 0x81 , // LOGICAL_MINIMUM (-127)
85+ 0x25 , 0x7f , // LOGICAL_MAXIMUM (127)
86+ 0xA1 , 0x00 , // COLLECTION (Physical)
87+ 0x09 , 0x30 , // USAGE (x)
88+ 0x09 , 0x31 , // USAGE (y)
89+ 0x75 , 0x08 , // REPORT_SIZE (8)
90+ 0x95 , 0x02 , // REPORT_COUNT (2)
91+ 0x81 , 0x02 , // INPUT (Data,Var,Abs)
92+ 0xc0 , // END_COLLECTION
93+ 0xc0 // END_COLLECTION
9494};
9595
9696Joystick_::Joystick_ (uint8_t initJoystickId)
9797{
98- // Setup HID report structure
99- static bool usbSetup = false ;
100-
101- if (!usbSetup)
102- {
103- static HIDSubDescriptor node (_hidReportDescriptor, sizeof (_hidReportDescriptor));
104- HID ().AppendDescriptor (&node);
105- usbSetup = true ;
106- }
98+ // Setup HID report structure
99+ static bool usbSetup = false ;
100+
101+ if (!usbSetup)
102+ {
103+ static HIDSubDescriptor node (_hidReportDescriptor, sizeof (_hidReportDescriptor));
104+ HID ().AppendDescriptor (&node);
105+ usbSetup = true ;
106+ }
107107
108- // Initalize State
109- joystickId = initJoystickId;
110- xAxis = 0 ;
111- yAxis = 0 ;
112- buttons = 0 ;
108+ // Initialize State
109+ joystickId = initJoystickId;
110+ xAxis = 0 ;
111+ yAxis = 0 ;
112+ buttons = 0 ;
113113}
114114
115115void Joystick_::begin (bool initAutoSendState)
116116{
117- autoSendState = initAutoSendState;
118- sendState ();
117+ autoSendState = initAutoSendState;
118+
119+ #if defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_UNOR4_MINIMA)
120+ // Delay added for the UNO R4 boards. Not sure why this delay is required,
121+ // but the library will sometimes hang on the first call to sendState
122+ // without it.
123+ delay (2000 );
124+ #endif
125+
126+ sendState ();
119127}
120128
121129void Joystick_::end ()
@@ -126,56 +134,56 @@ void Joystick_::setButton(uint8_t button, uint8_t value)
126134{
127135 if (value == 0 )
128136 {
129- releaseButton (button);
137+ releaseButton (button);
130138 }
131139 else
132140 {
133- pressButton (button);
141+ pressButton (button);
134142 }
135143}
136144void Joystick_::pressButton (uint8_t button)
137145{
138- bitSet (buttons, button);
139- if (autoSendState) sendState ();
146+ bitSet (buttons, button);
147+ if (autoSendState) sendState ();
140148}
141149void Joystick_::releaseButton (uint8_t button)
142150{
143- bitClear (buttons, button);
144- if (autoSendState) sendState ();
151+ bitClear (buttons, button);
152+ if (autoSendState) sendState ();
145153}
146154
147155void Joystick_::setXAxis (int8_t value)
148156{
149- xAxis = value;
150- if (autoSendState) sendState ();
157+ xAxis = value;
158+ if (autoSendState) sendState ();
151159}
152160void Joystick_::setYAxis (int8_t value)
153161{
154- yAxis = value;
155- if (autoSendState) sendState ();
162+ yAxis = value;
163+ if (autoSendState) sendState ();
156164}
157165
158166void Joystick_::sendState ()
159167{
160- int8_t data[JOYSTICK_STATE_SIZE];
161- uint16_t buttonTmp = buttons;
168+ int8_t data[JOYSTICK_STATE_SIZE];
169+ uint16_t buttonTmp = buttons;
162170
163- // Split 16 bit button-state into 2 bytes
164- data[0 ] = buttonTmp & 0xFF ;
165- buttonTmp >>= 8 ;
166- data[1 ] = buttonTmp & 0xFF ;
171+ // Split 16 bit button-state into 2 bytes
172+ data[0 ] = buttonTmp & 0xFF ;
173+ buttonTmp >>= 8 ;
174+ data[1 ] = buttonTmp & 0xFF ;
167175
168- data[2 ] = xAxis;
169- data[3 ] = yAxis;
176+ data[2 ] = xAxis;
177+ data[3 ] = yAxis;
170178
171- // HID().SendReport(Report number, array of values in same order as HID descriptor, length)
172- HID ().SendReport (JOYSTICK_REPORT_ID + joystickId, data, JOYSTICK_STATE_SIZE);
179+ // HID().SendReport(Report number, array of values in same order as HID descriptor, length)
180+ HID ().SendReport (JOYSTICK_REPORT_ID + joystickId, data, JOYSTICK_STATE_SIZE);
173181}
174182
175183Joystick_ Joystick[2 ] =
176184{
177- Joystick_ (0 ),
178- Joystick_ (1 )
185+ Joystick_ (0 ),
186+ Joystick_ (1 )
179187};
180188
181189#endif
0 commit comments