Skip to content

Commit

Permalink
Regenerate displayconfig.json if erroneous
Browse files Browse the repository at this point in the history
  • Loading branch information
tyeth committed Apr 22, 2024
1 parent dac7ebb commit 989f666
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/provisioning/tinyusb/Wippersnapper_FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,16 @@ void Wippersnapper_FS::createDisplayConfig() {
delay(2500); // give FS some time to write the file
}

void Wippersnapper_FS::parseDisplayConfig(displayConfig &dispCfg) {
bool 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...");
Expand All @@ -458,19 +467,26 @@ 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");
}

// Attempt to deserialize the file's json document
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<displayConfig>();
return true;
}
#endif // ARDUINO_FUNHOUSE_ESP32S2

Expand Down
2 changes: 1 addition & 1 deletion src/provisioning/tinyusb/Wippersnapper_FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 989f666

Please sign in to comment.