From 9fc44d7529fc9cdf2c891b3acfff480f361d13f6 Mon Sep 17 00:00:00 2001 From: amandel Date: Thu, 21 Jan 2021 19:29:01 +0100 Subject: [PATCH] Read initial cfg from SD card at startup fixes #217 --- docs/software/firmware/obs_cfg.md | 2 +- src/OpenBikeSensorFirmware.cpp | 106 ++++++++++++++++++------------ src/config.cpp | 17 ++++- src/config.h | 3 + 4 files changed, 82 insertions(+), 46 deletions(-) diff --git a/docs/software/firmware/obs_cfg.md b/docs/software/firmware/obs_cfg.md index 7b987a83..8efa1dd7 100644 --- a/docs/software/firmware/obs_cfg.md +++ b/docs/software/firmware/obs_cfg.md @@ -52,7 +52,7 @@ relevant put the order of the array content. ], // Token used to authenticate and authorize at the portal. "portalToken": "thisIsAlsoSecret", - // URL of the portal to be used (currently it is hardcoded, the value is not used) + // URL of the portal to be used "portalUrl": "https://openbikesensor.hlrs.de", // Array with privacy areas to be considered "privacyArea": [ diff --git a/src/OpenBikeSensorFirmware.cpp b/src/OpenBikeSensorFirmware.cpp index f8642230..2fe029d0 100644 --- a/src/OpenBikeSensorFirmware.cpp +++ b/src/OpenBikeSensorFirmware.cpp @@ -101,6 +101,7 @@ void bluetoothConfirmed(const DataSet *dataSet, uint16_t measureIndex); uint8_t batteryPercentage(); void serverLoop(); void handleButtonInServerMode(); +void loadConfig(ObsConfig &cfg); // The BMP280 can keep up to 3.4MHz I2C speed, so no need for an individual slower speed void switch_wire_speed_to_VL53(){ @@ -155,49 +156,8 @@ void setup() { //############################################################## // Load, print and save config //############################################################## - - displayTest->showTextOnGrid(2, displayTest->newLine(), "Config... "); - - if (!SPIFFS.begin(true)) { - Serial.println("An Error has occurred while mounting SPIFFS"); - displayTest->showTextOnGrid(2, displayTest->currentLine(), "Config... error "); - return; - } - - Serial.println(F("Load config")); ObsConfig cfg; // this one is valid in setup! - if (!cfg.loadConfig()) { - displayTest->showTextOnGrid(2, displayTest->currentLine(), "Config...RESET"); - delay(1000); // resetting config once, wait a moment - } - - // Dump config file - log_d("Print config file..."); - cfg.printConfig(); - - // TODO: Select Profile missing here or below! - // Copy data to static view on config! - cfg.fill(config); - - if(config.devConfig & ShowGrid) { - displayTest->showGrid(true); - } - if (config.displayConfig & DisplayInvert) { - displayTest->invert(); - } else { - displayTest->normalDisplay(); - } - - if (config.displayConfig & DisplayFlip) { - displayTest->flipScreen(); - } - - delay(333); // Added for user experience - char buffer[32]; - snprintf(buffer, sizeof(buffer), "<%02d| - |%02d>", - config.sensorOffsets[LEFT_SENSOR_ID], - config.sensorOffsets[RIGHT_SENSOR_ID]); - displayTest->showTextOnGrid(2, displayTest->currentLine(), buffer); + loadConfig(cfg); gps.begin(); @@ -342,6 +302,7 @@ void setup() { displayTest->clear(); } + void serverLoop() { gps.handle(); server.handleClient(); @@ -628,3 +589,64 @@ uint8_t batteryPercentage() { } return result; } + +void loadConfig(ObsConfig &cfg) { + displayTest->showTextOnGrid(2, displayTest->newLine(), "Config... "); + + if (!SPIFFS.begin(true)) { + log_e("An Error has occurred while mounting SPIFFS"); + displayTest->showTextOnGrid(2, displayTest->currentLine(), "Config... error "); + displayTest->showTextOnGrid(2, displayTest->newLine(), "Please reboot!"); + while (true) { + delay(1000); + } + } + + if (SD.begin() && SD.exists("/obs.json")) { + displayTest->showTextOnGrid(2, displayTest->currentLine(), "Init from SD."); + log_i("Configuration init from SD."); + delay(1000); + File f = SD.open("/obs.json"); + if (cfg.loadConfig(f)) { + cfg.saveConfig(); + } + f.close(); + SD.remove("/obs.json"); + displayTest->showTextOnGrid(2, displayTest->newLine(), "done."); + delay(5000); + } else { + log_d("No configuration init from SD. SD: %d File: %d", SD.begin(), (SD.begin() && SD.exists("/obs.json"))); + } + + log_i("Load cfg"); + if (!cfg.loadConfig()) { + displayTest->showTextOnGrid(2, displayTest->currentLine(), "Config...RESET"); + delay(5000); // resetting cfg once, wait a moment + } + + cfg.printConfig(); + + // TODO: Select Profile missing here or below! + // Copy data to static view on cfg! + cfg.fill(config); + + // Setup display, this is not the right place ;) + if(config.devConfig & ShowGrid) { + displayTest->showGrid(true); + } + if (config.displayConfig & DisplayInvert) { + displayTest->invert(); + } else { + displayTest->normalDisplay(); + } + if (config.displayConfig & DisplayFlip) { + displayTest->flipScreen(); + } + + delay(333); // Added for user experience + char buffer[32]; + snprintf(buffer, sizeof(buffer), "<%02d| - |%02d>", + config.sensorOffsets[LEFT_SENSOR_ID], + config.sensorOffsets[RIGHT_SENSOR_ID]); + displayTest->showTextOnGrid(2, displayTest->currentLine(), buffer); +} diff --git a/src/config.cpp b/src/config.cpp index ad758700..a7a1fa9c 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -274,21 +274,32 @@ bool ObsConfig::loadConfig(const String &filename) { return loadJson(jsonData, filename); } +bool ObsConfig::loadConfig(File &file) { + log_d("Loading config json from %s.", file.name()); + return loadJson(jsonData, file); +} + bool ObsConfig::loadJson(JsonDocument &jsonDocument, const String &filename) { - bool success = false; File file = SPIFFS.open(filename); + bool success = loadJson(jsonDocument, file); + file.close(); + return success; +} + +bool ObsConfig::loadJson(JsonDocument &jsonDocument, File &file) { + bool success = false; jsonDocument.clear(); DeserializationError error = deserializeJson(jsonDocument, file); if (error) { log_w("Failed to read file %s, using default configuration got %s.\n", - filename.c_str(), error.c_str()); + file.name(), error.c_str()); } else { success = true; } file.close(); #ifdef DEVELOP - log_d("Config found in file '%s':", filename.c_str()); + log_d("Config found in file '%s':", file.name()); serializeJsonPretty(jsonDocument, Serial); log_d("------------------------------------------"); #endif diff --git a/src/config.h b/src/config.h index 4c61a929..225f7c04 100644 --- a/src/config.h +++ b/src/config.h @@ -24,6 +24,7 @@ #include #include #include +#include enum DisplayOptions { DisplaySatellites = 0x01, // 1 @@ -85,6 +86,7 @@ class ObsConfig { ObsConfig() : jsonData(4096) {}; ~ObsConfig() = default; bool loadConfig(); + bool loadConfig(File &file); bool saveConfig() const; void printConfig() const; @@ -140,6 +142,7 @@ class ObsConfig { private: static bool loadJson(JsonDocument &jsonDocument, const String &filename); + static bool loadJson(JsonDocument &jsonDocument, fs::File &file); static bool parseJsonFromString(JsonDocument &jsonDocument, const String &jsonAsString); bool loadOldConfig(const String &filename); bool loadConfig(const String &filename);