integrated endSequence into mainloop (instead of delay)
This commit is contained in:
parent
60e178a60d
commit
241dc3c2b5
@ -2,11 +2,13 @@
|
||||
#include <LittleFS.h>
|
||||
#include "display.h"
|
||||
|
||||
enum State {
|
||||
#define COUNTDOWN_END_SEQUENCE_REPEAT 2
|
||||
|
||||
enum CountdownState {
|
||||
CONFIG, READY, RUNNING, PAUSED, END
|
||||
};
|
||||
|
||||
State state = CONFIG;
|
||||
CountdownState countdownState = CONFIG;
|
||||
|
||||
bool countdownConfigDirty = false;
|
||||
|
||||
@ -14,44 +16,22 @@ long countdownMillis = 6 * 60 * 1000;
|
||||
|
||||
long countdownRest = countdownMillis;
|
||||
|
||||
unsigned long last = 0;
|
||||
unsigned long countdownLast = 0;
|
||||
|
||||
void updateTime() {
|
||||
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;
|
||||
}
|
||||
int countdownEndSequenceStep = 0;
|
||||
|
||||
void drawSequence() {
|
||||
for (int x = 0; x < 3; ++x) {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
drawAll(WHITE);
|
||||
delay(100);
|
||||
drawBlack();
|
||||
delay(100);
|
||||
}
|
||||
drawDashes();
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
unsigned long countdownEndSequenceDelay = 0;
|
||||
|
||||
void setState(const State newState) {
|
||||
if (state == newState) {
|
||||
unsigned long countdownEndSequenceLast = 0;
|
||||
|
||||
void setState(const CountdownState newState) {
|
||||
if (countdownState == newState) {
|
||||
return;
|
||||
}
|
||||
|
||||
state = newState;
|
||||
countdownState = newState;
|
||||
const char* name;
|
||||
switch (state) {
|
||||
switch (countdownState) {
|
||||
case CONFIG:
|
||||
countdownRest = countdownMillis;
|
||||
name = "CONFIG";
|
||||
@ -61,14 +41,15 @@ void setState(const State newState) {
|
||||
name = "READY";
|
||||
break;
|
||||
case RUNNING:
|
||||
last = millis();
|
||||
countdownLast = millis();
|
||||
name = "RUNNING";
|
||||
break;
|
||||
case PAUSED:
|
||||
name = "PAUSED";
|
||||
break;
|
||||
case END:
|
||||
drawSequence();
|
||||
countdownEndSequenceDelay = 0;
|
||||
countdownEndSequenceStep = 0;
|
||||
name = "END";
|
||||
break;
|
||||
default:
|
||||
@ -78,8 +59,34 @@ void setState(const State newState) {
|
||||
Serial.printf("Mode: %s\n", name);
|
||||
}
|
||||
|
||||
void countdownUpdate() {
|
||||
switch (state) {
|
||||
void drawSequence() {
|
||||
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:
|
||||
drawMillis(countdownRest, MAGENTA);
|
||||
break;
|
||||
@ -87,20 +94,15 @@ void countdownUpdate() {
|
||||
drawMillis(countdownRest, GREEN);
|
||||
break;
|
||||
case RUNNING: {
|
||||
if (countdownRest > 0) {
|
||||
const auto color = countdownRest >= 60000 ? WHITE : (countdownRest >= 10000 ? YELLOW : RED);
|
||||
drawMillis(countdownRest, color);
|
||||
} else {
|
||||
setState(END);
|
||||
setState(READY);
|
||||
}
|
||||
const auto color = countdownRest >= 60000 ? WHITE : (countdownRest >= 10000 ? YELLOW : RED);
|
||||
drawMillis(countdownRest, color);
|
||||
break;
|
||||
}
|
||||
case PAUSED:
|
||||
drawMillis(countdownRest, BLUE);
|
||||
break;
|
||||
case END:
|
||||
drawDashes();
|
||||
drawSequence();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -152,13 +154,29 @@ void countdownSetup() {
|
||||
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() {
|
||||
updateTime();
|
||||
countdownUpdate();
|
||||
countdownDraw();
|
||||
}
|
||||
|
||||
void buttonShortPressed() {
|
||||
switch (state) {
|
||||
switch (countdownState) {
|
||||
case CONFIG: {
|
||||
const auto seconds = (countdownMillis / 30000) * 30 % (15 * 60) + 30;
|
||||
countdownMillis = seconds * 1000;
|
||||
@ -167,28 +185,11 @@ void buttonShortPressed() {
|
||||
break;
|
||||
}
|
||||
case READY:
|
||||
case END:
|
||||
setState(CONFIG);
|
||||
break;
|
||||
case RUNNING:
|
||||
case PAUSED:
|
||||
setState(READY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void buttonLongPressed() {
|
||||
switch (state) {
|
||||
case CONFIG:
|
||||
if (countdownConfigDirty) {
|
||||
countdownConfigWrite();
|
||||
}
|
||||
setState(READY);
|
||||
setState(RUNNING);
|
||||
break;
|
||||
case RUNNING:
|
||||
setState(PAUSED);
|
||||
break;
|
||||
case READY:
|
||||
case PAUSED:
|
||||
setState(RUNNING);
|
||||
break;
|
||||
@ -197,3 +198,26 @@ void buttonLongPressed() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user