From ad845db6a50c1d90e4f7cc45ca16a8861ee2da4d Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 22 May 2021 20:54:59 +0700 Subject: improve hid parser --- examples/host/cdc_msc_hid/src/hid_app.c | 50 ++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'examples/host') diff --git a/examples/host/cdc_msc_hid/src/hid_app.c b/examples/host/cdc_msc_hid/src/hid_app.c index eab555fc1..71904245a 100644 --- a/examples/host/cdc_msc_hid/src/hid_app.c +++ b/examples/host/cdc_msc_hid/src/hid_app.c @@ -37,14 +37,16 @@ static uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII }; +// Each HID instance can has multiple reports static uint8_t _report_count[CFG_TUH_HID]; -static tuh_hid_report_info_t _report_info[CFG_TUH_HID][MAX_REPORT]; +static tuh_hid_report_info_t _report_info_arr[CFG_TUH_HID][MAX_REPORT]; static void process_kbd_report(hid_keyboard_report_t const *report); static void process_mouse_report(hid_mouse_report_t const * report); void hid_app_task(void) { + // nothing to do } //--------------------------------------------------------------------+ @@ -55,21 +57,13 @@ void tuh_hid_mounted_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_ { printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance); - // Parse report descriptor with built-in parser - _report_count[instance] = tuh_hid_parse_report_descriptor(_report_info[instance], MAX_REPORT, desc_report, desc_len); - - if (_report_count[instance]) - { - printf("Composite with %u reports", _report_count[instance]); - }else - { - printf("Single report"); - } - // Interface protocol const char* protocol_str[] = { "None", "Keyboard", "Mouse" }; // hid_protocol_type_t uint8_t const interface_protocol = tuh_hid_interface_protocol(dev_addr, instance); - printf(", Interface protocol = %s, ", protocol_str[interface_protocol]); + + // Parse report descriptor with built-in parser + _report_count[instance] = tuh_hid_parse_report_descriptor(_report_info_arr[instance], MAX_REPORT, desc_report, desc_len); + printf("HID has %u reports and interface protocol = %s\r\n", _report_count[instance], protocol_str[interface_protocol]); } void tuh_hid_unmounted_cb(uint8_t dev_addr, uint8_t instance) @@ -82,20 +76,36 @@ void tuh_hid_get_report_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* re (void) dev_addr; uint8_t const rpt_count = _report_count[instance]; - tuh_hid_report_info_t* rpt_info; + tuh_hid_report_info_t* rpt_info_arr = _report_info_arr[instance]; + tuh_hid_report_info_t* rpt_info = NULL; - if (rpt_count) + if ( rpt_count == 1 && rpt_info_arr[0].report_id == 0) + { + // Simple report without report ID as 1st byte + rpt_info = &rpt_info_arr[0]; + }else { // Composite report, 1st byte is report ID, data starts from 2nd byte - // Note: report index = report ID - 1 - uint8_t idx = report[0] - 1; - rpt_info = &_report_info[instance][idx]; + uint8_t const rpt_id = report[0]; + + // Find report id in the arrray + for(uint8_t i=0; iusage_page == HID_USAGE_PAGE_DESKTOP ) -- cgit v1.3.1