未验证 提交 2e72894a 编写于 作者: R RefactorFactory 提交者: GitHub

USBHIDKeyboard: Fix 200ms delay for every key (#7218)

Arduino-esp32 2.0.4 was released with a version of TinyUSB hid_device.h
that uses uint16_t for the last argument:

https://github.com/espressif/arduino-esp32/blob/2.0.4/tools/sdk/esp32s2/include/arduino_tinyusb/tinyusb/src/class/hid/hid_device.h

    TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len);

But USBHID implements this callback with uint8_t:

https://github.com/espressif/arduino-esp32/blob/2.0.4/libraries/USB/src/USBHID.cpp

    void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len){
        if (tinyusb_hid_device_input_sem) {
            xSemaphoreGive(tinyusb_hid_device_input_sem);
        }
    }

The result is that when USBHIDKeyboard sends a report to the host, it
times out, waiting 100 ms for the callback to be called. It does this
once for pressing the key and once for releasing the key, so
100 ms * 2 = 200 ms.

The latest version of hid_device.h reverts the last argument to uint8_t:

https://github.com/espressif/arduino-esp32/blob/860b104691a28f77896ac544c7745de1ba53642d/tools/sdk/esp32s2/include/arduino_tinyusb/tinyusb/src/class/hid/hid_device.h

    TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, /*uint16_t*/ uint8_t len );

But these commits suggest that the last argument will eventually be
changed to uint16_t:

https://github.com/hathach/tinyusb/commit/556b5d5044bd02e89d6b6c6f21e95d46f5fcdfc1

    change report len in hid API from uint8_t to uint16_t

    since HS interrupt endpoint can be up to 1024, 8-bit is not enough.
    affected APIs are:
    - tud_hid_n_report() / tud_hid_report()
    - tud_hid_report_complete_cb()

https://github.com/hathach/tinyusb/commit/b495d6f8ec263e51b0683ab85ff9e54dab6f5fd3

    temporarily revert len back to uint8_t in tud_hid_report_complete_cb() for up coming release

To prevent this from becoming broken again, in preparation for the change
to uint16_t, make USBHID resilient to any type for the last argument for
tud_hid_report_complete_cb() by using some C++ template metaprogramming,
adapted from https://stackoverflow.com/a/22632571.
Co-authored-by: NRodrigo Garcia <rodrigo.garcia@espressif.com>
上级 8510734d
...@@ -310,7 +310,21 @@ bool USBHID::ready(void){ ...@@ -310,7 +310,21 @@ bool USBHID::ready(void){
return tud_hid_n_ready(0); return tud_hid_n_ready(0);
} }
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len){ // TinyUSB is in the process of changing the type of the last argument to
// tud_hid_report_complete_cb(), so extract the type from the version of TinyUSB that we're
// compiled with.
template <class F> struct ArgType;
template <class R, class T1, class T2, class T3>
struct ArgType<R(*)(T1, T2, T3)> {
typedef T1 type1;
typedef T2 type2;
typedef T3 type3;
};
typedef ArgType<decltype(&tud_hid_report_complete_cb)>::type3 tud_hid_report_complete_cb_len_t;
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, tud_hid_report_complete_cb_len_t len){
if (tinyusb_hid_device_input_sem) { if (tinyusb_hid_device_input_sem) {
xSemaphoreGive(tinyusb_hid_device_input_sem); xSemaphoreGive(tinyusb_hid_device_input_sem);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册