From 18268280dc6c0bd44035b53655ae43e9c88245bd Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 28 Feb 2026 17:37:19 +0530 Subject: Add set time mode + test for button --- src/button.c | 53 ++++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) (limited to 'src/button.c') diff --git a/src/button.c b/src/button.c index dc52265..f7c2a07 100644 --- a/src/button.c +++ b/src/button.c @@ -2,42 +2,27 @@ #include #include "button.h" -#include "rtc.h" - -enum ButtonState check_button_state(); - -void button_tick() { - enum ButtonState button_state = check_button_state(); - switch (button_state) { - case BUTTON_PRESSED: - if (seconds <= 10) - seconds = 0; - else - seconds -= 10; - break; - case BUTTON_LONG_PRESSED: - seconds += 10; - break; - default: - break; - } -} +#include "mode.h" + +void button_tick() { apply_button_state(check_button_state()); } bool is_button_active = false; uint16_t button_tick_count = 0; +uint16_t button_inactive_for_ticks = 0; enum ButtonState check_button_state() { - if (is_button_active && button_tick_count >= LONG_PRESS_TICK_COUNT) { - button_tick_count = 0; - is_button_active = false; - return BUTTON_LONG_PRESSED; + // Disable button for some time + if (button_inactive_for_ticks > 0) { + button_inactive_for_ticks--; + return ButtonIdle; } - bool in_debounce_period = is_button_active && button_tick_count > 0 && - button_tick_count < DEBOUNCE_TICK_COUNT; + bool was_active = is_button_active; + bool in_debounce_period = + button_tick_count > 0 && button_tick_count < DEBOUNCE_TICK_COUNT; // Ignore readings from debounce period - if (!in_debounce_period) { + if (!is_button_active || !in_debounce_period) { DDRA &= ~(1 << PA0); PORTA &= ~(1 << PA0); is_button_active = (PINA & (1 << PA0)) == 0; @@ -45,12 +30,22 @@ enum ButtonState check_button_state() { if (is_button_active) { button_tick_count++; + if (button_tick_count >= LONG_PRESS_TICK_COUNT) { + button_tick_count = 0; + is_button_active = false; + button_inactive_for_ticks = BUTTON_INACTIVE_TICK_COUNT; + return ButtonLongPressed; + } + return ButtonActive; } else { if (button_tick_count >= DEBOUNCE_TICK_COUNT) { button_tick_count = 0; - return BUTTON_PRESSED; + button_inactive_for_ticks = BUTTON_INACTIVE_TICK_COUNT; + return ButtonPressed; } + if (!was_active) + button_tick_count = 0; } - return BUTTON_IDLE; + return ButtonIdle; } -- cgit v1.3.1