kbd_task: improve long-press behaviour

This commit is contained in:
Federico Amedeo Izzo 2020-12-17 23:15:56 +01:00
parent e59085a85e
commit 400a766f54
2 changed files with 42 additions and 26 deletions

View File

@ -22,6 +22,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <os_cfg_app.h>
/** /**
* The following enum provides a set of flags to be used to check which buttons * 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 * 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 * Structure that represents a keyboard event payload

View File

@ -141,19 +141,22 @@ static void kbd_task(void *arg)
// Variable for saving previous and current keyboard status // Variable for saving previous and current keyboard status
keyboard_t prev_keys = 0; keyboard_t prev_keys = 0;
keyboard_t keys = 0; keyboard_t keys = 0;
bool long_press = false;
bool send_event = false;
while(1) 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 // Lock display mutex and read keyboard status
OSMutexPend(&display_mutex, 0u, OS_OPT_PEND_BLOCKING, 0u, &os_err); OSMutexPend(&display_mutex, 0u, OS_OPT_PEND_BLOCKING, 0u, &os_err);
keys = kbd_getKeys(); keys = kbd_getKeys();
OSMutexPost(&display_mutex, OS_OPT_POST_NONE, &os_err); OSMutexPost(&display_mutex, OS_OPT_POST_NONE, &os_err);
// Compare with previous keyboard status // The key status has changed
if(keys != prev_keys) 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++) for(uint8_t k=0; k < kbd_num_keys; k++)
{ {
// Key has been pressed // Key has been pressed
@ -166,9 +169,21 @@ static void kbd_task(void *arg)
else if((prev_keys & (1 << k)) && !(keys & (1 << k))) else if((prev_keys & (1 << k)) && !(keys & (1 << k)))
{ {
send_event = true; send_event = true;
// Check timestamp }
if((now - key_ts[k]) >= kbd_long_interval) }
}
// 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; long_press = true;
send_event = true;
}
} }
} }
if(send_event) if(send_event)
@ -189,7 +204,7 @@ static void kbd_task(void *arg)
} }
// Save current keyboard state as previous // Save current keyboard state as previous
prev_keys = keys; prev_keys = keys;
}
// Read keyboard state at 20Hz // Read keyboard state at 20Hz
OSTimeDlyHMSM(0u, 0u, 0u, 50u, OS_OPT_TIME_HMSM_STRICT, &os_err); OSTimeDlyHMSM(0u, 0u, 0u, 50u, OS_OPT_TIME_HMSM_STRICT, &os_err);
} }