diff --git a/lib/patrix/Patrix.cpp b/lib/patrix/Patrix.cpp index f913bdc..31adc27 100644 --- a/lib/patrix/Patrix.cpp +++ b/lib/patrix/Patrix.cpp @@ -8,18 +8,16 @@ #include "config.h" void setup() { - delay(500); - Serial.begin(115200); - Serial.print("\n\n\n"); - info("Startup..."); + consoleSetup(); configSetup(); - mqttSetup(); wifiSetup(); + mqttSetup(); patrixSetup(); } void loop() { consoleLoop(); + configLoop(); wifiLoop(); mqttLoop(); dataLoop(); diff --git a/lib/patrix/config.cpp b/lib/patrix/config.cpp index 8b12d64..393e017 100644 --- a/lib/patrix/config.cpp +++ b/lib/patrix/config.cpp @@ -1,108 +1,75 @@ #include "config.h" #include "log.h" -#ifdef ESP32 - -#include -#include - -Preferences config; - -void configSetup() { - config.begin("config", false); - const String &stored = config.getString("HOSTNAME", ""); - if (!stored.equals(HOSTNAME)) { - error(R"([WARNING] Stored config hostname mismatch: "%s" != "%s" [WARNING])", stored.c_str(), HOSTNAME); - } -} - -void configReset() { - info("[CONFIG RESET]"); - config.end(); - nvs_flash_erase(); - nvs_flash_init(); - config.begin("config", false); - config.putString("HOSTNAME", HOSTNAME); - configLoaded(); -} - -String configGetString(const char *name, const char *fallback, bool allowEmpty) { - if (!config.isKey(name)) { - return fallback; - } - String value = config.getString(name); - if (!allowEmpty && value.isEmpty()) { - return fallback; - } - return value; -} - -bool configPutString(const char *name, const char *value) { - return config.putString(name, value) == strlen(value); -} - -double configGetDouble(const char *name, double fallback) { - if (!config.isKey(name)) { - return fallback; - } - return config.getDouble(name); -} - -bool configPutDouble(const char *name, double value) { - return config.value(name, value) == 0; -} - -#else - #include #include -JsonDocument json; +#define DIRTY_WRITE_DELAY (10 * 1000) + +JsonDocument config; + +unsigned long lastChangeMillis = 0; bool configRead(); bool configWrite(); -size_t configWriteBytes(int *address, uint8_t *data, size_t size); +bool configWriteBytes(int *address, uint8_t *data, size_t size); -size_t configReadBytes(int *address, uint8_t *data, size_t size); +bool configReadBytes(int *address, uint8_t *data, size_t size); void configSetup() { configRead(); } +void configLoop() { + if (lastChangeMillis > 0 && millis() - lastChangeMillis > DIRTY_WRITE_DELAY) { + configWrite(); + } +} + void configReset() { - json.clear(); - json["HOSTNAME"] = HOSTNAME; + config.clear(); configWrite(); } String configGetString(const char *name, const char *fallback, bool allowEmpty) { - if (!json.containsKey(name)) { + if (!config.containsKey(name)) { return fallback; } - String value = json[name]; + String value = config[name]; if (!allowEmpty && value.isEmpty()) { return fallback; } return value; } -bool configPutString(const char *name, const char *value) { - json[name] = value; - return configWrite(); +void configPutString(const char *name, const char *value) { + if (config[name] != value) { + config[name] = value; + lastChangeMillis = millis(); + } } double configGetDouble(const char *name, double fallback) { - if (!json.containsKey(name)) { + if (!config.containsKey(name)) { return fallback; } - return json[name]; + return config[name]; } -bool configPutDouble(const char *name, double value) { - json[name] = value; - return configWrite(); +void configPutDouble(const char *name, double value) { + if (config[name] != value) { + config[name] = value; + lastChangeMillis = millis(); + } +} + +void configPrint() { + info("Config:"); + for (JsonPair pair: config.as()) { + info(" - %s: \"%s\"", pair.key().c_str(), pair.value().as()); + } } bool configRead() { @@ -119,9 +86,9 @@ bool configRead() { if (ok) { JsonDocument tmp; deserializeJson(tmp, buffer); - if (tmp.is() && !tmp.isNull() && tmp.containsKey("HOSTNAME")) { + if (tmp.is() && !tmp.isNull()) { info("Config loaded"); - json = tmp; + config = tmp; return true; } else { error("Failed to parse config JSON"); @@ -133,8 +100,10 @@ bool configRead() { } bool configWrite() { + lastChangeMillis = 0; + char buffer[512]; - size_t length = serializeJson(json, buffer, sizeof buffer); + size_t length = serializeJson(config, buffer, sizeof buffer); bool ok = true; int address = 0; @@ -142,31 +111,34 @@ bool configWrite() { ok &= configWriteBytes(&address, reinterpret_cast(&length), sizeof length); ok &= configWriteBytes(&address, reinterpret_cast(buffer), length); ok &= EEPROM.end(); + if (ok) { + info("Successfully wrote config to EEPROM"); + } else { + info("Failed to write config to EEPROM"); + } return ok; } -size_t configWriteBytes(int *address, uint8_t *data, size_t size) { +bool configWriteBytes(int *address, uint8_t *data, size_t size) { uint8_t *b = data; for (; b < data + size; b++) { EEPROM.write((*address)++, *b); - if (*address >= EEPROM.length()) { + if ((size_t) *address >= EEPROM.length()) { error("END OF EEPROM!!!"); break; } } - return b - data; + return b - data == size; } -size_t configReadBytes(int *address, uint8_t *data, size_t size) { +bool configReadBytes(int *address, uint8_t *data, size_t size) { uint8_t *b = data; for (; b < data + size; b++) { *b = EEPROM.read((*address)++); - if (*address >= EEPROM.length()) { + if ((size_t) *address >= EEPROM.length()) { error("END OF EEPROM!!!"); break; } } - return b - data; + return b - data == size; } - -#endif diff --git a/lib/patrix/config.h b/lib/patrix/config.h index c749472..3fee50a 100644 --- a/lib/patrix/config.h +++ b/lib/patrix/config.h @@ -5,16 +5,20 @@ void configSetup(); +void configLoop(); + void configReset(); void configLoaded(); String configGetString(const char *name, const char *fallback, bool allowEmpty); -bool configPutString(const char *name, const char *value); +void configPutString(const char *name, const char *value); double configGetDouble(const char *name, double fallback); -bool configPutDouble(const char *name, double value); +void configPutDouble(const char *name, double value); + +void configPrint(); #endif diff --git a/lib/patrix/console.cpp b/lib/patrix/console.cpp index 0109875..cb10909 100644 --- a/lib/patrix/console.cpp +++ b/lib/patrix/console.cpp @@ -26,6 +26,13 @@ void _setConfigString(const char *name, bool allowEmpty); const char *getFlashChipMode(); +void consoleSetup() { + delay(500); + Serial.begin(115200); + Serial.print("\n\n\n"); + info("Startup..."); +} + void consoleLoop() { uint8_t i = 0; while (Serial.available() > 0 && i++ < 100) { @@ -136,10 +143,7 @@ void _setConfigString(const char *name, bool allowEmpty) { error(R"(Value for "%s" cannot be empty!")", name); return; } - if (!configPutString(name, value)) { - error(R"(Failed to persist "%s" = "%s")", name, value); - return; - } + configPutString(name, value); info(R"(Set "%s" to "%s")", name, value); } @@ -161,6 +165,8 @@ void _info() { info(" bssid: %s", WiFi.BSSIDstr().c_str()); info(" rssi: %d", WiFi.RSSI()); + configPrint(); + info("Time:"); info(" uptime: %dd %2dh %2dm %2ds", days, hours % 24, minutes % 60, seconds % 60); info(" sys: %s", datetime); @@ -185,8 +191,8 @@ void _info() { info(" maxAlloc: %d", ESP.getMaxAllocHeap()); #endif - info("PS RAM:"); #ifdef ESP32 + info("PS RAM:"); info(" free: %d", ESP.getFreePsram()); info(" size: %d", ESP.getPsramSize()); info(" minFree: %d", ESP.getMinFreePsram()); diff --git a/lib/patrix/console.h b/lib/patrix/console.h index d5b4b12..56d7d02 100644 --- a/lib/patrix/console.h +++ b/lib/patrix/console.h @@ -1,6 +1,8 @@ #ifndef SENSOR3_CONSOLE_H #define SENSOR3_CONSOLE_H +void consoleSetup(); + void consoleLoop(); void consoleHandle(char *cmd); diff --git a/lib/patrix/wifi.cpp b/lib/patrix/wifi.cpp index 27b2f50..6d480e7 100644 --- a/lib/patrix/wifi.cpp +++ b/lib/patrix/wifi.cpp @@ -16,7 +16,7 @@ #define MIN_EPOCH_SECONDS 1712675973 -#define BOOT_DELAY_MS (10 * 1000) +#define BOOT_DELAY_MS (5 * 1000) #define WIFI_TIMEOUT_MS (15 * 1000)