countdown config to LittleFS + FIX: leave config via short-/long-button
This commit is contained in:
parent
f412bc60da
commit
5222d90890
@ -2,6 +2,7 @@
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
board_build.filesystem = littlefs
|
||||
lib_deps = https://github.com/adafruit/Adafruit_NeoPixel
|
||||
upload_port = /dev/ttyUSB0
|
||||
upload_speed = 460800
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#include "countdown.h"
|
||||
|
||||
#include <LittleFS.h>
|
||||
#include "display.h"
|
||||
|
||||
enum State {
|
||||
@ -8,9 +8,11 @@ enum State {
|
||||
|
||||
State state = CONFIG;
|
||||
|
||||
long countdownConfig = 6 * 60 * 1000;
|
||||
bool countdownConfigDirty = false;
|
||||
|
||||
long countdownRest = countdownConfig;
|
||||
long countdownMillis = 6 * 60 * 1000;
|
||||
|
||||
long countdownRest = countdownMillis;
|
||||
|
||||
unsigned long last = 0;
|
||||
|
||||
@ -51,11 +53,11 @@ void setState(const State newState) {
|
||||
const char* name;
|
||||
switch (state) {
|
||||
case CONFIG:
|
||||
countdownRest = countdownConfig;
|
||||
countdownRest = countdownMillis;
|
||||
name = "CONFIG";
|
||||
break;
|
||||
case READY:
|
||||
countdownRest = countdownConfig;
|
||||
countdownRest = countdownMillis;
|
||||
name = "READY";
|
||||
break;
|
||||
case RUNNING:
|
||||
@ -103,7 +105,50 @@ void countdownUpdate() {
|
||||
}
|
||||
}
|
||||
|
||||
void countdownConfigLoad() {
|
||||
if (LittleFS.begin(true)) {
|
||||
Serial.println("Filesystem mounted.");
|
||||
File file = LittleFS.open("/countdownMillis");
|
||||
if (file) {
|
||||
const String content = file.readString();
|
||||
if (content) {
|
||||
countdownMillis = content.toInt();
|
||||
Serial.printf("Read countdownMillis from file: countdownMillis=%lu, file=%s", countdownMillis, file.path());
|
||||
} else {
|
||||
Serial.printf("WARN: Cannot parse content of: %s\n", file.path());
|
||||
}
|
||||
file.close();
|
||||
} else {
|
||||
Serial.printf("WARN: Cannot open file for READ: %s\n", file.path());
|
||||
}
|
||||
LittleFS.end();
|
||||
Serial.println("Filesystem unmounted.");
|
||||
} else {
|
||||
Serial.println("ERROR: Failed to mount filesystem.");
|
||||
}
|
||||
}
|
||||
|
||||
void countdownConfigWrite() {
|
||||
if (LittleFS.begin(true)) {
|
||||
Serial.println("Filesystem mounted.");
|
||||
File file = LittleFS.open("/countdownMillis", "w", true);
|
||||
if (file) {
|
||||
file.printf("%lu", countdownMillis);
|
||||
Serial.printf("Wrote countdownMillis to file: countdownMillis=%lu, file=%s", countdownMillis, file.path());
|
||||
file.close();
|
||||
countdownConfigDirty = false;
|
||||
} else {
|
||||
Serial.printf("WARN: Cannot open file for WRITE: %s\n", file.path());
|
||||
}
|
||||
LittleFS.end();
|
||||
Serial.println("Filesystem unmounted.");
|
||||
} else {
|
||||
Serial.println("ERROR: failed to mount filesystem");
|
||||
}
|
||||
}
|
||||
|
||||
void countdownSetup() {
|
||||
countdownConfigLoad();
|
||||
setState(READY);
|
||||
}
|
||||
|
||||
@ -114,9 +159,13 @@ void countdownLoop() {
|
||||
|
||||
void buttonShortPressed() {
|
||||
switch (state) {
|
||||
case CONFIG:
|
||||
setState(READY);
|
||||
case CONFIG: {
|
||||
const auto seconds = (countdownMillis / 30000) * 30 % (15 * 60) + 30;
|
||||
countdownMillis = seconds * 1000;
|
||||
countdownRest = countdownMillis;
|
||||
countdownConfigDirty = true;
|
||||
break;
|
||||
}
|
||||
case READY:
|
||||
case END:
|
||||
setState(CONFIG);
|
||||
@ -130,12 +179,12 @@ void buttonShortPressed() {
|
||||
|
||||
void buttonLongPressed() {
|
||||
switch (state) {
|
||||
case CONFIG: {
|
||||
const auto seconds = (countdownConfig / 30000) * 30 % (15 * 60) + 30;
|
||||
countdownConfig = seconds * 1000;
|
||||
countdownRest = countdownConfig;
|
||||
case CONFIG:
|
||||
if (countdownConfigDirty) {
|
||||
countdownConfigWrite();
|
||||
}
|
||||
setState(READY);
|
||||
break;
|
||||
}
|
||||
case RUNNING:
|
||||
setState(PAUSED);
|
||||
break;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user