From 72bcc0685c142c512f1d04ff18eaa13b99f62e93 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 8 Feb 2021 19:08:16 +0700 Subject: [PATCH] add tud_hid_n_gamepad_report() helper for gamepad report - Add gamepad to hid_composite example. Though it needs a bit of extra work but it will come later as separated PR. --- examples/device/hid_composite/src/main.c | 29 +++++++++++++++++++ .../hid_composite/src/usb_descriptors.c | 3 +- .../hid_composite/src/usb_descriptors.h | 1 + .../src/usb_descriptors.c | 10 +++---- src/class/hid/hid.h | 4 +-- src/class/hid/hid_device.c | 27 ++++++++++++----- src/class/hid/hid_device.h | 6 ++++ 7 files changed, 65 insertions(+), 15 deletions(-) diff --git a/examples/device/hid_composite/src/main.c b/examples/device/hid_composite/src/main.c index 6a9b6445..45b8ba67 100644 --- a/examples/device/hid_composite/src/main.c +++ b/examples/device/hid_composite/src/main.c @@ -183,6 +183,35 @@ void hid_task(void) if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2); has_consumer_key = false; } + + // delay a bit before sending next report + board_delay(10); + } + + /*------------- Gamepad -------------*/ + if ( tud_hid_ready() ) + { + // use to avoid send multiple consecutive zero report for keyboard + static bool has_gamepad_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 ) + { + report.hat = GAMEPAD_HAT_UP; + tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report)); + + 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; + } } } diff --git a/examples/device/hid_composite/src/usb_descriptors.c b/examples/device/hid_composite/src/usb_descriptors.c index 67ea34c0..fbe0e255 100644 --- a/examples/device/hid_composite/src/usb_descriptors.c +++ b/examples/device/hid_composite/src/usb_descriptors.c @@ -75,7 +75,8 @@ uint8_t const desc_hid_report[] = { TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD )), TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE )), - TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL )) + TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL )), + TUD_HID_REPORT_DESC_GAMEPAD ( HID_REPORT_ID(REPORT_ID_GAMEPAD )) }; // Invoked when received GET HID REPORT DESCRIPTOR diff --git a/examples/device/hid_composite/src/usb_descriptors.h b/examples/device/hid_composite/src/usb_descriptors.h index feda83dc..7894719f 100644 --- a/examples/device/hid_composite/src/usb_descriptors.h +++ b/examples/device/hid_composite/src/usb_descriptors.h @@ -30,6 +30,7 @@ enum REPORT_ID_KEYBOARD = 1, REPORT_ID_MOUSE, REPORT_ID_CONSUMER_CONTROL, + REPORT_ID_GAMEPAD }; #endif /* USB_DESCRIPTORS_H_ */ diff --git a/examples/device/hid_multiple_interface/src/usb_descriptors.c b/examples/device/hid_multiple_interface/src/usb_descriptors.c index 2354e5c1..a28e57a2 100644 --- a/examples/device/hid_multiple_interface/src/usb_descriptors.c +++ b/examples/device/hid_multiple_interface/src/usb_descriptors.c @@ -140,11 +140,11 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) char const* string_desc_arr [] = { (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) - "TinyUSB", // 1: Manufacturer - "TinyUSB Device", // 2: Product - "123456", // 3: Serials, should use chip ID - "Keyboard Interface", // 4: Interface 1 String - "Mouse Interface", // 5: Interface 2 String + "TinyUSB", // 1: Manufacturer + "TinyUSB Device", // 2: Product + "123456", // 3: Serials, should use chip ID + "Keyboard Interface", // 4: Interface 1 String + "Mouse Interface", // 5: Interface 2 String }; static uint16_t _desc_str[32]; diff --git a/src/class/hid/hid.h b/src/class/hid/hid.h index cb1bd583..a7db087a 100644 --- a/src/class/hid/hid.h +++ b/src/class/hid/hid.h @@ -218,7 +218,7 @@ typedef enum //GAMEPAD_BUTTON_ = TU_BIT(15), ///< Undefined button }hid_gamepad_button_bm_t; -/// Standard Gamepad HAT/DPAD Buttons Bitmap (from Linux input event codes) +/// Standard Gamepad HAT/DPAD Buttons (from Linux input event codes) typedef enum { GAMEPAD_HAT_CENTERED = 0, ///< DPAD_CENTERED @@ -230,7 +230,7 @@ typedef enum GAMEPAD_HAT_DOWN_LEFT = 6, ///< DPAD_DOWN_LEFT GAMEPAD_HAT_LEFT = 7, ///< DPAD_LEFT GAMEPAD_HAT_UP_LEFT = 8, ///< DPAD_UP_LEFT -}hid_gamepad_hat_bm_t; +}hid_gamepad_hat_t; /// @} diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 1f03f886..5b973507 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -107,9 +107,6 @@ bool tud_hid_n_boot_mode(uint8_t itf) return _hidd_itf[itf].boot_mode; } -//--------------------------------------------------------------------+ -// KEYBOARD API -//--------------------------------------------------------------------+ bool tud_hid_n_keyboard_report(uint8_t itf, uint8_t report_id, uint8_t modifier, uint8_t keycode[6]) { hid_keyboard_report_t report; @@ -127,10 +124,8 @@ bool tud_hid_n_keyboard_report(uint8_t itf, uint8_t report_id, uint8_t modifier, return tud_hid_n_report(itf, report_id, &report, sizeof(report)); } -//--------------------------------------------------------------------+ -// MOUSE APPLICATION API -//--------------------------------------------------------------------+ -bool tud_hid_n_mouse_report(uint8_t itf, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) +bool tud_hid_n_mouse_report(uint8_t itf, uint8_t report_id, + uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) { hid_mouse_report_t report = { @@ -144,6 +139,24 @@ bool tud_hid_n_mouse_report(uint8_t itf, uint8_t report_id, uint8_t buttons, int return tud_hid_n_report(itf, report_id, &report, sizeof(report)); } +bool tud_hid_n_gamepad_report(uint8_t itf, uint8_t report_id, + int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons) +{ + hid_gamepad_report_t report = + { + .x = x, + .y = y, + .z = z, + .rz = rz, + .rx = rx, + .ry = ry, + .hat = hat, + .buttons = buttons, + }; + + return tud_hid_n_report(itf, report_id, &report, sizeof(report)); +} + //--------------------------------------------------------------------+ // USBD-CLASS API //--------------------------------------------------------------------+ diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index f429d8b9..a92dc14e 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -71,6 +71,10 @@ bool tud_hid_n_keyboard_report(uint8_t itf, uint8_t report_id, uint8_t modifier, // use template layout report as defined by hid_mouse_report_t bool tud_hid_n_mouse_report(uint8_t itf, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); +// Gamepad: convenient helper to send mouse report if application +// use template layout report TUD_HID_REPORT_DESC_GAMEPAD +bool tud_hid_n_gamepad_report(uint8_t itf, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint16_t buttons); + //--------------------------------------------------------------------+ // Application API (Single Port) //--------------------------------------------------------------------+ @@ -119,6 +123,8 @@ TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); #endif +// TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t itf, ); + //--------------------------------------------------------------------+ // Inline Functions