diff --git a/examples/device/cdc_msc_hid/src/main.c b/examples/device/cdc_msc_hid/src/main.c index be0b1493..06a99711 100644 --- a/examples/device/cdc_msc_hid/src/main.c +++ b/examples/device/cdc_msc_hid/src/main.c @@ -121,10 +121,11 @@ void tud_cdc_rx_cb(uint8_t itf) void usb_hid_task(void) { // Poll every 10ms - static tu_timeout_t tm = { .start = 0, .interval = 10 }; + const uint32_t interval_ms = 10; + static uint32_t start_ms = 0; - if ( !tu_timeout_expired(&tm) ) return; // not enough time - tu_timeout_reset(&tm); + if ( board_noos_millis() < start_ms + interval_ms) return; // not enough time + start_ms += interval_ms; uint32_t const btn = board_buttons(); @@ -193,12 +194,14 @@ void tud_umount_cb(void) //--------------------------------------------------------------------+ void led_blinking_task(void) { + const uint32_t interval_ms = 1000; static uint32_t start_ms = 0; + static bool led_state = false; // Blink every 1000 ms - if ( board_noos_millis() < start_ms + 1000) return; // not enough time - start_ms += 1000; + if ( board_noos_millis() < start_ms + interval_ms) return; // not enough time + start_ms += interval_ms; board_led_control(led_state); led_state = 1 - led_state; // toggle diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 73af0c9a..fd760225 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -43,7 +43,6 @@ // Max report len is keyboard's one with 8 byte + 1 byte report id #define REPORT_BUFSIZE 12 - #define ITF_IDX_BOOT_KBD 0 #define ITF_IDX_BOOT_MSE ( ITF_IDX_BOOT_KBD + (CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT) ) #define ITF_IDX_GENERIC ( ITF_IDX_BOOT_MSE + (CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT) ) @@ -70,8 +69,10 @@ typedef struct typedef struct { - uint8_t usage; // HID_USAGE_* - uint8_t idle_rate; // in unit of 4 ms + uint8_t usage; // HID_USAGE_* + 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 report_id; uint8_t report_len; @@ -91,7 +92,6 @@ static hidd_report_t _mse_rpt; #endif /*------------- Helpers -------------*/ - static inline hidd_interface_t* get_interface_by_itfnum(uint8_t itf_num) { for (uint8_t i=0; i < ITF_COUNT; i++ ) @@ -151,19 +151,14 @@ static bool hidd_kbd_report(hid_keyboard_report_t const *p_report) hidd_interface_t * p_hid = _kbd_rpt.itf; - // 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. - static tu_timeout_t idle_tm = { 0, 0 }; - - if ( (_kbd_rpt.idle_rate == 0) || !tu_timeout_expired(&idle_tm) ) - { - if ( 0 == memcmp(p_hid->report_buf, p_report, sizeof(hid_keyboard_report_t)) ) return true; - } - - tu_timeout_set(&idle_tm, _kbd_rpt.idle_rate * 4); + // 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)); } @@ -195,33 +190,6 @@ bool tud_hid_keyboard_key_press(char ch) return tud_hid_keyboard_keycode(modifier, keycode); } -#if 0 // should be at application -bool tud_hid_keyboard_key_sequence(const char* str, uint32_t interval_ms) -{ - // Send each key in string - char ch; - while( (ch = *str++) != 0 ) - { - char lookahead = *str; - - tud_hid_keyboard_key_press(ch); - - // Blocking delay - tu_timeout_wait(interval_ms); - - /* Only need to empty report if the next character is NULL or the same with - * the current one, else no need to send */ - if ( lookahead == ch || lookahead == 0 ) - { - tud_hid_keyboard_key_release(); - tu_timeout_wait(interval_ms); - } - } - - return true; -} -#endif - #endif // CFG_TUD_HID_ASCII_TO_KEYCODE_LOOKUP #endif // CFG_TUD_HID_KEYBOARD @@ -246,6 +214,7 @@ static bool hidd_mouse_report(hid_mouse_report_t const *p_report) TU_VERIFY( tud_hid_mouse_ready() ); hidd_interface_t * p_hid = _mse_rpt.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));