refractor keyboard_device_app.c

refractor mouse_device_app.c app to only send report when necessary (movement or button changed)
This commit is contained in:
hathach 2014-04-24 15:59:35 +07:00
parent 8f81aa6c72
commit 9d404b9f00
2 changed files with 31 additions and 26 deletions

View File

@ -115,16 +115,15 @@ OSAL_TASK_FUNCTION( keyboard_device_app_task , p_task_para)
{ {
OSAL_TASK_LOOP_BEGIN OSAL_TASK_LOOP_BEGIN
osal_task_delay(100); osal_task_delay(50);
if ( tusbd_is_configured(0) ) if ( tusbd_is_configured(0) && !tusbd_hid_keyboard_is_busy(0) )
{ {
static uint32_t button_mask = 0; static uint32_t button_mask = 0;
uint32_t new_button_mask = board_buttons(); uint32_t new_button_mask = board_buttons();
//------------- button pressed -------------// //------------- button pressed -------------//
if ( (button_mask != new_button_mask) && !tusbd_hid_keyboard_is_busy(0) ) if (button_mask != new_button_mask)
{ {
button_mask = new_button_mask; button_mask = new_button_mask;

View File

@ -104,25 +104,25 @@ OSAL_TASK_FUNCTION( mouse_device_app_task , p_task_para)
{ {
OSAL_TASK_LOOP_BEGIN OSAL_TASK_LOOP_BEGIN
osal_task_delay(10); osal_task_delay(20);
if ( tusbd_is_configured(0) ) if ( tusbd_is_configured(0) && !tusbd_hid_mouse_is_busy(0) )
{ {
uint32_t button_mask = board_buttons(); static uint8_t prev_mouse_buttons = 0;
//------------- button pressed -------------//
if ( !tusbd_hid_mouse_is_busy(0) )
{
enum { enum {
BUTTON_UP = 0, // map to button mask BUTTON_UP = 0, // map to button mask
BUTTON_DOWN = 1, BUTTON_DOWN = 1,
BUTTON_LEFT = 2, BUTTON_LEFT = 2,
BUTTON_RIGHT = 3, BUTTON_RIGHT = 3,
BUTTON_CLICK_LEFT = 4, BUTTON_CLICK_LEFT = 4,
// BUTTON_CLICK_RIGHT = 5, BUTTON_CLICK_RIGHT = 5,
MOUSE_RESOLUTION = 5 BUTTON_CLICK_MIDDLE = 6
}; };
enum { MOUSE_RESOLUTION = 5 };
uint32_t button_mask = board_buttons();
memclr_(&mouse_report, sizeof(hid_mouse_report_t)); memclr_(&mouse_report, sizeof(hid_mouse_report_t));
if ( BIT_TEST_(button_mask, BUTTON_UP ) ) mouse_report.y = -MOUSE_RESOLUTION; if ( BIT_TEST_(button_mask, BUTTON_UP ) ) mouse_report.y = -MOUSE_RESOLUTION;
@ -131,7 +131,13 @@ OSAL_TASK_FUNCTION( mouse_device_app_task , p_task_para)
if ( BIT_TEST_(button_mask, BUTTON_LEFT ) ) mouse_report.x = -MOUSE_RESOLUTION; if ( BIT_TEST_(button_mask, BUTTON_LEFT ) ) mouse_report.x = -MOUSE_RESOLUTION;
if ( BIT_TEST_(button_mask, BUTTON_RIGHT ) ) mouse_report.x = MOUSE_RESOLUTION; if ( BIT_TEST_(button_mask, BUTTON_RIGHT ) ) mouse_report.x = MOUSE_RESOLUTION;
if ( BIT_TEST_(button_mask, BUTTON_CLICK_LEFT) ) mouse_report.buttons = MOUSE_BUTTON_LEFT; if ( BIT_TEST_(button_mask, BUTTON_CLICK_LEFT ) ) mouse_report.buttons |= MOUSE_BUTTON_LEFT;
if ( BIT_TEST_(button_mask, BUTTON_CLICK_RIGHT ) ) mouse_report.buttons |= MOUSE_BUTTON_RIGHT;
if ( BIT_TEST_(button_mask, BUTTON_CLICK_MIDDLE ) ) mouse_report.buttons |= MOUSE_BUTTON_MIDDLE;
if ( ! (prev_mouse_buttons == mouse_report.buttons && mouse_report.y == 0 && mouse_report.x == 0 && mouse_report.wheel == 0) )
{ // send report if clicked buttons are changed or there is any movement x, y, wheel
prev_mouse_buttons = mouse_report.buttons;
tusbd_hid_mouse_send(0, &mouse_report); tusbd_hid_mouse_send(0, &mouse_report);
} }