diff options
| author | hathach <[email protected]> | 2021-02-09 15:57:29 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2021-02-09 15:57:29 +0700 |
| commit | d2b8e591f6f939f87c28aaa8b12f67a5072d2b06 (patch) | |
| tree | c97c4692866d3ec7cf3b3f7c4e1fe3acd3fcead5 /examples | |
| parent | ecd16cf24bcef606019c8e4d694b3bf0f138c9aa (diff) | |
tud_hid_report_complete_cb() API
update hid composite to make use of tud_hid_report_complete_cb() for
sending reports when possible.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/device/hid_composite/src/main.c | 185 | ||||
| -rw-r--r-- | examples/device/hid_composite/src/usb_descriptors.c | 2 | ||||
| -rw-r--r-- | examples/device/hid_composite/src/usb_descriptors.h | 3 |
3 files changed, 106 insertions, 84 deletions
diff --git a/examples/device/hid_composite/src/main.c b/examples/device/hid_composite/src/main.c index 45b8ba674..9fb2de88d 100644 --- a/examples/device/hid_composite/src/main.c +++ b/examples/device/hid_composite/src/main.c @@ -104,114 +104,135 @@ void tud_resume_cb(void) // USB HID //--------------------------------------------------------------------+ -void hid_task(void) +static void send_hid_report(uint8_t report_id, uint32_t btn) { - // Poll every 10ms - const uint32_t interval_ms = 10; - static uint32_t start_ms = 0; + // skip if hid is not ready yet + if ( !tud_hid_ready() ) return; - if ( board_millis() - start_ms < interval_ms) return; // not enough time - start_ms += interval_ms; + switch(report_id) + { + case REPORT_ID_KEYBOARD: + { + // use to avoid send multiple consecutive zero report for keyboard + static bool has_keyboard_key = false; - uint32_t const btn = board_button_read(); + if ( btn ) + { + uint8_t keycode[6] = { 0 }; + keycode[0] = HID_KEY_A; - // Remote wakeup - if ( tud_suspended() && btn ) - { - // Wake up host if we are in suspend mode - // and REMOTE_WAKEUP feature is enabled by host - tud_remote_wakeup(); - } + tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode); + has_keyboard_key = true; + }else + { + // send empty key report if previously has key pressed + if (has_keyboard_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); + has_keyboard_key = false; + } + } + break; - /*------------- Mouse -------------*/ - if ( tud_hid_ready() ) - { - if ( btn ) + case REPORT_ID_MOUSE: { int8_t const delta = 5; - // no button, right + down, no scroll pan + // no button, right + down, no scroll, no pan tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0); - - // delay a bit before sending keyboard report - board_delay(10); } - } - - /*------------- Keyboard -------------*/ - if ( tud_hid_ready() ) - { - // use to avoid send multiple consecutive zero report for keyboard - static bool has_key = false; + break; - if ( btn ) + case REPORT_ID_CONSUMER_CONTROL: { - uint8_t keycode[6] = { 0 }; - keycode[0] = HID_KEY_A; - - tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode); + // use to avoid send multiple consecutive zero report + static bool has_consumer_key = false; - has_key = true; - }else - { - // send empty key report if previously has key pressed - if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); - has_key = false; + if ( btn ) + { + // volume down + uint16_t volume_down = HID_USAGE_CONSUMER_VOLUME_DECREMENT; + tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &volume_down, 2); + has_consumer_key = true; + }else + { + // send empty key report (release key) if previously has key pressed + uint16_t empty_key = 0; + if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2); + has_consumer_key = false; + } } + break; - // delay a bit before sending consumer report - board_delay(10); - } + case REPORT_ID_GAMEPAD: + { + // use to avoid send multiple consecutive zero report for keyboard + static bool has_gamepad_key = false; - /*------------- Consume Control -------------*/ - if ( tud_hid_ready() ) - { - // use to avoid send multiple consecutive zero report - static bool has_consumer_key = false; + hid_gamepad_report_t report = + { + .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0, + .hat = 0, .buttons = 0 + }; - if ( btn ) - { - // volume down - uint16_t volume_down = HID_USAGE_CONSUMER_VOLUME_DECREMENT; - tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &volume_down, 2); + if ( btn ) + { + report.hat = GAMEPAD_HAT_UP; + report.buttons = GAMEPAD_BUTTON_A; + tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report)); - has_consumer_key = true; - }else - { - // send empty key report (release key) if previously has key pressed - uint16_t empty_key = 0; - if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2); - has_consumer_key = false; + has_gamepad_key = true; + }else + { + report.hat = GAMEPAD_HAT_CENTERED; + report.buttons = 0; + if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report)); + has_gamepad_key = false; + } } + break; - // delay a bit before sending next report - board_delay(10); + default: break; } +} + +// Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..) +// tud_hid_report_complete_cb() is used to send the next report after previous one is complete +void hid_task(void) +{ + // Poll every 10ms + const uint32_t interval_ms = 10; + static uint32_t start_ms = 0; + + if ( board_millis() - start_ms < interval_ms) return; // not enough time + start_ms += interval_ms; - /*------------- Gamepad -------------*/ - if ( tud_hid_ready() ) + uint32_t const btn = board_button_read(); + + // Remote wakeup + if ( tud_suspended() && btn ) { - // use to avoid send multiple consecutive zero report for keyboard - static bool has_gamepad_key = false; + // Wake up host if we are in suspend mode + // and REMOTE_WAKEUP feature is enabled by host + tud_remote_wakeup(); + }else + { + // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb() + send_hid_report(REPORT_ID_KEYBOARD, btn); + } +} - hid_gamepad_report_t report = - { - .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0, - .hat = 0, .buttons = 0 - }; +// Invoked when sent REPORT successfully to host +// Application can use this to send the next report +// Note: For composite reports, report[0] is report ID +void tud_hid_report_complete_cb(uint8_t itf, uint8_t const* report, uint8_t len) +{ + (void) itf; + (void) len; - if ( btn ) - { - report.hat = GAMEPAD_HAT_UP; - tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report)); + uint8_t next_report_id = report[0] + 1; - has_gamepad_key = true; - }else - { - report.hat = GAMEPAD_HAT_CENTERED; - if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report)); - has_gamepad_key = false; - } + if (next_report_id < REPORT_ID_COUNT) + { + send_hid_report(next_report_id, board_button_read()); } } diff --git a/examples/device/hid_composite/src/usb_descriptors.c b/examples/device/hid_composite/src/usb_descriptors.c index fbe0e2550..e5f1ea703 100644 --- a/examples/device/hid_composite/src/usb_descriptors.c +++ b/examples/device/hid_composite/src/usb_descriptors.c @@ -107,7 +107,7 @@ uint8_t const desc_configuration[] = TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval - TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10) + TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 2) }; // Invoked when received GET CONFIGURATION DESCRIPTOR diff --git a/examples/device/hid_composite/src/usb_descriptors.h b/examples/device/hid_composite/src/usb_descriptors.h index 7894719fd..ca8925ad9 100644 --- a/examples/device/hid_composite/src/usb_descriptors.h +++ b/examples/device/hid_composite/src/usb_descriptors.h @@ -30,7 +30,8 @@ enum REPORT_ID_KEYBOARD = 1, REPORT_ID_MOUSE, REPORT_ID_CONSUMER_CONTROL, - REPORT_ID_GAMEPAD + REPORT_ID_GAMEPAD, + REPORT_ID_COUNT }; #endif /* USB_DESCRIPTORS_H_ */ |
