buttonLoop code clean
This commit is contained in:
parent
5fc270fe8d
commit
365a785217
@ -1,38 +1,47 @@
|
||||
#include "button.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 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,4 +5,8 @@ void buttonSetup();
|
||||
|
||||
void buttonLoop();
|
||||
|
||||
void buttonShortPressed();
|
||||
|
||||
void buttonLongPressed();
|
||||
|
||||
#endif
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -5,8 +5,4 @@ void countdownSetup();
|
||||
|
||||
void countdownLoop();
|
||||
|
||||
void countdownLongPress();
|
||||
|
||||
void countdownShortPress();
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
Reference in New Issue
Block a user