aboutsummaryrefslogtreecommitdiff
path: root/src/button.c
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2026-02-28 17:37:19 +0530
committerAkshay Nair <phenax5@gmail.com>2026-02-28 17:37:19 +0530
commit18268280dc6c0bd44035b53655ae43e9c88245bd (patch)
treec17b1703d8a3deb4cd67a6e6466d024f02a49bb7 /src/button.c
parent02c12bfedcf669df81491ea49502a982e980bcda (diff)
downloaddaft-watch-18268280dc6c0bd44035b53655ae43e9c88245bd.tar.gz
daft-watch-18268280dc6c0bd44035b53655ae43e9c88245bd.zip
Add set time mode + test for button
Diffstat (limited to 'src/button.c')
-rw-r--r--src/button.c51
1 files changed, 23 insertions, 28 deletions
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 <stdint.h>
#include "button.h"
-#include "rtc.h"
+#include "mode.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;
- }
-}
+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;
}