diff --git a/src/sysinput.c b/src/sysinput.c index bf02ca7..b5688ac 100644 --- a/src/sysinput.c +++ b/src/sysinput.c @@ -1,10 +1,14 @@ #include #include +#include #include #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); }