Skip to content

Commit b9c66ca

Browse files
Ensure HID reports aren't dropped (#1685)
Fixes #1682 Make the HID report wait up to 500ms for an existing one to go out before giving up sending a report.
1 parent faf06a3 commit b9c66ca

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

cores/rp2040/RP2040USB.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,18 @@ void __USBStart() {
394394
}
395395

396396

397+
bool __USBHIDReady() {
398+
uint32_t start = millis();
399+
const uint32_t timeout = 500;
400+
401+
while (((millis() - start) < timeout) && tud_ready() && !tud_hid_ready()) {
402+
tud_task();
403+
delay(1);
404+
}
405+
return tud_hid_ready();
406+
}
407+
408+
397409
// Invoked when received GET_REPORT control request
398410
// Application must fill buffer report's content and return its length.
399411
// Return zero will cause the stack to STALL request

cores/rp2040/RP2040USB.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ int __USBGetJoystickReportID();
4545

4646
// Called by main() to init the USB HW/SW.
4747
void __USBStart();
48+
49+
// Helper class for HID report sending with wait and timeout
50+
bool __USBHIDReady();

libraries/Joystick/src/Joystick.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Joystick_::Joystick_(void) {
4040
void Joystick_::send_now(void) {
4141
CoreMutex m(&__usb_mutex);
4242
tud_task();
43-
if (tud_hid_ready()) {
43+
if (__USBHIDReady()) {
4444
tud_hid_n_report(0, __USBGetJoystickReportID(), &data, sizeof(data));
4545
}
4646
tud_task();

libraries/Keyboard/src/Keyboard.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Keyboard_::Keyboard_(void) {
4040
void Keyboard_::sendReport(KeyReport* keys) {
4141
CoreMutex m(&__usb_mutex);
4242
tud_task();
43-
if (tud_hid_ready()) {
43+
if (__USBHIDReady()) {
4444
tud_hid_keyboard_report(__USBGetKeyboardReportID(), keys->modifiers, keys->keys);
4545
}
4646
tud_task();
@@ -49,7 +49,7 @@ void Keyboard_::sendReport(KeyReport* keys) {
4949
void Keyboard_::sendConsumerReport(uint16_t key) {
5050
CoreMutex m(&__usb_mutex);
5151
tud_task();
52-
if (tud_hid_ready()) {
52+
if (__USBHIDReady()) {
5353
tud_hid_report(__USBGetKeyboardReportID() + 1, &key, sizeof(key));
5454
}
5555
tud_task();

libraries/Mouse/src/Mouse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Mouse_::Mouse_(void) {
4444
void Mouse_::move(int x, int y, signed char wheel) {
4545
CoreMutex m(&__usb_mutex);
4646
tud_task();
47-
if (tud_hid_ready()) {
47+
if (__USBHIDReady()) {
4848
tud_hid_mouse_report(__USBGetMouseReportID(), _buttons, limit_xy(x), limit_xy(y), wheel, 0);
4949
}
5050
tud_task();

0 commit comments

Comments
 (0)