From 365a7852174b63931070ca39212b6267bc75e851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Thu, 13 Mar 2025 08:30:52 +0100 Subject: [PATCH] buttonLoop code clean --- src/button.cpp | 53 +++++++++++++++++++++++++++-------------------- src/button.h | 4 ++++ src/countdown.cpp | 6 +++--- src/countdown.h | 4 ---- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/button.cpp b/src/button.cpp index 32e2466..51143b8 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -1,38 +1,47 @@ #include "button.h" #include -#include "countdown.h" + +#define BUTTON_GPIO 25 +#define BUTTON_INVERT true +#define BUTTON_PULL_UP true +#define BUTTON_DEBOUNCE 100 +#define BUTTON_LONG_PRESS 1000 bool buttonLastState = false; -bool buttonLongPressed = false; +bool buttonLongPressedSent = false; unsigned long buttonLastMillis = 0; +bool buttonRead() { + return BUTTON_INVERT ^ (digitalRead(BUTTON_GPIO) == HIGH); +} + void buttonSetup() { - pinMode(25, INPUT_PULLUP); - buttonLastState = digitalRead(25) == LOW; + pinMode(BUTTON_GPIO, BUTTON_PULL_UP ? INPUT_PULLUP : INPUT); + buttonLastState = buttonRead(); buttonLastMillis = millis(); } void buttonLoop() { - const auto currentMillis = millis(); - const auto durationMillis = currentMillis - buttonLastMillis; - if (durationMillis >= 100) { - const auto currentState = digitalRead(25) == LOW; - if (buttonLastState != currentState) { - buttonLastState = currentState; - buttonLastMillis = currentMillis; - buttonLongPressed = false; - if (!buttonLastState) { - if (durationMillis < 1000) { - countdownShortPress(); - } - } + const auto now = millis(); + const auto duration = now - buttonLastMillis; + if (duration < BUTTON_DEBOUNCE) { + return; + } + + const auto state = buttonRead(); + if (buttonLastState != state) { + if (state) { + buttonLongPressedSent = false; + } else if (duration < BUTTON_LONG_PRESS && !buttonLongPressedSent) { + buttonShortPressed(); } + buttonLastState = state; + buttonLastMillis = now; + } else if (buttonLastState && duration >= BUTTON_LONG_PRESS && !buttonLongPressedSent) { + buttonLongPressedSent = true; + buttonLongPressed(); } - if (buttonLastState && currentMillis - buttonLastMillis >= 1000 && !buttonLongPressed) { - buttonLongPressed = true; - countdownLongPress(); - } -} \ No newline at end of file +} diff --git a/src/button.h b/src/button.h index 61b71bb..01c97b3 100644 --- a/src/button.h +++ b/src/button.h @@ -5,4 +5,8 @@ void buttonSetup(); void buttonLoop(); +void buttonShortPressed(); + +void buttonLongPressed(); + #endif diff --git a/src/countdown.cpp b/src/countdown.cpp index 9d99c03..7f9471e 100644 --- a/src/countdown.cpp +++ b/src/countdown.cpp @@ -136,7 +136,7 @@ void countdownLoop() { countdownUpdate(); } -void countdownLongPress() { +void buttonShortPressed() { switch (state) { case CONFIG: setState(READY); @@ -152,10 +152,10 @@ void countdownLongPress() { } } -void countdownShortPress() { +void buttonLongPressed() { switch (state) { case CONFIG: { - auto seconds = (configMillis / 30000) * 30 % (15 * 60) + 30; + const auto seconds = (configMillis / 30000) * 30 % (15 * 60) + 30; configMillis = seconds * 1000; rest = configMillis; break; diff --git a/src/countdown.h b/src/countdown.h index 6e3997b..de0d172 100644 --- a/src/countdown.h +++ b/src/countdown.h @@ -5,8 +5,4 @@ void countdownSetup(); void countdownLoop(); -void countdownLongPress(); - -void countdownShortPress(); - #endif