diff --git a/include/device.h b/include/device.h index d277c36e..d8d8d1ae 100644 --- a/include/device.h +++ b/include/device.h @@ -112,6 +112,7 @@ static inline void start_quick_blinking(uint8_t sec) { } void stop_blinking(void); uint8_t device_is_blinking(void); +bool device_allow_kbd_touch(void); void fm11_init(void); uint8_t fm_read_reg(uint16_t reg); void fm_read_regs(uint16_t reg, uint8_t *buf, uint8_t len); diff --git a/interfaces/USB/class/kbdhid/kbdhid.c b/interfaces/USB/class/kbdhid/kbdhid.c index 967a752f..1ae04ac2 100644 --- a/interfaces/USB/class/kbdhid/kbdhid.c +++ b/interfaces/USB/class/kbdhid/kbdhid.c @@ -139,7 +139,7 @@ uint8_t KBDHID_Init() { } uint8_t KBDHID_Loop(void) { - if (state == KBDHID_Idle) { + if (state == KBDHID_Idle && device_allow_kbd_touch()) { const uint8_t touch = get_touch_result(); if (touch != TOUCH_NO) { const int len = pass_handle_touch(touch, key_sequence); @@ -150,9 +150,9 @@ uint8_t KBDHID_Loop(void) { key_sequence[len] = 0; key_seq_position = 0; state = KBDHID_Typing; - DBG_MSG("Start typing %s", key_sequence); + DBG_MSG("Start typing %s\n", key_sequence); + set_touch_result(TOUCH_NO); } - set_touch_result(TOUCH_NO); } else { KBDHID_TypeKeySeq(); } diff --git a/src/device.c b/src/device.c index b3d1fcf9..1e5ccd6e 100644 --- a/src/device.c +++ b/src/device.c @@ -9,22 +9,31 @@ volatile static uint8_t touch_result; static uint8_t has_rf; -static uint32_t last_blink = UINT32_MAX, blink_timeout, blink_interval; +static uint32_t last_blink, blink_timeout, blink_interval; static enum { ON, OFF } led_status; typedef enum { WAIT_NONE = 1, WAIT_CCID, WAIT_CTAPHID, WAIT_DEEP, WAIT_DEEP_TOUCHED, WAIT_DEEP_CANCEL } wait_status_t; volatile static wait_status_t wait_status = WAIT_NONE; // WAIT_NONE is not 0, hence inited -uint8_t device_is_blinking(void) { return last_blink != UINT32_MAX; } +uint8_t device_is_blinking(void) { return blink_timeout != 0; } void device_loop(uint8_t has_touch) { CCID_Loop(); CTAPHID_Loop(0); WebUSB_Loop(); - if (has_touch && // hardware features the touch pad - !device_is_blinking() && // applets are not waiting for touch - device_get_tick() > 2000 // ignore touch for the first 2 seconds - ) - KBDHID_Loop(); + KBDHID_Loop(); +} + +bool device_allow_kbd_touch(void) { + uint32_t now = device_get_tick(); + if (!device_is_blinking() && // applets are not waiting for touch + now > 2000 && // ignore touch for the first 2 seconds + now - 1000 > last_blink && + get_touch_result() != TOUCH_NO + ) { + DBG_MSG("now=%lu last_blink=%lu\n", now, last_blink); + return true; + } + return false; } uint8_t get_touch_result(void) { @@ -140,8 +149,9 @@ static void toggle_led(void) { void device_update_led(void) { uint32_t now = device_get_tick(); + if (!device_is_blinking()) return; if (now > blink_timeout) stop_blinking(); - if (now >= last_blink && now - last_blink >= blink_interval) { + else if (now >= last_blink && now - last_blink >= blink_interval) { last_blink = now; toggle_led(); } @@ -160,7 +170,7 @@ void start_blinking_interval(uint8_t sec, uint32_t interval) { } void stop_blinking(void) { - last_blink = UINT32_MAX; + blink_timeout = 0; if (cfg_is_led_normally_on()) { led_on(); led_status = ON;