From 0c0f3996574f114a30a00f50353d202fc07eea30 Mon Sep 17 00:00:00 2001 From: DiSlord Date: Sun, 22 Mar 2020 19:18:40 +0300 Subject: [PATCH] Improve button input, better debounce less code size --- ui.c | 117 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 53 insertions(+), 64 deletions(-) diff --git a/ui.c b/ui.c index 634928a..785cdd6 100644 --- a/ui.c +++ b/ui.c @@ -43,8 +43,8 @@ uistat_t uistat = { #define BUTTON_DOWN_LONG_TICKS 5000 /* 1sec */ #define BUTTON_DOUBLE_TICKS 2500 /* 500ms */ -#define BUTTON_REPEAT_TICKS 625 /* 125ms */ -#define BUTTON_DEBOUNCE_TICKS 200 +#define BUTTON_REPEAT_TICKS 500 /* 100ms */ +#define BUTTON_DEBOUNCE_TICKS 400 /* 80ms */ /* lever switch assignment */ #define BIT_UP1 3 @@ -55,9 +55,8 @@ uistat_t uistat = { #define BUTTON_MASK 0b1111 static uint16_t last_button = 0b0000; -static uint32_t last_button_down_ticks; -static uint32_t last_button_repeat_ticks; -static int8_t inhibit_until_release = FALSE; +static systime_t last_button_down_ticks; +static systime_t last_button_repeat_ticks; volatile uint8_t operation_requested = OP_NONE; @@ -126,73 +125,62 @@ static void menu_push_submenu(const menuitem_t *submenu); static int btn_check(void) { - int cur_button = READ_PORT() & BUTTON_MASK; - int changed = last_button ^ cur_button; - int status = 0; - uint32_t ticks = chVTGetSystemTime(); - if (changed & (1<= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS) { - // button released - status |= EVT_BUTTON_SINGLE_CLICK; - if (inhibit_until_release) { - status = 0; - inhibit_until_release = FALSE; - } - } - } - if (changed & (1<= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS)) { - status |= EVT_UP; - } - } - if (changed & (1<= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS)) { - status |= EVT_DOWN; - } - } - last_button_down_ticks = ticks; - last_button = cur_button; + systime_t ticks; + // Debounce input + while(TRUE){ + ticks = chVTGetSystemTimeX(); + if(ticks - last_button_down_ticks > BUTTON_DEBOUNCE_TICKS) + break; + chThdSleepMilliseconds(10); + } + int status = 0; + uint16_t cur_button = READ_PORT() & BUTTON_MASK; + // Detect only changed and pressed buttons + uint16_t button_set = (last_button ^ cur_button) & cur_button; + last_button_down_ticks = ticks; + last_button = cur_button; - return status; + if (button_set & (1<= last_button_down_ticks + BUTTON_DOWN_LONG_TICKS) { - inhibit_until_release = TRUE; - return EVT_BUTTON_DOWN_LONG; - } - if ((changed & (1<= BUTTON_DOWN_LONG_TICKS && (cur_button & (1<= last_button_down_ticks + BUTTON_DOWN_LONG_TICKS - && ticks >= last_button_repeat_ticks + BUTTON_REPEAT_TICKS) { + if (dt > BUTTON_DOWN_LONG_TICKS && + ticks > last_button_repeat_ticks) { + int status = 0; if (cur_button & (1<= 0 && markers[active_marker].enabled) { if ((status & EVT_DOWN) && markers[active_marker].index > 0) { markers[active_marker].index--; - markers[active_marker].frequency = frequencies[markers[active_marker].index]; - redraw_marker(active_marker); } if ((status & EVT_UP) && markers[active_marker].index < sweep_points-1) { markers[active_marker].index++; - markers[active_marker].frequency = frequencies[markers[active_marker].index]; - redraw_marker(active_marker); } + markers[active_marker].frequency = frequencies[markers[active_marker].index]; + redraw_marker(active_marker); } status = btn_wait_release(); } while (status != 0); - if (active_marker >= 0) - redraw_marker(active_marker); } static void @@ -1693,9 +1681,10 @@ lever_search_marker(int status) i = marker_search_left(markers[active_marker].index); else if (status & EVT_UP) i = marker_search_right(markers[active_marker].index); - if (i != -1) + if (i != -1){ markers[active_marker].index = i; - redraw_marker(active_marker); + redraw_marker(active_marker); + } } }