From 5222d9089080e4e9226c2e506156e8adab9d776c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Thu, 13 Mar 2025 09:28:44 +0100 Subject: [PATCH] countdown config to LittleFS + FIX: leave config via short-/long-button --- platformio.ini | 1 + src/countdown.cpp | 73 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/platformio.ini b/platformio.ini index e81ba0d..c148024 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/countdown.cpp b/src/countdown.cpp index 3502abb..9e3e76c 100644 --- a/src/countdown.cpp +++ b/src/countdown.cpp @@ -1,5 +1,5 @@ #include "countdown.h" - +#include #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;