added debounce logic for hardware buttons

This commit is contained in:
Amy G. Bowersox 2019-12-06 21:25:15 -07:00
parent 15d85ce2eb
commit 8b7bd91817

View File

@ -1,10 +1,14 @@
#include <stddef.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include "scode.h"
#include "log.h"
#include "msg_queue.h"
#include "gpio.h"
#include "time_func.h"
#define DEBOUNCE_BUTTON_MSEC 350
PMSG_QUEUE Sys_Queue = NULL;
@ -16,6 +20,10 @@ static void *input_thread(void *arg)
{
UINT32 st, down, up, mask;
UINT_PTR attr;
TIMESTAMP now;
TIMESTAMP button_event_ok[GPIO_BUTTON_COUNT];
memset(button_event_ok, 0, GPIO_BUTTON_COUNT * sizeof(TIMESTAMP));
while (running)
{
@ -23,12 +31,18 @@ static void *input_thread(void *arg)
st = Gpio_read_buttons();
if (st != last_bstate)
{
now = Time_since_start();
up = last_bstate & ~st;
down = st & ~last_bstate;
for (attr = 1, mask = GRB_STATE_BUTTON1; attr <= GPIO_BUTTON_COUNT; attr++, mask <<= 1)
{
if (now < button_event_ok[attr - 1])
continue;
if (up & mask)
{
button_event_ok[attr - 1] = now + DEBOUNCE_BUTTON_MSEC;
Mq_post1(Sys_Queue, 0, WM_HWBUTTONUP, attr);
}
else if (down & mask)
Mq_post1(Sys_Queue, 0, WM_HWBUTTONDOWN, attr);
}