Skip to content

Commit

Permalink
Instance & interface number conversion
Browse files Browse the repository at this point in the history
- Search for an instance by an interface and vice versa
- Allows to use the HID functions by interface numbers
  • Loading branch information
RockyZeroFour committed May 28, 2024
1 parent ac3c2c5 commit e111148
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/class/hid/hid_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,47 @@ 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)
//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
uint8_t tud_hid_get_instance(uint8_t itf_num)
{
for (uint8_t i=0; i < CFG_TUD_HID; i++ )
// Search for the interface
for (uint8_t i=0; i < CFG_TUD_HID; i++ )
{
if ( itf_num == _hidd_itf[i].itf_num ) return i;
// Skip current instance if that isn't assigned yet
if (0 == _hidd_itf[i].ep_in && 0 == _hidd_itf[i].ep_out)
{
continue;
}

if (itf_num == _hidd_itf[i].itf_num)
{
return i;
}
}

return 0xFF;
}

//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
uint8_t tud_hid_get_itf_num(uint8_t instance)
{
// Abort if an invalid instance number was given
if (CFG_TUD_HID <= instance)
{
return 0xFF;
}

// Abort if the instance hasn't a valid interface assigned yet
if (0 == _hidd_itf[instance].ep_in && 0 == _hidd_itf[instance].ep_out)
{
return 0xFF;
}

// Return the interface number
return _hidd_itf[instance].itf_num;
}

bool tud_hid_n_ready(uint8_t instance)
{
uint8_t const ep_in = _hidd_itf[instance].ep_in;
Expand Down Expand Up @@ -241,7 +268,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((uint8_t) request->wIndex);
TU_VERIFY(hid_itf < CFG_TUD_HID);

hidd_interface_t* p_hid = &_hidd_itf[hid_itf];
Expand Down
6 changes: 6 additions & 0 deletions src/class/hid/hid_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
// CFG_TUD_HID > 1
//--------------------------------------------------------------------+

// Returns the instance of the given interface or 0xFF if not available
uint8_t tud_hid_get_instance(uint8_t itf_num);

// Returns the interface number of the given instance or 0xFF if not available
uint8_t tud_hid_get_itf_num(uint8_t instance);

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

Expand Down

0 comments on commit e111148

Please sign in to comment.