kbd_task: improve long-press behaviour
This commit is contained in:
parent
e59085a85e
commit
400a766f54
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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,30 +169,42 @@ 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)
|
|
||||||
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
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue