mirror of
https://github.com/hathach/tinyusb.git
synced 2025-02-20 18:40:57 +00:00
simplify hid keyboard & mouse report to one API each
This commit is contained in:
parent
0b6999a28e
commit
84f81f6b21
@ -196,7 +196,9 @@ void hid_task(void)
|
|||||||
if ( btn )
|
if ( btn )
|
||||||
{
|
{
|
||||||
int8_t const delta = 5;
|
int8_t const delta = 5;
|
||||||
tud_hid_mouse_move(REPORT_ID_MOUSE, delta, delta); // right + down
|
|
||||||
|
// no button, right + down, no scroll pan
|
||||||
|
tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
|
||||||
|
|
||||||
// delay a bit before attempt to send keyboard report
|
// delay a bit before attempt to send keyboard report
|
||||||
board_delay(2);
|
board_delay(2);
|
||||||
@ -220,7 +222,7 @@ void hid_task(void)
|
|||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
// send empty key report if previously has key pressed
|
// send empty key report if previously has key pressed
|
||||||
if (has_key) tud_hid_keyboard_key_release(REPORT_ID_KEYBOARD);
|
if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
|
||||||
has_key = false;
|
has_key = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,9 @@ void hid_task(void* params)
|
|||||||
if ( btn )
|
if ( btn )
|
||||||
{
|
{
|
||||||
int8_t const delta = 5;
|
int8_t const delta = 5;
|
||||||
tud_hid_mouse_move(REPORT_ID_MOUSE, delta, delta); // right + down
|
|
||||||
|
// no button, right + down, no scroll pan
|
||||||
|
tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
|
||||||
|
|
||||||
// delay a bit before attempt to send keyboard report
|
// delay a bit before attempt to send keyboard report
|
||||||
vTaskDelay(pdMS_TO_TICKS(2));
|
vTaskDelay(pdMS_TO_TICKS(2));
|
||||||
@ -250,7 +252,7 @@ void hid_task(void* params)
|
|||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
// send empty key report if previously has key pressed
|
// send empty key report if previously has key pressed
|
||||||
if (has_key) tud_hid_keyboard_key_release(REPORT_ID_KEYBOARD);
|
if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
|
||||||
has_key = false;
|
has_key = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,16 +47,11 @@ typedef struct
|
|||||||
{
|
{
|
||||||
uint8_t itf_num;
|
uint8_t itf_num;
|
||||||
uint8_t ep_in;
|
uint8_t ep_in;
|
||||||
uint8_t ep_out; // optional
|
uint8_t ep_out; // optional
|
||||||
|
|
||||||
|
|
||||||
uint8_t boot_protocol; // Boot mouse or keyboard
|
uint8_t boot_protocol; // Boot mouse or keyboard
|
||||||
bool boot_mode;
|
bool boot_mode; // default = false (Report)
|
||||||
|
uint8_t idle_rate; // up to application to handle idle rate
|
||||||
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[CFG_TUD_HID_BUFSIZE];
|
CFG_TUSB_MEM_ALIGN uint8_t report_buf[CFG_TUD_HID_BUFSIZE];
|
||||||
}hidd_interface_t;
|
}hidd_interface_t;
|
||||||
@ -101,7 +96,6 @@ 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 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) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +122,6 @@ bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycod
|
|||||||
tu_memclr(report.keycode, 6);
|
tu_memclr(report.keycode, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO skip duplication ? and or idle rate
|
|
||||||
return tud_hid_report(report_id, &report, sizeof(report));
|
return tud_hid_report(report_id, &report, sizeof(report));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,28 +139,9 @@ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y
|
|||||||
.pan = horizontal
|
.pan = horizontal
|
||||||
};
|
};
|
||||||
|
|
||||||
uint8_t itf = 0;
|
|
||||||
_hidd_itf[itf].mouse_button = buttons;
|
|
||||||
|
|
||||||
return tud_hid_report(report_id, &report, sizeof(report));
|
return tud_hid_report(report_id, &report, sizeof(report));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tud_hid_mouse_move(uint8_t report_id, int8_t x, int8_t y)
|
|
||||||
{
|
|
||||||
uint8_t itf = 0;
|
|
||||||
uint8_t const button = _hidd_itf[itf].mouse_button;
|
|
||||||
|
|
||||||
return tud_hid_mouse_report(report_id, button, x, y, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool tud_hid_mouse_scroll(uint8_t report_id, int8_t vertical, int8_t horizontal)
|
|
||||||
{
|
|
||||||
uint8_t itf = 0;
|
|
||||||
uint8_t const button = _hidd_itf[itf].mouse_button;
|
|
||||||
|
|
||||||
return tud_hid_mouse_report(report_id, button, 0, 0, vertical, horizontal);
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// USBD-CLASS API
|
// USBD-CLASS API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
@ -65,42 +65,24 @@ void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uin
|
|||||||
// Invoked when host switch mode Boot <-> Report via SET_PROTOCOL request
|
// Invoked when host switch mode Boot <-> Report via SET_PROTOCOL request
|
||||||
ATTR_WEAK void tud_hid_boot_mode_cb(uint8_t boot_mode);
|
ATTR_WEAK void tud_hid_boot_mode_cb(uint8_t boot_mode);
|
||||||
|
|
||||||
// Invoked when host send SET_IDLE request
|
// Invoked when host send SET_IDLE request. return false will stall the request
|
||||||
// return false will stall the request
|
// - 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).
|
||||||
ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate);
|
ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate);
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// KEYBOARD API
|
// KEYBOARD: convenient helper to send keyboard report if application
|
||||||
// Convenient helper to send keyboard report if application use standard/boot
|
// use template layout report as defined by hid_keyboard_report_t
|
||||||
// layout report as defined by hid_keyboard_report_t
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
bool tud_hid_keyboard_report(uint8_t report_id, uint8_t modifier, uint8_t keycode[6]);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// MOUSE API
|
// MOUSE: convenient helper to send mouse report if application
|
||||||
// Convenient helper to send mouse report if application use standard/boot
|
// use template layout report as defined by hid_mouse_report_t
|
||||||
// layout report as defined by hid_mouse_report_t
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal);
|
bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, 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 vertical, int8_t horizontal);
|
|
||||||
|
|
||||||
static inline bool tud_hid_mouse_button_press(uint8_t report_id, uint8_t buttons)
|
|
||||||
{
|
|
||||||
return tud_hid_mouse_report(report_id, buttons, 0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool tud_hid_mouse_button_release(uint8_t report_id)
|
|
||||||
{
|
|
||||||
return tud_hid_mouse_report(report_id, 0, 0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* --------------------------------------------------------------------+
|
/* --------------------------------------------------------------------+
|
||||||
* HID Report Descriptor Template
|
* HID Report Descriptor Template
|
||||||
|
Loading…
x
Reference in New Issue
Block a user