diff options
| author | hathach <[email protected]> | 2014-04-24 15:59:35 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2014-04-24 15:59:35 +0700 |
| commit | 9d404b9f00d7b0dd0d893eb4b4f1feaf1b75082d (patch) | |
| tree | 941eed26eeb6eadb4fc3046f65a88729b032d70a | |
| parent | 8f81aa6c72c67c83c4482b9f99c71aafae133ca4 (diff) | |
refractor keyboard_device_app.c
refractor mouse_device_app.c app to only send report when necessary (movement or button changed)
| -rw-r--r-- | demos/device/src/keyboard_device_app.c | 7 | ||||
| -rw-r--r-- | demos/device/src/mouse_device_app.c | 50 |
2 files changed, 31 insertions, 26 deletions
diff --git a/demos/device/src/keyboard_device_app.c b/demos/device/src/keyboard_device_app.c index aa26677b8..f4c3691be 100644 --- a/demos/device/src/keyboard_device_app.c +++ b/demos/device/src/keyboard_device_app.c @@ -115,16 +115,15 @@ OSAL_TASK_FUNCTION( keyboard_device_app_task , p_task_para) {
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;
-
uint32_t new_button_mask = board_buttons();
//------------- 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;
diff --git a/demos/device/src/mouse_device_app.c b/demos/device/src/mouse_device_app.c index 360cb8787..49469ab8e 100644 --- a/demos/device/src/mouse_device_app.c +++ b/demos/device/src/mouse_device_app.c @@ -104,36 +104,42 @@ OSAL_TASK_FUNCTION( mouse_device_app_task , p_task_para) {
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;
+
+ enum {
+ BUTTON_UP = 0, // map to button mask
+ BUTTON_DOWN = 1,
+ BUTTON_LEFT = 2,
+ BUTTON_RIGHT = 3,
+ BUTTON_CLICK_LEFT = 4,
+ BUTTON_CLICK_RIGHT = 5,
+ BUTTON_CLICK_MIDDLE = 6
+ };
- //------------- button pressed -------------//
- if ( !tusbd_hid_mouse_is_busy(0) )
- {
- enum {
- BUTTON_UP = 0, // map to button mask
- BUTTON_DOWN = 1,
- BUTTON_LEFT = 2,
- BUTTON_RIGHT = 3,
- BUTTON_CLICK_LEFT = 4,
-// BUTTON_CLICK_RIGHT = 5,
- MOUSE_RESOLUTION = 5
- };
+ 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_DOWN ) ) mouse_report.y = MOUSE_RESOLUTION;
- if ( BIT_TEST_(button_mask, BUTTON_UP) ) mouse_report.y = -MOUSE_RESOLUTION;
- if ( BIT_TEST_(button_mask, BUTTON_DOWN) ) mouse_report.y = 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_LEFT) ) 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_RIGHT ) ) mouse_report.buttons |= MOUSE_BUTTON_RIGHT;
+ if ( BIT_TEST_(button_mask, BUTTON_CLICK_MIDDLE ) ) mouse_report.buttons |= MOUSE_BUTTON_MIDDLE;
- if ( BIT_TEST_(button_mask, BUTTON_CLICK_LEFT) ) mouse_report.buttons = MOUSE_BUTTON_LEFT;
+ 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);
}
}
|
