improve hid parser

This commit is contained in:
hathach
2021-05-22 20:54:59 +07:00
parent 350dfb2ea3
commit ad845db6a5
3 changed files with 50 additions and 62 deletions

View File

@@ -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; i<rpt_count; i++)
{
if (rpt_id == rpt_info_arr[i].report_id )
{
rpt_info = &rpt_info_arr[i];
break;
}
}
report++;
len--;
}else
}
if (!rpt_info)
{
rpt_info = &_report_info[instance][0];
printf("Couldn't find the report info for this report !\r\n");
return;
}
if ( rpt_info->usage_page == HID_USAGE_PAGE_DESKTOP )