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.
This commit is contained in:
Earle F. Philhower, III 2023-08-31 08:40:38 -07:00 committed by GitHub
parent faf06a3b70
commit b9c66ca0fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 4 deletions

View file

@ -394,6 +394,18 @@ void __USBStart() {
} }
bool __USBHIDReady() {
uint32_t start = millis();
const uint32_t timeout = 500;
while (((millis() - start) < timeout) && tud_ready() && !tud_hid_ready()) {
tud_task();
delay(1);
}
return tud_hid_ready();
}
// Invoked when received GET_REPORT control request // Invoked when received GET_REPORT control request
// Application must fill buffer report's content and return its length. // Application must fill buffer report's content and return its length.
// Return zero will cause the stack to STALL request // Return zero will cause the stack to STALL request

View file

@ -45,3 +45,6 @@ int __USBGetJoystickReportID();
// Called by main() to init the USB HW/SW. // Called by main() to init the USB HW/SW.
void __USBStart(); void __USBStart();
// Helper class for HID report sending with wait and timeout
bool __USBHIDReady();

View file

@ -40,7 +40,7 @@ Joystick_::Joystick_(void) {
void Joystick_::send_now(void) { void Joystick_::send_now(void) {
CoreMutex m(&__usb_mutex); CoreMutex m(&__usb_mutex);
tud_task(); tud_task();
if (tud_hid_ready()) { if (__USBHIDReady()) {
tud_hid_n_report(0, __USBGetJoystickReportID(), &data, sizeof(data)); tud_hid_n_report(0, __USBGetJoystickReportID(), &data, sizeof(data));
} }
tud_task(); tud_task();

View file

@ -40,7 +40,7 @@ Keyboard_::Keyboard_(void) {
void Keyboard_::sendReport(KeyReport* keys) { void Keyboard_::sendReport(KeyReport* keys) {
CoreMutex m(&__usb_mutex); CoreMutex m(&__usb_mutex);
tud_task(); tud_task();
if (tud_hid_ready()) { if (__USBHIDReady()) {
tud_hid_keyboard_report(__USBGetKeyboardReportID(), keys->modifiers, keys->keys); tud_hid_keyboard_report(__USBGetKeyboardReportID(), keys->modifiers, keys->keys);
} }
tud_task(); tud_task();
@ -49,7 +49,7 @@ void Keyboard_::sendReport(KeyReport* keys) {
void Keyboard_::sendConsumerReport(uint16_t key) { void Keyboard_::sendConsumerReport(uint16_t key) {
CoreMutex m(&__usb_mutex); CoreMutex m(&__usb_mutex);
tud_task(); tud_task();
if (tud_hid_ready()) { if (__USBHIDReady()) {
tud_hid_report(__USBGetKeyboardReportID() + 1, &key, sizeof(key)); tud_hid_report(__USBGetKeyboardReportID() + 1, &key, sizeof(key));
} }
tud_task(); tud_task();

View file

@ -44,7 +44,7 @@ Mouse_::Mouse_(void) {
void Mouse_::move(int x, int y, signed char wheel) { void Mouse_::move(int x, int y, signed char wheel) {
CoreMutex m(&__usb_mutex); CoreMutex m(&__usb_mutex);
tud_task(); tud_task();
if (tud_hid_ready()) { if (__USBHIDReady()) {
tud_hid_mouse_report(__USBGetMouseReportID(), _buttons, limit_xy(x), limit_xy(y), wheel, 0); tud_hid_mouse_report(__USBGetMouseReportID(), _buttons, limit_xy(x), limit_xy(y), wheel, 0);
} }
tud_task(); tud_task();