Skip to content

Commit d1223dd

Browse files
committed
Allow more than one instance of the same device
1 parent f36e483 commit d1223dd

7 files changed

+17
-27
lines changed

libraries/USB/src/USBHIDConsumerControl.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ USBHIDConsumerControl::USBHIDConsumerControl(): hid(){
2727
if(!initialized){
2828
initialized = true;
2929
hid.addDevice(this, sizeof(report_descriptor));
30-
} else {
31-
isr_log_e("Only one instance of USBHIDConsumerControl is allowed!");
32-
abort();
3330
}
3431
}
3532

libraries/USB/src/USBHIDGamepad.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ USBHIDGamepad::USBHIDGamepad(): hid(), _x(0), _y(0), _z(0), _rz(0), _rx(0), _ry(
2727
if(!initialized){
2828
initialized = true;
2929
hid.addDevice(this, sizeof(report_descriptor));
30-
} else {
31-
isr_log_e("Only one instance of USBHIDGamepad is allowed!");
32-
abort();
3330
}
3431
}
3532

libraries/USB/src/USBHIDKeyboard.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ USBHIDKeyboard::USBHIDKeyboard(): hid(){
3838
if(!initialized){
3939
initialized = true;
4040
hid.addDevice(this, sizeof(report_descriptor));
41-
} else {
42-
isr_log_e("Only one instance of USBHIDKeyboard is allowed!");
43-
abort();
4441
}
4542
}
4643

@@ -64,10 +61,11 @@ void USBHIDKeyboard::onEvent(arduino_usb_hid_keyboard_event_t event, esp_event_h
6461
}
6562

6663
void USBHIDKeyboard::_onOutput(uint8_t report_id, const uint8_t* buffer, uint16_t len){
67-
//log_d("LEDS: 0x%02x", buffer[0]);
68-
arduino_usb_hid_keyboard_event_data_t p = {0};
69-
p.leds = buffer[0];
70-
arduino_usb_event_post(ARDUINO_USB_HID_KEYBOARD_EVENTS, ARDUINO_USB_HID_KEYBOARD_LED_EVENT, &p, sizeof(arduino_usb_hid_keyboard_event_data_t), portMAX_DELAY);
64+
if(report_id == HID_REPORT_ID_KEYBOARD){
65+
arduino_usb_hid_keyboard_event_data_t p = {0};
66+
p.leds = buffer[0];
67+
arduino_usb_event_post(ARDUINO_USB_HID_KEYBOARD_EVENTS, ARDUINO_USB_HID_KEYBOARD_LED_EVENT, &p, sizeof(arduino_usb_hid_keyboard_event_data_t), portMAX_DELAY);
68+
}
7169
}
7270

7371
void USBHIDKeyboard::sendReport(KeyReport* keys)

libraries/USB/src/USBHIDMouse.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ USBHIDMouse::USBHIDMouse(): hid(), _buttons(0){
3434
if(!initialized){
3535
initialized = true;
3636
hid.addDevice(this, sizeof(report_descriptor));
37-
} else {
38-
isr_log_e("Only one instance of USBHIDMouse is allowed!");
39-
abort();
4037
}
4138
}
4239

libraries/USB/src/USBHIDSystemControl.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ USBHIDSystemControl::USBHIDSystemControl(): hid(){
2727
if(!initialized){
2828
initialized = true;
2929
hid.addDevice(this, sizeof(report_descriptor));
30-
} else {
31-
isr_log_e("Only one instance of USBHIDSystemControl is allowed!");
32-
abort();
3330
}
3431
}
3532

libraries/USB/src/USBHIDVendor.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ esp_err_t arduino_usb_event_handler_register_with(esp_event_base_t event_base, i
5858
// max size is 64 and we need one byte for the report ID
5959
static const uint8_t HID_VENDOR_REPORT_SIZE = 63;
6060

61+
static uint8_t feature[HID_VENDOR_REPORT_SIZE];
62+
static xQueueHandle rx_queue = NULL;
63+
6164
static const uint8_t report_descriptor[] = {
6265
TUD_HID_REPORT_DESC_GENERIC_INOUT_FEATURE(HID_VENDOR_REPORT_SIZE, HID_REPORT_ID(HID_REPORT_ID_VENDOR))
6366
};
@@ -68,9 +71,6 @@ USBHIDVendor::USBHIDVendor(): hid(){
6871
initialized = true;
6972
hid.addDevice(this, sizeof(report_descriptor));
7073
memset(feature, 0, HID_VENDOR_REPORT_SIZE);
71-
} else {
72-
isr_log_e("Only one instance of USBHIDVendor is allowed!");
73-
abort();
7474
}
7575
}
7676

@@ -112,7 +112,9 @@ void USBHIDVendor::onEvent(arduino_usb_hid_vendor_event_t event, esp_event_handl
112112
}
113113

114114
uint16_t USBHIDVendor::_onGetFeature(uint8_t report_id, uint8_t* buffer, uint16_t len){
115-
log_v("len: %u", len);
115+
if(report_id != HID_REPORT_ID_VENDOR){
116+
return 0;
117+
}
116118
memcpy(buffer, feature, len);
117119
arduino_usb_hid_vendor_event_data_t p = {0};
118120
p.buffer = feature;
@@ -122,7 +124,9 @@ uint16_t USBHIDVendor::_onGetFeature(uint8_t report_id, uint8_t* buffer, uint16_
122124
}
123125

124126
void USBHIDVendor::_onSetFeature(uint8_t report_id, const uint8_t* buffer, uint16_t len){
125-
log_v("len: %u", len);
127+
if(report_id != HID_REPORT_ID_VENDOR){
128+
return;
129+
}
126130
memcpy(feature, buffer, len);
127131
arduino_usb_hid_vendor_event_data_t p = {0};
128132
p.buffer = feature;
@@ -131,7 +135,9 @@ void USBHIDVendor::_onSetFeature(uint8_t report_id, const uint8_t* buffer, uint1
131135
}
132136

133137
void USBHIDVendor::_onOutput(uint8_t report_id, const uint8_t* buffer, uint16_t len){
134-
log_v("len: %u", len);
138+
if(report_id != HID_REPORT_ID_VENDOR){
139+
return;
140+
}
135141
for(uint32_t i=0; i<len; i++){
136142
if(rx_queue == NULL || !xQueueSend(rx_queue, buffer+i, 0)){
137143
len = i+1;

libraries/USB/src/USBHIDVendor.h

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ typedef struct {
3535
class USBHIDVendor: public USBHIDDevice, public Stream {
3636
private:
3737
USBHID hid;
38-
uint8_t feature[63];
39-
xQueueHandle rx_queue;
4038
public:
4139
USBHIDVendor(void);
4240
void begin(void);

0 commit comments

Comments
 (0)