Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HID class improvements #11

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/class/hid/hid_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ typedef struct
CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[CFG_TUD_HID];

/*------------- Helpers -------------*/
static inline uint8_t get_index_by_itfnum(uint8_t itf_num)
{
for (uint8_t i=0; i < CFG_TUD_HID; i++ )
{
if ( itf_num == _hidd_itf[i].itf_num ) return i;
}

return 0xFF;
}

//--------------------------------------------------------------------+
// APPLICATION API
Expand All @@ -82,14 +73,30 @@ uint8_t tud_hid_get_itfnum_by_instance(uint8_t index)
return 0xFF;
}

uint8_t tud_hid_get_instance_by_itfnum(uint8_t index)
{
// Search for the interface index number
for (uint8_t i=0; i < CFG_TUD_HID; i++ )
{
// Only return the instance number when the interface is valid
if ( index == _hidd_itf[i].itf_num && (_hidd_itf[i].ep_in || _hidd_itf[i].ep_out) ) return i;
}

return 0xFF;
}

bool tud_hid_n_ready(uint8_t instance)
{
TU_VERIFY(instance < CFG_TUD_HID);

uint8_t const ep_in = _hidd_itf[instance].ep_in;
return tud_ready() && (ep_in != 0) && !usbd_edpt_busy(TUD_OPT_RHPORT, ep_in);
}

bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, uint16_t len)
{
TU_ASSERT(instance < CFG_TUD_HID);

uint8_t const rhport = 0;
hidd_interface_t * p_hid = &_hidd_itf[instance];

Expand Down Expand Up @@ -202,7 +209,7 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint1
uint8_t hid_id;
for(hid_id=0; hid_id<CFG_TUD_HID; hid_id++)
{
if ( _hidd_itf[hid_id].ep_in == 0 )
if ( _hidd_itf[hid_id].ep_in == 0 && _hidd_itf[hid_id].ep_out == 0 )
{
p_hid = &_hidd_itf[hid_id];
break;
Expand Down Expand Up @@ -249,7 +256,7 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
{
TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);

uint8_t const hid_itf = get_index_by_itfnum((uint8_t) request->wIndex);
uint8_t const hid_itf = tud_hid_get_instance_by_itfnum((uint8_t) request->wIndex);
TU_VERIFY(hid_itf < CFG_TUD_HID);

hidd_interface_t* p_hid = &_hidd_itf[hid_itf];
Expand Down
3 changes: 3 additions & 0 deletions src/class/hid/hid_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
// Get the interface number for the HID instance index
uint8_t tud_hid_get_itfnum_by_instance(uint8_t index);

// Get the HID instance number for the interface index
uint8_t tud_hid_get_instance_by_itfnum(uint8_t index);

// Check if the interface is ready to use
bool tud_hid_n_ready(uint8_t instance);

Expand Down
Loading