From 91ae5ef60cf80a6644c47bf2810b43aacd19e345 Mon Sep 17 00:00:00 2001 From: elmo128 <60213508+elmo128@users.noreply.github.com> Date: Fri, 15 Sep 2023 00:27:19 +0200 Subject: [PATCH] better identifiers --- src/drivers/storage/SDCard.h | 10 +- src/drivers/storage/SPIStorage.h | 134 ------------------------- src/drivers/storage/nvMemory.h | 167 +++++++++++++++++++++++++++++++ src/wManager.cpp | 18 ++-- 4 files changed, 181 insertions(+), 148 deletions(-) delete mode 100644 src/drivers/storage/SPIStorage.h create mode 100644 src/drivers/storage/nvMemory.h diff --git a/src/drivers/storage/SDCard.h b/src/drivers/storage/SDCard.h index 2a1cfcc..8734178 100644 --- a/src/drivers/storage/SDCard.h +++ b/src/drivers/storage/SDCard.h @@ -3,14 +3,14 @@ #include "..\drivers.h" #include "storage.h" -#include "SPIStorage.h" +#include "nvMemory.h" class SDCard { public: SDCard(); ~SDCard(); - void SD2SPIStorage(SPIStorage* spifs); + void SD2nvMemory(nvMemory* nvMem); bool loadConfigFile(TSettings* Settings); private: bool initSDcard(); @@ -37,12 +37,12 @@ SDCard::~SDCard() SD_MMC.end(); } -void SDCard::SD2SPIStorage(SPIStorage* spifs) +void SDCard::SD2nvMemory(NVMem* nvMem) { TSettings Settings; if (loadConfigFile(&Settings)) { - spifs->saveConfigFile(&Settings); + nvMem->saveConfig(&Settings); WiFi.begin(Settings.WifiSSID, Settings.WifiPW); Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); ESP.restart(); @@ -143,7 +143,7 @@ bool SDCard::initSDcard() SDCard::SDCard() {} SDCard::~SDCard() {} -void SDCard::SD2SPIStorage(SPIStorage* spifs) {}; +void SDCard::SD2nvMemory(nvMemory* nvMem) {}; bool SDCard::loadConfigFile(TSettings* Settings) { return false; } bool SDCard::initSDcard() { return false; } diff --git a/src/drivers/storage/SPIStorage.h b/src/drivers/storage/SPIStorage.h deleted file mode 100644 index c17d15f..0000000 --- a/src/drivers/storage/SPIStorage.h +++ /dev/null @@ -1,134 +0,0 @@ -#ifndef _SPISTORAGE_H_ -#define _SPISTORAGE_H_ - -#define ESP_DRD_USE_SPIFFS true - -#include -#include -#include - -#include - -#include "..\drivers.h" -#include "storage.h" - -class SPIStorage -{ -private: - bool SPIFFSInitialized_; -public: - SPIStorage() - { - SPIFFSInitialized_ = false; - } - - ~SPIStorage() - { - if (SPIFFSInitialized_) - SPIFFS.end(); - }; - - void saveConfigFile(TSettings*Settings) - { - if (init()) - { - // Save Config in JSON format - Serial.println(F("SPIFS: Saving configuration...")); - - // Create a JSON document - StaticJsonDocument<512> json; - json[JSON_KEY_POOLURL] = Settings->PoolAddress; - json[JSON_KEY_POOLPORT] = Settings->PoolPort; - json[JSON_KEY_WALLETID] = Settings->BtcWallet; - json[JSON_KEY_TIMEZONE] = Settings->Timezone; - - // Open config file - File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w"); - if (!configFile) - { - // Error, file did not open - Serial.println("SPIFS: failed to open config file for writing"); - } - - // Serialize JSON data to write to file - serializeJsonPretty(json, Serial); - Serial.print('\n'); - if (serializeJson(json, configFile) == 0) - { - // Error writing file - Serial.println(F("SPIFS: Failed to write to file")); - } - // Close file - configFile.close(); - }; - } - - bool loadConfigFile(TSettings* Settings) - { - // Uncomment if we need to format filesystem - // SPIFFS.format(); - - // Load existing configuration file - // Read configuration from FS json - - if (init()) - { - if (SPIFFS.exists(JSON_CONFIG_FILE)) - { - // The file exists, reading and loading - Serial.println("SPIFS: Reading config file"); - File configFile = SPIFFS.open(JSON_CONFIG_FILE, "r"); - if (configFile) - { - Serial.println("SPIFS: Opened configuration file"); - StaticJsonDocument<512> json; - DeserializationError error = deserializeJson(json, configFile); - configFile.close(); - serializeJsonPretty(json, Serial); - Serial.print('\n'); - if (!error) - { - Serial.println("SPIFS: Parsing JSON"); - strcpy(Settings->PoolAddress, json[JSON_KEY_POOLURL] | Settings->PoolAddress); - strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet); - if(json.containsKey(JSON_KEY_POOLPORT)) - Settings->PoolPort = json[JSON_KEY_POOLPORT].as(); - if (json.containsKey(JSON_KEY_TIMEZONE)) - Settings->Timezone = json[JSON_KEY_TIMEZONE].as(); - return true; - } - else - { - // Error loading JSON data - Serial.println("SPIFS: Failed to load json config"); - } - } - } - } - return false; - } - - void deleteConfigFile() - { - Serial.println("SPIFS: Erasing config file.."); - SPIFFS.remove(JSON_CONFIG_FILE); //Borramos fichero - } -private: - bool init() - { - if (!SPIFFSInitialized_) - { - Serial.println("SPIFS: Mounting File System..."); - // May need to make it begin(true) first time you are using SPIFFS - SPIFFSInitialized_ = SPIFFS.begin(false) || SPIFFS.begin(true); - SPIFFSInitialized_ ? Serial.println("SPIFS: Mounted") : Serial.println("SPIFS: Mounting failed."); - } - else - { - Serial.println("SPIFS: Already Mounted"); - } - return SPIFFSInitialized_; - }; -}; - -#endif // _SPISTORAGE_H_ diff --git a/src/drivers/storage/nvMemory.h b/src/drivers/storage/nvMemory.h new file mode 100644 index 0000000..448e1fb --- /dev/null +++ b/src/drivers/storage/nvMemory.h @@ -0,0 +1,167 @@ +#ifndef _NVMEMORY_H_ +#define _NVMEMORY_H_ + +#define BUILD_SPIFFS + +#include "..\drivers.h" +#include "storage.h" + +class nvMemory +{ +public: + nvMemory(); + ~nvMemory(); + bool saveConfig(TSettings* Settings); + bool loadConfig(TSettings* Settings); + bool deleteConfig(); +private: + bool init(); + bool Initialized_; +}; + +#ifdef BUILD_SPIFFS + +#define ESP_DRD_USE_SPIFFS true + +#include +#include +#include +#include + +nvMemory::nvMemory() +{ + Initialized_ = false; +} + +nvMemory::~nvMemory() +{ + if (Initialized_) + SPIFFS.end(); +}; + +bool nvMemory::saveConfig(TSettings* Settings) +{ + if (init()) + { + // Save Config in JSON format + Serial.println(F("SPIFS: Saving configuration...")); + + // Create a JSON document + StaticJsonDocument<512> json; + json[JSON_KEY_POOLURL] = Settings->PoolAddress; + json[JSON_KEY_POOLPORT] = Settings->PoolPort; + json[JSON_KEY_WALLETID] = Settings->BtcWallet; + json[JSON_KEY_TIMEZONE] = Settings->Timezone; + + // Open config file + File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w"); + if (!configFile) + { + // Error, file did not open + Serial.println("SPIFS: failed to open config file for writing"); + return false; + } + + // Serialize JSON data to write to file + serializeJsonPretty(json, Serial); + Serial.print('\n'); + if (serializeJson(json, configFile) == 0) + { + // Error writing file + Serial.println(F("SPIFS: Failed to write to file")); + return false; + } + // Close file + configFile.close(); + return true; + }; + return false; +} + +bool nvMemory::loadConfig(TSettings* Settings) +{ + // Uncomment if we need to format filesystem + // SPIFFS.format(); + + // Load existing configuration file + // Read configuration from FS json + + if (init()) + { + if (SPIFFS.exists(JSON_CONFIG_FILE)) + { + // The file exists, reading and loading + Serial.println("SPIFS: Reading config file"); + File configFile = SPIFFS.open(JSON_CONFIG_FILE, "r"); + if (configFile) + { + Serial.println("SPIFS: Opened configuration file"); + StaticJsonDocument<512> json; + DeserializationError error = deserializeJson(json, configFile); + configFile.close(); + serializeJsonPretty(json, Serial); + Serial.print('\n'); + if (!error) + { + Serial.println("SPIFS: Parsing JSON"); + strcpy(Settings->PoolAddress, json[JSON_KEY_POOLURL] | Settings->PoolAddress); + strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet); + if (json.containsKey(JSON_KEY_POOLPORT)) + Settings->PoolPort = json[JSON_KEY_POOLPORT].as(); + if (json.containsKey(JSON_KEY_TIMEZONE)) + Settings->Timezone = json[JSON_KEY_TIMEZONE].as(); + return true; + } + else + { + // Error loading JSON data + Serial.println("SPIFS: Failed to load json config"); + } + } + } + } + return false; +} + +bool nvMemory::deleteConfig() +{ + Serial.println("SPIFS: Erasing config file.."); + return SPIFFS.remove(JSON_CONFIG_FILE); //Borramos fichero +} + +bool nvMemory::init() +{ + if (!Initialized_) + { + Serial.println("SPIFS: Mounting File System..."); + // May need to make it begin(true) first time you are using SPIFFS + Initialized_ = SPIFFS.begin(false) || SPIFFS.begin(true); + Initialized_ ? Serial.println("SPIFS: Mounted") : Serial.println("SPIFS: Mounting failed."); + } + else + { + Serial.println("SPIFS: Already Mounted"); + } + return Initialized_; +}; + + +#else +#error We need some kind of permanent storage here! + +nvMemory::nvMemory() {} + +nvMemory::~nvMemory() {} + +bool nvMemory::saveConfig(TSettings* Settings) { return false; } + +bool nvMemory::loadConfig(TSettings* Settings) { return false; } + +bool nvMemory::deleteConfig() { return false; } + +bool nvMemory::init() { return false; } + + +#endif //BUILD_SPIFFS + +#endif // _NVMEMORY_H_ diff --git a/src/wManager.cpp b/src/wManager.cpp index a1c526f..bb8ccde 100644 --- a/src/wManager.cpp +++ b/src/wManager.cpp @@ -9,7 +9,7 @@ #include "monitor.h" #include "drivers/display.h" #include "drivers/storage/SDCard.h" -#include "drivers/storage/SPIStorage.h" +#include "drivers/storage/nvMemory.h" #include "drivers/storage/storage.h" @@ -24,7 +24,7 @@ TSettings Settings; WiFiManager wm; extern monitor_data mMonitor; -SPIStorage SPIFS; +nvMemory nvMem; void saveConfigCallback() // Callback notifying us of the need to save configuration @@ -49,7 +49,7 @@ void configModeCallback(WiFiManager* myWiFiManager) void reset_configurations() { Serial.println("Erasing Config, restarting"); - SPIFS.deleteConfigFile(); + nvMem.deleteConfig(); wm.resetSettings(); ESP.restart(); } @@ -84,14 +84,14 @@ void init_WifiManager() // Explicitly set WiFi mode WiFi.mode(WIFI_STA); - if (!SPIFS.loadConfigFile(&Settings)) + if (!nvMem.loadConfig(&Settings)) { //No config file on internal flash. - SDCard sdc; - if (!sdc.loadConfigFile(&Settings)) + SDCard SDCrd; + if (!SDCrd.loadConfigFile(&Settings)) { //No config file on SD card. - sdc.SD2SPIStorage(&SPIFS); // reboot on success. + SDCrd.SD2nvMemory(&nvMem); // reboot on success. } else @@ -166,7 +166,7 @@ void init_WifiManager() strncpy(Settings.BtcWallet, addr_text_box.getValue(), sizeof(Settings.BtcWallet)); Settings.Timezone = atoi(time_text_box_num.getValue()); - SPIFS.saveConfigFile(&Settings); + nvMem.saveConfig(&Settings); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.restart(); @@ -225,7 +225,7 @@ void init_WifiManager() // Save the custom parameters to FS if (shouldSaveConfig) { - SPIFS.saveConfigFile(&Settings); + nvMem.saveConfig(&Settings); } }