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
|
platform = espressif32
|
||||||
board = esp32dev
|
board = esp32dev
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
board_build.filesystem = littlefs
|
||||||
lib_deps = https://github.com/adafruit/Adafruit_NeoPixel
|
lib_deps = https://github.com/adafruit/Adafruit_NeoPixel
|
||||||
upload_port = /dev/ttyUSB0
|
upload_port = /dev/ttyUSB0
|
||||||
upload_speed = 460800
|
upload_speed = 460800
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
#include "countdown.h"
|
#include "countdown.h"
|
||||||
|
#include <LittleFS.h>
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
|
|
||||||
enum State {
|
enum State {
|
||||||
@ -8,9 +8,11 @@ enum State {
|
|||||||
|
|
||||||
State state = CONFIG;
|
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;
|
unsigned long last = 0;
|
||||||
|
|
||||||
@ -51,11 +53,11 @@ void setState(const State newState) {
|
|||||||
const char* name;
|
const char* name;
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case CONFIG:
|
case CONFIG:
|
||||||
countdownRest = countdownConfig;
|
countdownRest = countdownMillis;
|
||||||
name = "CONFIG";
|
name = "CONFIG";
|
||||||
break;
|
break;
|
||||||
case READY:
|
case READY:
|
||||||
countdownRest = countdownConfig;
|
countdownRest = countdownMillis;
|
||||||
name = "READY";
|
name = "READY";
|
||||||
break;
|
break;
|
||||||
case RUNNING:
|
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() {
|
void countdownSetup() {
|
||||||
|
countdownConfigLoad();
|
||||||
setState(READY);
|
setState(READY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,9 +159,13 @@ void countdownLoop() {
|
|||||||
|
|
||||||
void buttonShortPressed() {
|
void buttonShortPressed() {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case CONFIG:
|
case CONFIG: {
|
||||||
setState(READY);
|
const auto seconds = (countdownMillis / 30000) * 30 % (15 * 60) + 30;
|
||||||
|
countdownMillis = seconds * 1000;
|
||||||
|
countdownRest = countdownMillis;
|
||||||
|
countdownConfigDirty = true;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case READY:
|
case READY:
|
||||||
case END:
|
case END:
|
||||||
setState(CONFIG);
|
setState(CONFIG);
|
||||||
@ -130,12 +179,12 @@ void buttonShortPressed() {
|
|||||||
|
|
||||||
void buttonLongPressed() {
|
void buttonLongPressed() {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case CONFIG: {
|
case CONFIG:
|
||||||
const auto seconds = (countdownConfig / 30000) * 30 % (15 * 60) + 30;
|
if (countdownConfigDirty) {
|
||||||
countdownConfig = seconds * 1000;
|
countdownConfigWrite();
|
||||||
countdownRest = countdownConfig;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
setState(READY);
|
||||||
|
break;
|
||||||
case RUNNING:
|
case RUNNING:
|
||||||
setState(PAUSED);
|
setState(PAUSED);
|
||||||
break;
|
break;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user