Skip to content

Commit

Permalink
settings: add the FFat compile switches
Browse files Browse the repository at this point in the history
  • Loading branch information
Angus Ainslie committed Mar 31, 2024
1 parent a7dd759 commit aa78882
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
1 change: 1 addition & 0 deletions esp32_marauder/configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
#define USE_SD
#define HAS_TEMP_SENSOR
//#define HAS_GPS
#define USE_FFAT
#endif

#ifdef MARAUDER_KIT
Expand Down
69 changes: 50 additions & 19 deletions esp32_marauder/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,59 @@ String Settings::getSettingsString() {
}

bool Settings::begin() {
#ifndef USE_FFAT
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
Serial.println("Settings SPIFFS Mount Failed");
return false;
}
#else
if(!FFat.begin(FORMAT_SPIFFS_IF_FAILED)){
return false;
}
Serial.println("Using FFat");

File root = FFat.open("/");
if(!root){
Serial.println("failed to open root directory - formatiing");
FFat.format();
}
else {
Serial.println("FFat root good");
root.close();
}
Serial.println("Found default fs FFat");
#endif

File settingsFile;

//SPIFFS.remove("/settings.json"); // NEED TO REMOVE THIS LINE

if (SPIFFS.exists("/settings.json")) {
settingsFile = SPIFFS.open("/settings.json", FILE_READ);

if (!settingsFile) {
#ifndef USE_FFAT
if (SPIFFS.exists("/settings.jsn")) {
settingsFile = SPIFFS.open("/settings.jsn", FILE_READ);
}
#else
if(FFat.exists("/settings.json")) {
settingsFile = FFat.open("/settings.jsn", FILE_READ);
}
#endif

if (!settingsFile) {
settingsFile.close();
Serial.println(F("Could not find settings file"));
#ifndef USE_FFAT
if (this->createDefaultSettings(SPIFFS))
return true;
#else
if (this->createDefaultSettings(FFat))
return true;
#endif
else
return false;
}
}
else {
Serial.println("Settings file does not exist");
if (this->createDefaultSettings(SPIFFS))
return true;
else
return false;
return false;
}

Serial.println("Created settings file");

String json_string;
DynamicJsonDocument jsonBuffer(1024);
DeserializationError error = deserializeJson(jsonBuffer, settingsFile);
Expand Down Expand Up @@ -131,17 +155,23 @@ bool Settings::saveSetting<bool>(String key, bool value) {
}

String settings_string;
File settingsFile;

for (int i = 0; i < json["Settings"].size(); i++) {
if (json["Settings"][i]["name"].as<String>() == key) {
json["Settings"][i]["value"] = value;

Serial.println("Saving setting...");

File settingsFile = SPIFFS.open("/settings.json", FILE_WRITE);

#ifndef USE_FFAT
settingsFile = SPIFFS.open("/settings.jsn", FILE_WRITE);
#else
settingsFile = FFat.open("/settings.jsn", FILE_WRITE);
#endif

if (!settingsFile) {
Serial.println(F("Failed to create settings file"));
Serial.print(F("Failed to create settings file - "));
Serial.println(settingsFile, DEC);
return false;
}

Expand Down Expand Up @@ -240,12 +270,13 @@ void Settings::printJsonSettings(String json_string) {
}

bool Settings::createDefaultSettings(fs::FS &fs) {
Serial.println(F("Creating default settings file: settings.json"));
Serial.println(F("Creating default settings file: settings.jsn"));

File settingsFile = fs.open("/settings.json", FILE_WRITE);
File settingsFile = fs.open("/settings.jsn", FILE_WRITE);

if (!settingsFile) {
Serial.println(F("Failed to create settings file"));
Serial.print(F("Failed to create settings file - "));
Serial.println(settingsFile, DEC);
return false;
}

Expand Down
4 changes: 4 additions & 0 deletions esp32_marauder/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

#include "configs.h"

#ifndef USE_FFAT
#include "SPIFFS.h"
#else
#include "FFat.h"
#endif
#include <FS.h>
#include <ArduinoJson.h>

Expand Down

0 comments on commit aa78882

Please sign in to comment.