From 400a766f542ef33d0dcd42d75760d06f4ba98f37 Mon Sep 17 00:00:00 2001 From: Federico Amedeo Izzo Date: Thu, 17 Dec 2020 23:15:56 +0100 Subject: [PATCH] kbd_task: improve long-press behaviour --- openrtx/include/interfaces/keyboard.h | 3 +- openrtx/src/threads.c | 65 ++++++++++++++++----------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/openrtx/include/interfaces/keyboard.h b/openrtx/include/interfaces/keyboard.h index 9c5925cc..8fba101f 100644 --- a/openrtx/include/interfaces/keyboard.h +++ b/openrtx/include/interfaces/keyboard.h @@ -22,6 +22,7 @@ #include #include +#include /** * The following enum provides a set of flags to be used to check which buttons @@ -68,7 +69,7 @@ static const uint8_t kbd_num_keys = 29; /** * Time interval in ticks after which a keypress is considered a long-press */ -static const uint8_t kbd_long_interval = 29; +static const uint16_t kbd_long_interval = OS_CFG_TICK_RATE_HZ * 1; /** * Structure that represents a keyboard event payload diff --git a/openrtx/src/threads.c b/openrtx/src/threads.c index b8f252b8..84782607 100644 --- a/openrtx/src/threads.c +++ b/openrtx/src/threads.c @@ -141,19 +141,22 @@ static void kbd_task(void *arg) // Variable for saving previous and current keyboard status keyboard_t prev_keys = 0; keyboard_t keys = 0; + bool long_press = false; + bool send_event = false; while(1) { + // Reset flags and get current time + long_press = false; + send_event = false; + now = OSTimeGet(&os_err); // Lock display mutex and read keyboard status OSMutexPend(&display_mutex, 0u, OS_OPT_PEND_BLOCKING, 0u, &os_err); keys = kbd_getKeys(); OSMutexPost(&display_mutex, OS_OPT_POST_NONE, &os_err); - // Compare with previous keyboard status + // The key status has changed if(keys != prev_keys) { - bool long_press = false; - bool send_event = false; - now = OSTimeGet(&os_err); for(uint8_t k=0; k < kbd_num_keys; k++) { // Key has been pressed @@ -166,30 +169,42 @@ static void kbd_task(void *arg) else if((prev_keys & (1 << k)) && !(keys & (1 << k))) { send_event = true; - // Check timestamp - if((now - key_ts[k]) >= kbd_long_interval) - long_press = true; } } - if(send_event) - { - kbd_msg_t msg; - msg.long_press = long_press; - // OR the saved key status with the current key status - // Do this because the new key status is got when the - // key is lifted, and does not contain the pressed key anymore - msg.keys = keys | prev_keys; - // Send event_t as void * message to use with OSQPost - event_t event; - event.type = EVENT_KBD; - event.payload = msg.value; - // Send keyboard status in queue - OSQPost(&ui_queue, (void *)event.value, sizeof(event_t), - OS_OPT_POST_FIFO + OS_OPT_POST_NO_SCHED, &os_err); - } - // Save current keyboard state as previous - prev_keys = keys; } + // Some key is kept pressed + else if(keys != 0) + { + // Check for long-press timers + for(uint8_t k=0; k < kbd_num_keys; k++) + { + // The key is pressed and its long-press timer is over + if(keys & (1 << k) && (now - key_ts[k]) >= kbd_long_interval) + { + long_press = true; + send_event = true; + } + } + } + if(send_event) + { + kbd_msg_t msg; + msg.long_press = long_press; + // OR the saved key status with the current key status + // Do this because the new key status is got when the + // key is lifted, and does not contain the pressed key anymore + msg.keys = keys | prev_keys; + // Send event_t as void * message to use with OSQPost + event_t event; + event.type = EVENT_KBD; + event.payload = msg.value; + // Send keyboard status in queue + OSQPost(&ui_queue, (void *)event.value, sizeof(event_t), + OS_OPT_POST_FIFO + OS_OPT_POST_NO_SCHED, &os_err); + } + // Save current keyboard state as previous + prev_keys = keys; + // Read keyboard state at 20Hz OSTimeDlyHMSM(0u, 0u, 0u, 50u, OS_OPT_TIME_HMSM_STRICT, &os_err); }