From b7a8b278c83b9b8e03a328e17f4b833f925226a2 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 May 2021 21:40:17 +0700 Subject: [PATCH] rename tuh_device_is_configured() to tuh_device_configured() - remove tuh_device_get_state() - more hid mouse clean up --- .../host/cdc_msc_hid/src/keyboard_helper.h | 59 ------------------- examples/host/cdc_msc_hid/src/main.c | 12 ++-- src/class/hid/hid_host.c | 38 ++++++++---- src/class/hid/hid_host.h | 19 ++---- src/host/usbh.c | 5 +- src/host/usbh.h | 6 +- 6 files changed, 42 insertions(+), 97 deletions(-) delete mode 100644 examples/host/cdc_msc_hid/src/keyboard_helper.h diff --git a/examples/host/cdc_msc_hid/src/keyboard_helper.h b/examples/host/cdc_msc_hid/src/keyboard_helper.h deleted file mode 100644 index 1fde2768c..000000000 --- a/examples/host/cdc_msc_hid/src/keyboard_helper.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef KEYBOARD_HELPER_H -#define KEYBAORD_HELPER_H - -#include -#include - -#include "tusb.h" - -// look up new key in previous keys -inline bool find_key_in_report(hid_keyboard_report_t const *p_report, uint8_t keycode) -{ - for(uint8_t i = 0; i < 6; i++) - { - if (p_report->keycode[i] == keycode) return true; - } - - return false; -} - -inline uint8_t keycode_to_ascii(uint8_t modifier, uint8_t keycode) -{ - return keycode > 128 ? 0 : - hid_keycode_to_ascii_tbl [keycode][modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT) ? 1 : 0]; -} - -void print_kbd_report(hid_keyboard_report_t *prev_report, hid_keyboard_report_t const *new_report) -{ - - printf("Report: "); - uint8_t c; - - // I assume it's possible to have up to 6 keypress events per report? - for (uint8_t i = 0; i < 6; i++) - { - // Check for key presses - if (new_report->keycode[i]) - { - // If not in prev report then it is newly pressed - if ( !find_key_in_report(prev_report, new_report->keycode[i]) ) - c = keycode_to_ascii(new_report->modifier, new_report->keycode[i]); - printf("press %c ", c); - } - - // Check for key depresses (i.e. was present in prev report but not here) - if (prev_report->keycode[i]) - { - // If not present in the current report then depressed - if (!find_key_in_report(new_report, prev_report->keycode[i])) - { - c = keycode_to_ascii(prev_report->modifier, prev_report->keycode[i]); - printf("depress %c ", c); - } - } - } - - printf("\n"); -} - -#endif \ No newline at end of file diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index ccc50bbf2..6b2d0affb 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -156,16 +156,18 @@ static inline void process_kbd_report(hid_keyboard_report_t const *p_new_report) void tuh_hid_mounted_cb(uint8_t dev_addr, uint8_t instance) { - // application set-up - printf("A Keyboard device (address %d) is mounted\r\n", dev_addr); + printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance); +// printf("A Keyboard device (address %d) is mounted\r\n", dev_addr); + if (instance == 0) { tuh_hid_keyboard_get_report(dev_addr, &usb_keyboard_report); + } } void tuh_hid_unmounted_cb(uint8_t dev_addr, uint8_t instance) { - // application tear-down - printf("A Keyboard device (address %d) is unmounted\r\n", dev_addr); + printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); +// printf("A Keyboard device (address %d) is unmounted\r\n", dev_addr); } #endif @@ -255,7 +257,7 @@ void hid_task(void) #endif #if CFG_TUH_HID_MOUSE - if ( tuh_hid_mouse_mounted(addr) ) + if ( tuh_n_hid_n_mouse_mounted(addr, 0) ) { if ( !tuh_hid_mouse_is_busy(addr) ) { diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index 80680894f..72cecb8d1 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -104,7 +104,7 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int { //------------- parameters validation -------------// // TODO change to use is configured function - TU_ASSERT(TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY); + TU_ASSERT(tuh_device_configured(dev_addr), TUSB_ERROR_DEVICE_NOT_READY); TU_VERIFY(report, TUSB_ERROR_INVALID_PARA); TU_VERIFY(!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY); @@ -113,32 +113,45 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int return TUSB_ERROR_NONE; } +//bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance) +//{ +// +//} + //--------------------------------------------------------------------+ // KEYBOARD //--------------------------------------------------------------------+ -bool tuh_n_hid_n_keyboard_mounted(uint8_t dev_addr, uint8_t instance) +bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance) { - hidh_interface_t* hid_itf = get_instance(dev_addr, instance); + hidh_interface_t* hid_itf = get_instance(daddr, instance); // TODO check rid_keyboard - return tuh_device_is_configured(dev_addr) && (hid_itf->ep_in != 0); + return tuh_device_configured(daddr) && (hid_itf->ep_in != 0); +} + +bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance) +{ + hidh_interface_t* hid_itf = get_instance(daddr, instance); + + // TODO check rid_keyboard + return tuh_device_configured(daddr) && (hid_itf->ep_in != 0); } tusb_error_t tuh_hid_keyboard_get_report(uint8_t dev_addr, void* buffer) { - uint8_t itf = 0; - hidh_interface_t* hid_itf = get_instance(dev_addr, itf); + uint8_t inst = 0; + hidh_interface_t* hid_itf = get_instance(dev_addr, inst); return hidh_interface_get_report(dev_addr, buffer, hid_itf); } bool tuh_hid_keyboard_is_busy(uint8_t dev_addr) { - uint8_t instance = 0; - hidh_interface_t* hid_itf = get_instance(dev_addr, instance); + uint8_t inst = 0; + hidh_interface_t* hid_itf = get_instance(dev_addr, inst); - return tuh_n_hid_n_keyboard_mounted(dev_addr, instance) && hcd_edpt_busy(dev_addr, hid_itf->ep_in); + return tuh_n_hid_n_keyboard_mounted(dev_addr, inst) && hcd_edpt_busy(dev_addr, hid_itf->ep_in); } //--------------------------------------------------------------------+ @@ -149,14 +162,15 @@ bool tuh_hid_keyboard_is_busy(uint8_t dev_addr) static hidh_interface_t mouseh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1 //------------- Public API -------------// -bool tuh_hid_mouse_mounted(uint8_t dev_addr) +bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance) { - return tuh_device_is_configured(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0); +// hidh_interface_t* hid_itf = get_instance(dev_addr, instance); + return tuh_device_configured(dev_addr) && (mouseh_data[dev_addr-1].ep_in != 0); } bool tuh_hid_mouse_is_busy(uint8_t dev_addr) { - return tuh_hid_mouse_mounted(dev_addr) && hcd_edpt_busy(dev_addr, mouseh_data[dev_addr-1].ep_in); + return tuh_n_hid_n_mouse_mounted(dev_addr, 0) && hcd_edpt_busy(dev_addr, mouseh_data[dev_addr-1].ep_in); } tusb_error_t tuh_hid_mouse_get_report(uint8_t dev_addr, void * report) diff --git a/src/class/hid/hid_host.h b/src/class/hid/hid_host.h index c45ca9bd4..0052a2b0c 100644 --- a/src/class/hid/hid_host.h +++ b/src/class/hid/hid_host.h @@ -60,10 +60,14 @@ // Get the number of HID instances uint8_t tuh_n_hid_instance_count(uint8_t daddr); -// Check if HID instance has keyboard +// Check if HID instance is mounted +//bool tuh_n_hid_n_mounted(uint8_t daddr, uint8_t instance); + +// Check if HID instance with Keyboard is mounted bool tuh_n_hid_n_keyboard_mounted(uint8_t daddr, uint8_t instance); - +// Check if HID instance with Mouse is mounted +bool tuh_n_hid_n_mouse_mounted(uint8_t dev_addr, uint8_t instance); //--------------------------------------------------------------------+ // Application API (Single device) @@ -101,10 +105,6 @@ TU_ATTR_WEAK void tuh_hid_unmounted_cb(uint8_t dev_addr, uint8_t instance); * The interface API includes status checking function, data transferring function and callback functions * @{ */ -// TODO used weak attr if build failed without KEYBOARD enabled -// TODO remove -extern uint8_t const hid_keycode_to_ascii_tbl[2][128]; - /** \brief Check if device supports Keyboard interface or not * \param[in] dev_addr device address * \retval true if device supports Keyboard interface @@ -170,13 +170,6 @@ void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr); * The interface API includes status checking function, data transferring function and callback functions * @{ */ -/** \brief Check if device supports Mouse interface or not - * \param[in] dev_addr device address - * \retval true if device supports Mouse interface - * \retval false if device does not support Mouse interface or is not mounted - */ -bool tuh_hid_mouse_mounted(uint8_t dev_addr); - /** \brief Check if the interface is currently busy or not * \param[in] dev_addr device address * \retval true if the interface is busy meaning the stack is still transferring/waiting data from/to device diff --git a/src/host/usbh.c b/src/host/usbh.c index 7a021c11c..f1c386a4d 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -152,10 +152,9 @@ uint8_t* usbh_get_enum_buf(void) //--------------------------------------------------------------------+ // PUBLIC API (Parameter Verification is required) //--------------------------------------------------------------------+ -tusb_device_state_t tuh_device_get_state (uint8_t const dev_addr) +bool tuh_device_configured(uint8_t dev_addr) { - TU_ASSERT( dev_addr <= CFG_TUSB_HOST_DEVICE_MAX, TUSB_DEVICE_STATE_UNPLUG); - return (tusb_device_state_t) _usbh_devices[dev_addr].state; + return _usbh_devices[dev_addr].configured; } tusb_speed_t tuh_device_get_speed (uint8_t const dev_addr) diff --git a/src/host/usbh.h b/src/host/usbh.h index 5e7abe868..e503ef8fb 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -88,13 +88,9 @@ void tuh_task(void); extern void hcd_int_handler(uint8_t rhport); #define tuh_int_handler hcd_int_handler -tusb_device_state_t tuh_device_get_state (uint8_t dev_addr); tusb_speed_t tuh_device_get_speed (uint8_t dev_addr); -static inline bool tuh_device_is_configured(uint8_t dev_addr) -{ - return tuh_device_get_state(dev_addr) == TUSB_DEVICE_STATE_CONFIGURED; -} +bool tuh_device_configured(uint8_t dev_addr); bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, void* buffer, tuh_control_complete_cb_t complete_cb); //--------------------------------------------------------------------+