Skip to content

Commit

Permalink
Read initial cfg from SD card at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
amandel committed Jan 21, 2021
1 parent 96b918e commit 9fc44d7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 46 deletions.
2 changes: 1 addition & 1 deletion docs/software/firmware/obs_cfg.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
106 changes: 64 additions & 42 deletions src/OpenBikeSensorFirmware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -342,6 +302,7 @@ void setup() {
displayTest->clear();
}


void serverLoop() {
gps.handle();
server.handleClient();
Expand Down Expand Up @@ -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);
}
17 changes: 14 additions & 3 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <Arduino.h>
#include <ArduinoJson.h>
#include <vector>
#include <FS.h>

enum DisplayOptions {
DisplaySatellites = 0x01, // 1
Expand Down Expand Up @@ -85,6 +86,7 @@ class ObsConfig {
ObsConfig() : jsonData(4096) {};
~ObsConfig() = default;
bool loadConfig();
bool loadConfig(File &file);
bool saveConfig() const;
void printConfig() const;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9fc44d7

Please sign in to comment.