integrated endSequence into mainloop (instead of delay)

This commit is contained in:
Patrick Haßel 2025-03-13 11:10:19 +01:00
parent 60e178a60d
commit 241dc3c2b5

View File

@ -2,11 +2,13 @@
#include <LittleFS.h> #include <LittleFS.h>
#include "display.h" #include "display.h"
enum State { #define COUNTDOWN_END_SEQUENCE_REPEAT 2
enum CountdownState {
CONFIG, READY, RUNNING, PAUSED, END CONFIG, READY, RUNNING, PAUSED, END
}; };
State state = CONFIG; CountdownState countdownState = CONFIG;
bool countdownConfigDirty = false; bool countdownConfigDirty = false;
@ -14,44 +16,22 @@ long countdownMillis = 6 * 60 * 1000;
long countdownRest = countdownMillis; long countdownRest = countdownMillis;
unsigned long last = 0; unsigned long countdownLast = 0;
void updateTime() { int countdownEndSequenceStep = 0;
if (state != RUNNING) {
return;
}
const auto now = max(1UL, millis());
if (last != 0) {
const auto diff = now - last;
countdownRest -= static_cast<long>(diff);
if (countdownRest < 0) {
countdownRest = 0;
}
}
last = now;
}
void drawSequence() { unsigned long countdownEndSequenceDelay = 0;
for (int x = 0; x < 3; ++x) {
for (int i = 0; i < 2; ++i) {
drawAll(WHITE);
delay(100);
drawBlack();
delay(100);
}
drawDashes();
delay(500);
}
}
void setState(const State newState) { unsigned long countdownEndSequenceLast = 0;
if (state == newState) {
void setState(const CountdownState newState) {
if (countdownState == newState) {
return; return;
} }
state = newState; countdownState = newState;
const char* name; const char* name;
switch (state) { switch (countdownState) {
case CONFIG: case CONFIG:
countdownRest = countdownMillis; countdownRest = countdownMillis;
name = "CONFIG"; name = "CONFIG";
@ -61,14 +41,15 @@ void setState(const State newState) {
name = "READY"; name = "READY";
break; break;
case RUNNING: case RUNNING:
last = millis(); countdownLast = millis();
name = "RUNNING"; name = "RUNNING";
break; break;
case PAUSED: case PAUSED:
name = "PAUSED"; name = "PAUSED";
break; break;
case END: case END:
drawSequence(); countdownEndSequenceDelay = 0;
countdownEndSequenceStep = 0;
name = "END"; name = "END";
break; break;
default: default:
@ -78,8 +59,34 @@ void setState(const State newState) {
Serial.printf("Mode: %s\n", name); Serial.printf("Mode: %s\n", name);
} }
void countdownUpdate() { void drawSequence() {
switch (state) { const auto now = max(1UL, millis());
if (countdownEndSequenceLast != 0 && now - countdownEndSequenceLast < countdownEndSequenceDelay) {
return;
}
switch (countdownEndSequenceStep % 3) {
case 0:
drawAll(WHITE);
countdownEndSequenceDelay = 100;
break;
case 1:
drawBlack();
countdownEndSequenceDelay = 100;
break;
default:
drawDashes();
countdownEndSequenceDelay = 500;
break;
}
countdownEndSequenceStep++;
countdownEndSequenceLast = now;
if (countdownEndSequenceStep >= 3 * COUNTDOWN_END_SEQUENCE_REPEAT) {
setState(READY);
}
}
void countdownDraw() {
switch (countdownState) {
case CONFIG: case CONFIG:
drawMillis(countdownRest, MAGENTA); drawMillis(countdownRest, MAGENTA);
break; break;
@ -87,20 +94,15 @@ void countdownUpdate() {
drawMillis(countdownRest, GREEN); drawMillis(countdownRest, GREEN);
break; break;
case RUNNING: { case RUNNING: {
if (countdownRest > 0) {
const auto color = countdownRest >= 60000 ? WHITE : (countdownRest >= 10000 ? YELLOW : RED); const auto color = countdownRest >= 60000 ? WHITE : (countdownRest >= 10000 ? YELLOW : RED);
drawMillis(countdownRest, color); drawMillis(countdownRest, color);
} else {
setState(END);
setState(READY);
}
break; break;
} }
case PAUSED: case PAUSED:
drawMillis(countdownRest, BLUE); drawMillis(countdownRest, BLUE);
break; break;
case END: case END:
drawDashes(); drawSequence();
break; break;
} }
} }
@ -152,13 +154,29 @@ void countdownSetup() {
setState(READY); setState(READY);
} }
void updateTime() {
if (countdownState != RUNNING) {
return;
}
const auto now = max(1UL, millis());
if (countdownLast != 0) {
const auto diff = now - countdownLast;
countdownRest -= static_cast<long>(diff);
if (countdownRest <= 0) {
countdownRest = 0;
setState(END);
}
}
countdownLast = now;
}
void countdownLoop() { void countdownLoop() {
updateTime(); updateTime();
countdownUpdate(); countdownDraw();
} }
void buttonShortPressed() { void buttonShortPressed() {
switch (state) { switch (countdownState) {
case CONFIG: { case CONFIG: {
const auto seconds = (countdownMillis / 30000) * 30 % (15 * 60) + 30; const auto seconds = (countdownMillis / 30000) * 30 % (15 * 60) + 30;
countdownMillis = seconds * 1000; countdownMillis = seconds * 1000;
@ -167,28 +185,11 @@ void buttonShortPressed() {
break; break;
} }
case READY: case READY:
case END: setState(RUNNING);
setState(CONFIG);
break;
case RUNNING:
case PAUSED:
setState(READY);
break;
}
}
void buttonLongPressed() {
switch (state) {
case CONFIG:
if (countdownConfigDirty) {
countdownConfigWrite();
}
setState(READY);
break; break;
case RUNNING: case RUNNING:
setState(PAUSED); setState(PAUSED);
break; break;
case READY:
case PAUSED: case PAUSED:
setState(RUNNING); setState(RUNNING);
break; break;
@ -197,3 +198,26 @@ void buttonLongPressed() {
break; break;
} }
} }
void buttonLongPressed() {
switch (countdownState) {
case CONFIG:
if (countdownConfigDirty) {
countdownConfigWrite();
}
setState(READY);
break;
case RUNNING:
setState(READY);
break;
case READY:
setState(CONFIG);
break;
case PAUSED: // NOLINT(*-branch-clone)
setState(READY);
break;
case END:
setState(READY);
break;
}
}