32
32
#define EPIN 0x80
33
33
34
34
uint8_t const _ascii2keycode[128 ][2 ] = {HID_ASCII_TO_KEYCODE};
35
+ static Adafruit_USBD_HID *_hid_instances[CFG_TUD_HID] = {0 };
35
36
36
- // TODO multiple instances
37
- static Adafruit_USBD_HID *_hid_dev = NULL ;
37
+ uint8_t Adafruit_USBD_HID::_instance_count = 0 ;
38
38
39
39
#ifdef ARDUINO_ARCH_ESP32
40
40
static uint16_t hid_load_descriptor (uint8_t *dst, uint8_t *itf) {
41
41
// uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB HID");
42
42
43
- TU_VERIFY (_hid_dev);
43
+ uint8_t const inst_count = Adafruit_USBD_HID::getInstanceCount ();
44
+ TU_VERIFY (inst_count > 0 , 0 );
45
+
46
+ Adafruit_USBD_HID *p_hid = _hid_instances[inst_count - 1 ];
47
+ TU_VERIFY (p_hid);
44
48
45
49
uint8_t ep_in = tinyusb_get_free_in_endpoint ();
46
50
TU_VERIFY (ep_in != 0 );
47
51
ep_in |= 0x80 ;
48
52
49
53
uint8_t ep_out = 0 ;
50
- if (_hid_dev ->isOutEndpointEnabled ()) {
54
+ if (p_hid ->isOutEndpointEnabled ()) {
51
55
ep_out = tinyusb_get_free_out_endpoint ();
52
56
TU_VERIFY (ep_out != 0 );
53
57
}
54
58
55
59
uint16_t const desc_len =
56
- _hid_dev ->makeItfDesc (*itf, dst, TUD_HID_INOUT_DESC_LEN, ep_in, ep_out);
60
+ p_hid ->makeItfDesc (*itf, dst, TUD_HID_INOUT_DESC_LEN, ep_in, ep_out);
57
61
58
62
*itf += 1 ;
59
63
return desc_len;
@@ -68,6 +72,7 @@ Adafruit_USBD_HID::Adafruit_USBD_HID(void)
68
72
Adafruit_USBD_HID::Adafruit_USBD_HID (uint8_t const *desc_report, uint16_t len,
69
73
uint8_t protocol, uint8_t interval_ms,
70
74
bool has_out_endpoint) {
75
+ _instance = INVALID_INSTANCE;
71
76
_interval_ms = interval_ms;
72
77
_protocol = protocol;
73
78
@@ -82,7 +87,13 @@ Adafruit_USBD_HID::Adafruit_USBD_HID(uint8_t const *desc_report, uint16_t len,
82
87
83
88
#ifdef ARDUINO_ARCH_ESP32
84
89
// ESP32 requires setup configuration descriptor within constructor
85
- _hid_dev = this ;
90
+ if (_instance_count >= CFG_TUD_HID) {
91
+ return ;
92
+ }
93
+
94
+ _instance = _instance_count++;
95
+ _hid_instances[_instance] = this ;
96
+
86
97
uint16_t const desc_len = getInterfaceDescriptor (0 , NULL , 0 );
87
98
tinyusb_enable_interface (USB_INTERFACE_HID, desc_len, hid_load_descriptor);
88
99
#endif
@@ -158,11 +169,20 @@ uint16_t Adafruit_USBD_HID::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
158
169
}
159
170
160
171
bool Adafruit_USBD_HID::begin (void ) {
172
+ if (isValid ()) {
173
+ return true ;
174
+ }
175
+
176
+ if (_instance_count >= CFG_TUD_HID) {
177
+ return false ;
178
+ }
179
+
161
180
if (!TinyUSBDevice.addInterface (*this )) {
162
181
return false ;
163
182
}
183
+ _instance = _instance_count++;
184
+ _hid_instances[_instance] = this ;
164
185
165
- _hid_dev = this ;
166
186
return true ;
167
187
}
168
188
@@ -192,13 +212,13 @@ extern "C" {
192
212
// Application return pointer to descriptor, whose contents must exist long
193
213
// enough for transfer to complete
194
214
uint8_t const *tud_hid_descriptor_report_cb (uint8_t itf) {
195
- ( void ) itf;
215
+ Adafruit_USBD_HID *p_hid = _hid_instances[ itf] ;
196
216
197
- if (!_hid_dev ) {
217
+ if (!p_hid ) {
198
218
return NULL ;
199
219
}
200
220
201
- return _hid_dev ->_desc_report ;
221
+ return p_hid ->_desc_report ;
202
222
}
203
223
204
224
// Invoked when received GET_REPORT control request
@@ -207,27 +227,27 @@ uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf) {
207
227
uint16_t tud_hid_get_report_cb (uint8_t itf, uint8_t report_id,
208
228
hid_report_type_t report_type, uint8_t *buffer,
209
229
uint16_t reqlen) {
210
- ( void ) itf;
230
+ Adafruit_USBD_HID *p_hid = _hid_instances[ itf] ;
211
231
212
- if (!(_hid_dev && _hid_dev ->_get_report_cb )) {
232
+ if (!(p_hid && p_hid ->_get_report_cb )) {
213
233
return 0 ;
214
234
}
215
235
216
- return _hid_dev ->_get_report_cb (report_id, report_type, buffer, reqlen);
236
+ return p_hid ->_get_report_cb (report_id, report_type, buffer, reqlen);
217
237
}
218
238
219
239
// Invoked when received SET_REPORT control request or
220
240
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
221
241
void tud_hid_set_report_cb (uint8_t itf, uint8_t report_id,
222
242
hid_report_type_t report_type, uint8_t const *buffer,
223
243
uint16_t bufsize) {
224
- ( void ) itf;
244
+ Adafruit_USBD_HID *p_hid = _hid_instances[ itf] ;
225
245
226
- if (!(_hid_dev && _hid_dev ->_set_report_cb )) {
246
+ if (!(p_hid && p_hid ->_set_report_cb )) {
227
247
return ;
228
248
}
229
249
230
- _hid_dev ->_set_report_cb (report_id, report_type, buffer, bufsize);
250
+ p_hid ->_set_report_cb (report_id, report_type, buffer, bufsize);
231
251
}
232
252
233
253
} // extern "C"
0 commit comments