update tud_hid_keyboard/mouse helper
This commit is contained in:
@@ -153,11 +153,10 @@ void usb_hid_task(void)
|
|||||||
uint8_t keycode[6] = { 0 };
|
uint8_t keycode[6] = { 0 };
|
||||||
keycode[0] = HID_KEY_A;
|
keycode[0] = HID_KEY_A;
|
||||||
|
|
||||||
tud_hid_keyboard_keycode(0, keycode);
|
tud_hid_keyboard_report(0, 0, keycode);
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
// Null means all zeroes keycodes
|
tud_hid_keyboard_key_release(0);
|
||||||
tud_hid_keyboard_keycode(0, NULL);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@@ -172,11 +172,11 @@ void usb_hid_task(void* params)
|
|||||||
if ( btn & (1 << i) ) keycode[i] = HID_KEY_A + i;
|
if ( btn & (1 << i) ) keycode[i] = HID_KEY_A + i;
|
||||||
}
|
}
|
||||||
|
|
||||||
tud_hid_keyboard_keycode(0, keycode);
|
tud_hid_keyboard_report(0, keycode);
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
// Null means all zeroes keycodes
|
// Null means all zeroes keycodes
|
||||||
tud_hid_keyboard_keycode(0, NULL);
|
tud_hid_keyboard_report(0, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -179,7 +179,7 @@ typedef enum
|
|||||||
/// Standard HID Boot Protocol Keyboard Report.
|
/// Standard HID Boot Protocol Keyboard Report.
|
||||||
typedef struct ATTR_PACKED
|
typedef struct ATTR_PACKED
|
||||||
{
|
{
|
||||||
uint8_t modifier; /**< Keyboard modifier byte, indicating pressed modifier keys (a combination of HID_KEYBOARD_MODIFER_* masks). */
|
uint8_t modifier; /**< Keyboard modifier (KEYBOARD_MODIFER_* masks). */
|
||||||
uint8_t reserved; /**< Reserved for OEM use, always set to 0. */
|
uint8_t reserved; /**< Reserved for OEM use, always set to 0. */
|
||||||
uint8_t keycode[6]; /**< Key codes of the currently pressed keys. */
|
uint8_t keycode[6]; /**< Key codes of the currently pressed keys. */
|
||||||
} hid_keyboard_report_t;
|
} hid_keyboard_report_t;
|
||||||
|
@@ -39,22 +39,23 @@
|
|||||||
// MACRO CONSTANT TYPEDEF
|
// MACRO CONSTANT TYPEDEF
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
#define REPORT_BUFSIZE 16
|
#ifndef CFG_TUD_HID_BUFSIZE
|
||||||
|
#define CFG_TUD_HID_BUFSIZE 16
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t itf_num;
|
uint8_t itf_num;
|
||||||
uint8_t ep_in;
|
uint8_t ep_in;
|
||||||
|
|
||||||
uint8_t idle_rate; // Idle Rate = 0 : only send report if there is changes, i.e skip duplication
|
|
||||||
// Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms).
|
|
||||||
// If idle time is less than interrupt polling then use the polling.
|
|
||||||
uint8_t boot_protocol; // Boot mouse or keyboard
|
uint8_t boot_protocol; // Boot mouse or keyboard
|
||||||
bool boot_mode;
|
bool boot_mode;
|
||||||
|
|
||||||
uint16_t reprot_desc_len;
|
uint16_t reprot_desc_len;
|
||||||
|
uint8_t idle_rate; // Idle Rate = 0 : only send report if there is changes, i.e skip duplication
|
||||||
|
// Idle Rate > 0 : skip duplication, but send at least 1 report every idle rate (in unit of 4 ms).
|
||||||
|
uint8_t mouse_button; // caching button for using with tud_hid_mouse_ API
|
||||||
|
|
||||||
CFG_TUSB_MEM_ALIGN uint8_t report_buf[REPORT_BUFSIZE];
|
CFG_TUSB_MEM_ALIGN uint8_t report_buf[CFG_TUD_HID_BUFSIZE];
|
||||||
}hidd_interface_t;
|
}hidd_interface_t;
|
||||||
|
|
||||||
CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[CFG_TUD_HID];
|
CFG_TUSB_MEM_SECTION static hidd_interface_t _hidd_itf[CFG_TUD_HID];
|
||||||
@@ -71,7 +72,7 @@ static inline hidd_interface_t* get_interface_by_itfnum(uint8_t itf_num)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// HID GENERIC API
|
// APPLICATION API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
bool tud_hid_ready(void)
|
bool tud_hid_ready(void)
|
||||||
{
|
{
|
||||||
@@ -82,7 +83,7 @@ bool tud_hid_ready(void)
|
|||||||
|
|
||||||
bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len)
|
bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len)
|
||||||
{
|
{
|
||||||
TU_VERIFY( tud_hid_ready() && (len < REPORT_BUFSIZE) );
|
TU_VERIFY( tud_hid_ready() && (len < CFG_TUD_HID_BUFSIZE) );
|
||||||
|
|
||||||
uint8_t itf = 0;
|
uint8_t itf = 0;
|
||||||
hidd_interface_t * p_hid = &_hidd_itf[itf];
|
hidd_interface_t * p_hid = &_hidd_itf[itf];
|
||||||
@@ -97,8 +98,8 @@ bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len)
|
|||||||
memcpy(p_hid->report_buf, report, len);
|
memcpy(p_hid->report_buf, report, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO idle rate
|
// TODO skip duplication ? and or idle rate
|
||||||
return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->report_buf, len + ( report_id ? 1 : 0) );
|
return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->report_buf, len + (report_id ? 1 : 0) );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tud_hid_boot_mode(void)
|
bool tud_hid_boot_mode(void)
|
||||||
@@ -108,30 +109,13 @@ bool tud_hid_boot_mode(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// KEYBOARD APPLICATION API
|
// KEYBOARD API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
#if CFG_TUD_HID_KEYBOARD
|
bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6])
|
||||||
static bool hidd_kbd_report(hid_keyboard_report_t const *p_report)
|
|
||||||
{
|
{
|
||||||
TU_VERIFY( tud_hid_ready() );
|
hid_keyboard_report_t report;
|
||||||
|
|
||||||
uint8_t itf = 0;
|
report.modifier = modifier;
|
||||||
hidd_interface_t * p_hid = &_hidd_itf[itf];
|
|
||||||
|
|
||||||
// only send report if there is changes, i.e skip duplication
|
|
||||||
// if ( _kbd_rpt.idle_rate == 0 )
|
|
||||||
// {
|
|
||||||
// if ( 0 == memcmp(p_hid->report_buf, p_report, sizeof(hid_keyboard_report_t)) ) return true;
|
|
||||||
// }
|
|
||||||
|
|
||||||
memcpy(p_hid->report_buf, p_report, sizeof(hid_keyboard_report_t));
|
|
||||||
|
|
||||||
return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->report_buf, sizeof(hid_keyboard_report_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool tud_hid_keyboard_keycode(uint8_t modifier, uint8_t keycode[6])
|
|
||||||
{
|
|
||||||
hid_keyboard_report_t report = { .modifier = modifier };
|
|
||||||
|
|
||||||
if ( keycode )
|
if ( keycode )
|
||||||
{
|
{
|
||||||
@@ -141,12 +125,12 @@ bool tud_hid_keyboard_keycode(uint8_t modifier, uint8_t keycode[6])
|
|||||||
tu_memclr(report.keycode, 6);
|
tu_memclr(report.keycode, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
return hidd_kbd_report(&report);
|
// TODO skip duplication ? and or idle rate
|
||||||
|
return tud_hid_report(report_id, &report, sizeof(report));
|
||||||
}
|
}
|
||||||
|
|
||||||
#if CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
#if CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
||||||
|
bool tud_hid_keyboard_key_press(uint8_t report_id, char ch)
|
||||||
bool tud_hid_keyboard_key_press(char ch)
|
|
||||||
{
|
{
|
||||||
uint8_t keycode[6] = { 0 };
|
uint8_t keycode[6] = { 0 };
|
||||||
uint8_t modifier = 0;
|
uint8_t modifier = 0;
|
||||||
@@ -154,70 +138,45 @@ bool tud_hid_keyboard_key_press(char ch)
|
|||||||
if ( HID_ASCII_TO_KEYCODE[(uint8_t)ch].shift ) modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
|
if ( HID_ASCII_TO_KEYCODE[(uint8_t)ch].shift ) modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
|
||||||
keycode[0] = HID_ASCII_TO_KEYCODE[(uint8_t)ch].keycode;
|
keycode[0] = HID_ASCII_TO_KEYCODE[(uint8_t)ch].keycode;
|
||||||
|
|
||||||
return tud_hid_keyboard_keycode(modifier, keycode);
|
return tud_hid_keyboard_report(report_id, modifier, keycode);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
#endif // CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
||||||
|
|
||||||
#endif // CFG_TUD_HID_KEYBOARD
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// MOUSE APPLICATION API
|
// MOUSE APPLICATION API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
#if CFG_TUD_HID_MOUSE
|
bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan)
|
||||||
|
|
||||||
static bool hidd_mouse_report(hid_mouse_report_t const *p_report)
|
|
||||||
{
|
|
||||||
TU_VERIFY( tud_hid_ready() );
|
|
||||||
|
|
||||||
uint8_t itf = 0;
|
|
||||||
hidd_interface_t * p_hid = &_hidd_itf[itf];
|
|
||||||
|
|
||||||
// only send report if there is changes, i.e skip duplication
|
|
||||||
memcpy(p_hid->report_buf, p_report, sizeof(hid_mouse_report_t));
|
|
||||||
|
|
||||||
return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->report_buf, sizeof(hid_mouse_report_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool tud_hid_mouse_data(uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan)
|
|
||||||
{
|
{
|
||||||
hid_mouse_report_t report =
|
hid_mouse_report_t report =
|
||||||
{
|
{
|
||||||
.buttons = buttons,
|
.buttons = buttons,
|
||||||
.x = x,
|
.x = x,
|
||||||
.y = y,
|
.y = y,
|
||||||
.wheel = scroll,
|
.wheel = scroll,
|
||||||
// .pan = pan
|
//.pan = pan
|
||||||
};
|
};
|
||||||
|
|
||||||
return hidd_mouse_report( &report );
|
|
||||||
}
|
|
||||||
|
|
||||||
bool tud_hid_mouse_move(int8_t x, int8_t y)
|
|
||||||
{
|
|
||||||
TU_VERIFY( tud_hid_mouse_ready() );
|
|
||||||
|
|
||||||
uint8_t itf = 0;
|
uint8_t itf = 0;
|
||||||
hidd_interface_t * p_hid = &_hidd_itf[itf];
|
_hidd_itf[itf].mouse_button = buttons;
|
||||||
|
|
||||||
uint8_t prev_buttons = p_hid->report_buf[0];
|
return tud_hid_report(report_id, &report, sizeof(report));
|
||||||
|
|
||||||
return tud_hid_mouse_data(prev_buttons, x, y, 0, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tud_hid_mouse_scroll(int8_t vertical, int8_t horizontal)
|
bool tud_hid_mouse_move(uint8_t report_id, int8_t x, int8_t y)
|
||||||
{
|
{
|
||||||
TU_VERIFY( tud_hid_mouse_ready() );
|
|
||||||
|
|
||||||
uint8_t itf = 0;
|
uint8_t itf = 0;
|
||||||
hidd_interface_t * p_hid = &_hidd_itf[itf];
|
uint8_t const button = _hidd_itf[itf].mouse_button;
|
||||||
|
|
||||||
uint8_t prev_buttons = p_hid->report_buf[0];
|
return tud_hid_mouse_report(report_id, button, x, y, 0, 0);
|
||||||
|
|
||||||
return tud_hid_mouse_data(prev_buttons, 0, 0, vertical, horizontal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // CFG_TUD_HID_MOUSE
|
bool tud_hid_mouse_scroll(uint8_t report_id, int8_t scroll, int8_t pan)
|
||||||
|
{
|
||||||
|
uint8_t itf = 0;
|
||||||
|
uint8_t const button = _hidd_itf[itf].mouse_button;
|
||||||
|
|
||||||
|
return tud_hid_mouse_report(report_id, button, 0, 0, scroll, pan);
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// USBD-CLASS API
|
// USBD-CLASS API
|
||||||
@@ -237,9 +196,6 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t
|
|||||||
{
|
{
|
||||||
uint8_t const *p_desc = (uint8_t const *) desc_itf;
|
uint8_t const *p_desc = (uint8_t const *) desc_itf;
|
||||||
|
|
||||||
// TODO support HID OUT Endpoint
|
|
||||||
TU_ASSERT(desc_itf->bNumEndpoints == 1);
|
|
||||||
|
|
||||||
//------------- HID descriptor -------------//
|
//------------- HID descriptor -------------//
|
||||||
p_desc = tu_desc_next(p_desc);
|
p_desc = tu_desc_next(p_desc);
|
||||||
tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
|
tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc;
|
||||||
@@ -381,11 +337,8 @@ bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*------------------------------------------------------------------*/
|
//------------- Ascii to Keycode Lookup -------------//
|
||||||
/* Ascii to Keycode
|
|
||||||
*------------------------------------------------------------------*/
|
|
||||||
#if CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
#if CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
||||||
|
|
||||||
const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128] =
|
const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128] =
|
||||||
{
|
{
|
||||||
{0, 0 }, // 0x00 Null
|
{0, 0 }, // 0x00 Null
|
||||||
@@ -520,7 +473,6 @@ const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128] =
|
|||||||
{1, HID_KEY_GRAVE }, // 0x7E ~
|
{1, HID_KEY_GRAVE }, // 0x7E ~
|
||||||
{0, HID_KEY_DELETE } // 0x7F Delete
|
{0, HID_KEY_DELETE } // 0x7F Delete
|
||||||
};
|
};
|
||||||
|
#endif // CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -66,6 +66,7 @@ bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len);
|
|||||||
bool tud_hid_boot_mode(void);
|
bool tud_hid_boot_mode(void);
|
||||||
|
|
||||||
/*------------- Callbacks (Weak is optional) -------------*/
|
/*------------- Callbacks (Weak is optional) -------------*/
|
||||||
|
|
||||||
/** Callback invoked when USB host request \ref HID_REQ_CONTROL_GET_REPORT.
|
/** Callback invoked when USB host request \ref HID_REQ_CONTROL_GET_REPORT.
|
||||||
* \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests
|
* \param[in] report_type specify which report (INPUT, OUTPUT, FEATURE) that host requests
|
||||||
* \param[out] buffer data that application need to update, value must be accessible by USB controller (see \ref CFG_TUSB_MEM_SECTION)
|
* \param[out] buffer data that application need to update, value must be accessible by USB controller (see \ref CFG_TUSB_MEM_SECTION)
|
||||||
@@ -90,16 +91,22 @@ void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type,
|
|||||||
|
|
||||||
ATTR_WEAK void tud_hid_mode_changed_cb(uint8_t boot_mode);
|
ATTR_WEAK void tud_hid_mode_changed_cb(uint8_t boot_mode);
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// KEYBOARD API
|
// KEYBOARD API
|
||||||
|
// Convenient helper to send keyboard report if application use standard/boot
|
||||||
|
// layout report as defined by hid_keyboard_report_t
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
#if CFG_TUD_HID_KEYBOARD
|
|
||||||
bool tud_hid_keyboard_keycode(uint8_t modifier, uint8_t keycode[6]);
|
|
||||||
|
|
||||||
static inline bool tud_hid_keyboard_key_release(void) { return tud_hid_keyboard_keycode(0, NULL); }
|
bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
|
||||||
|
|
||||||
|
static inline bool tud_hid_keyboard_key_release(uint8_t report_id)
|
||||||
|
{
|
||||||
|
return tud_hid_keyboard_report(report_id, 0, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
#if CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
#if CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP
|
||||||
bool tud_hid_keyboard_key_press(char ch);
|
bool tud_hid_keyboard_key_press(uint8_t report_id, char ch);
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
uint8_t shift;
|
uint8_t shift;
|
||||||
@@ -108,30 +115,26 @@ typedef struct{
|
|||||||
extern const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128];
|
extern const hid_ascii_to_keycode_entry_t HID_ASCII_TO_KEYCODE[128];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// MOUSE API
|
// MOUSE API
|
||||||
|
// Convenient helper to send mouse report if application use standard/boot
|
||||||
|
// layout report as defined by hid_mouse_report_t
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
#if CFG_TUD_HID_MOUSE
|
|
||||||
bool tud_hid_mouse_data(uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan);
|
|
||||||
|
|
||||||
bool tud_hid_mouse_move(int8_t x, int8_t y);
|
bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t scroll, int8_t pan);
|
||||||
bool tud_hid_mouse_scroll(int8_t vertical, int8_t horizontal);
|
bool tud_hid_mouse_move(uint8_t report_id, int8_t x, int8_t y);
|
||||||
|
bool tud_hid_mouse_scroll(uint8_t report_id, int8_t scroll, int8_t pan);
|
||||||
|
|
||||||
static inline bool tud_hid_mouse_button_press(uint8_t buttons)
|
static inline bool tud_hid_mouse_button_press(uint8_t report_id, uint8_t buttons)
|
||||||
{
|
{
|
||||||
return tud_hid_mouse_data(buttons, 0, 0, 0, 0);
|
return tud_hid_mouse_report(report_id, buttons, 0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool tud_hid_mouse_button_release(void)
|
static inline bool tud_hid_mouse_button_release(uint8_t report_id)
|
||||||
{
|
{
|
||||||
return tud_hid_mouse_data(0, 0, 0, 0, 0);
|
return tud_hid_mouse_report(report_id, 0, 0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// HID Report Descriptor Template
|
// HID Report Descriptor Template
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
@@ -113,7 +113,6 @@ bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, u
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// USBD-CLASS API
|
// USBD-CLASS API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
Reference in New Issue
Block a user