buttonLoop code clean

This commit is contained in:
Patrick Haßel 2025-03-13 08:30:52 +01:00
parent 5fc270fe8d
commit 365a785217
4 changed files with 38 additions and 29 deletions

View File

@ -1,38 +1,47 @@
#include "button.h" #include "button.h"
#include <Arduino.h> #include <Arduino.h>
#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 buttonLastState = false;
bool buttonLongPressed = false; bool buttonLongPressedSent = false;
unsigned long buttonLastMillis = 0; unsigned long buttonLastMillis = 0;
bool buttonRead() {
return BUTTON_INVERT ^ (digitalRead(BUTTON_GPIO) == HIGH);
}
void buttonSetup() { void buttonSetup() {
pinMode(25, INPUT_PULLUP); pinMode(BUTTON_GPIO, BUTTON_PULL_UP ? INPUT_PULLUP : INPUT);
buttonLastState = digitalRead(25) == LOW; buttonLastState = buttonRead();
buttonLastMillis = millis(); buttonLastMillis = millis();
} }
void buttonLoop() { void buttonLoop() {
const auto currentMillis = millis(); const auto now = millis();
const auto durationMillis = currentMillis - buttonLastMillis; const auto duration = now - buttonLastMillis;
if (durationMillis >= 100) { if (duration < BUTTON_DEBOUNCE) {
const auto currentState = digitalRead(25) == LOW; return;
if (buttonLastState != currentState) {
buttonLastState = currentState;
buttonLastMillis = currentMillis;
buttonLongPressed = false;
if (!buttonLastState) {
if (durationMillis < 1000) {
countdownShortPress();
} }
const auto state = buttonRead();
if (buttonLastState != state) {
if (state) {
buttonLongPressedSent = false;
} else if (duration < BUTTON_LONG_PRESS && !buttonLongPressedSent) {
buttonShortPressed();
} }
} buttonLastState = state;
} buttonLastMillis = now;
if (buttonLastState && currentMillis - buttonLastMillis >= 1000 && !buttonLongPressed) { } else if (buttonLastState && duration >= BUTTON_LONG_PRESS && !buttonLongPressedSent) {
buttonLongPressed = true; buttonLongPressedSent = true;
countdownLongPress(); buttonLongPressed();
} }
} }

View File

@ -5,4 +5,8 @@ void buttonSetup();
void buttonLoop(); void buttonLoop();
void buttonShortPressed();
void buttonLongPressed();
#endif #endif

View File

@ -136,7 +136,7 @@ void countdownLoop() {
countdownUpdate(); countdownUpdate();
} }
void countdownLongPress() { void buttonShortPressed() {
switch (state) { switch (state) {
case CONFIG: case CONFIG:
setState(READY); setState(READY);
@ -152,10 +152,10 @@ void countdownLongPress() {
} }
} }
void countdownShortPress() { void buttonLongPressed() {
switch (state) { switch (state) {
case CONFIG: { case CONFIG: {
auto seconds = (configMillis / 30000) * 30 % (15 * 60) + 30; const auto seconds = (configMillis / 30000) * 30 % (15 * 60) + 30;
configMillis = seconds * 1000; configMillis = seconds * 1000;
rest = configMillis; rest = configMillis;
break; break;

View File

@ -5,8 +5,4 @@ void countdownSetup();
void countdownLoop(); void countdownLoop();
void countdownLongPress();
void countdownShortPress();
#endif #endif