From d4017abf695fdf085cace250181de362441f1c28 Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Mon, 22 Apr 2024 16:20:14 +0100 Subject: [PATCH] Regenerate displayconfig.json if erroneous --- src/provisioning/tinyusb/Wippersnapper_FS.cpp | 18 +++++++++++++++++- src/provisioning/tinyusb/Wippersnapper_FS.h | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/provisioning/tinyusb/Wippersnapper_FS.cpp b/src/provisioning/tinyusb/Wippersnapper_FS.cpp index 1aefeea14..da1a9ee4d 100644 --- a/src/provisioning/tinyusb/Wippersnapper_FS.cpp +++ b/src/provisioning/tinyusb/Wippersnapper_FS.cpp @@ -446,7 +446,16 @@ void Wippersnapper_FS::createDisplayConfig() { delay(2500); // give FS some time to write the file } -void Wippersnapper_FS::parseDisplayConfig(displayConfig &dispCfg) { +void Wippersnapper_FS::parseDisplayConfig(displayConfig &dispCfg, bool forceRecreate) { + if (forceRecreate) { + if (wipperFatFs.exists("/display_config.json")) { + wipperFatFs.remove("/display_config.json"); + } +#ifdef ARDUINO_FUNHOUSE_ESP32S2 + createDisplayConfig(); +#endif + } + // Check if display_config.json file exists, if not, generate it if (!wipperFatFs.exists("/display_config.json")) { WS_DEBUG_PRINTLN("Could not find display_config.json, generating..."); @@ -458,6 +467,9 @@ void Wippersnapper_FS::parseDisplayConfig(displayConfig &dispCfg) { // Attempt to open file for JSON parsing File32 file = wipperFatFs.open("/display_config.json", FILE_READ); if (!file) { + if (!forceRecreate && parseDisplayConfig(dispCfg, true)) { + return true; + } fsHalt("FATAL ERROR: Unable to open display_config.json for parsing"); } @@ -465,12 +477,16 @@ void Wippersnapper_FS::parseDisplayConfig(displayConfig &dispCfg) { JsonDocument doc; DeserializationError error = deserializeJson(doc, file); if (error) { + if (!forceRecreate && parseDisplayConfig(dispCfg, true)) { + return true; + } fsHalt(String("FATAL ERROR: Unable to parse display_config.json - deserializeJson() failed with code") + error.c_str()); } // Close the file, we're done with it file.close(); // Extract a displayConfig struct from the JSON document dispCfg = doc.as(); + return true; } #endif // ARDUINO_FUNHOUSE_ESP32S2 diff --git a/src/provisioning/tinyusb/Wippersnapper_FS.h b/src/provisioning/tinyusb/Wippersnapper_FS.h index 22fe7df27..25e2475df 100644 --- a/src/provisioning/tinyusb/Wippersnapper_FS.h +++ b/src/provisioning/tinyusb/Wippersnapper_FS.h @@ -59,7 +59,7 @@ class Wippersnapper_FS { void parseSecrets(); #ifdef ARDUINO_FUNHOUSE_ESP32S2 - void parseDisplayConfig(displayConfig &displayFile); + bool parseDisplayConfig(displayConfig &displayFile, bool forceRecreate = false); void createDisplayConfig(); #endif private: