diff --git a/SDCard.cpp b/SDCard.cpp new file mode 100644 index 0000000..21faf58 --- /dev/null +++ b/SDCard.cpp @@ -0,0 +1,145 @@ +#include"src/drivers/storage/SDCard.h" + +#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) || defined (BUILD_SDSPI) + +SDCard::SDCard() +{ +#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) + iSD_ = &SD_MMC; +#elif defined (BUILD_SDSPI) +#error You chose to run the sd card in SPI mode. This is not implemented yet. +#endif +} + +SDCard::~SDCard() +{ + unmount(); +} + +void SDCard::SD2nvMemory(nvMemory* nvMem) +{ + TSettings Settings; + if (loadConfigFile(&Settings)) + { + nvMem->saveConfig(&Settings); + WiFi.begin(Settings.WifiSSID, Settings.WifiPW); + Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); + ESP.restart(); + } +} + +bool SDCard::loadConfigFile(TSettings* Settings) +{ + // Load existing configuration file + // Read configuration from FS json + + if (initSDcard()) + { + if (iSD_->exists(JSON_CONFIG_FILE)) + { + // The file exists, reading and loading + File configFile = iSD_->open(JSON_CONFIG_FILE, "r"); + if (configFile) + { + StaticJsonDocument<512> json; + DeserializationError error = deserializeJson(json, configFile); + configFile.close(); + Serial.println("SDCard: Loading config file"); + serializeJsonPretty(json, Serial); + Serial.print('\n'); + unmount(); + if (!error) + { + strcpy(Settings->WifiSSID, json[JSON_KEY_SSID] | Settings->WifiSSID); + strcpy(Settings->WifiPW, json[JSON_KEY_PASW] | Settings->WifiPW); + 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("SDCard: Error parsing config file!"); + } + } + else + { + Serial.println("SDCard: Error opening config file!"); + } + } + else + { + Serial.println("SDCard: No config file available!"); + } + unmount(); + } + return false; +} + +void SDCard::unmount() +{ + iSD_->end(); + Serial.println("SDCard: Unmounted"); +} + +bool SDCard::initSDcard() +{ + if (iSD_->cardType() != CARD_NONE) + { + Serial.println("SDCard: Already mounted."); + return true; + } + Serial.println("SDCard: Mounting card."); + + bool cardInitialized = false; +#if defined (BUILD_SDMMC_4) + if (iSD_->cardType() == CARD_NONE) + { + iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); + Serial.println("SDCard: 4-Bit Mode."); + cardInitialized = iSD_->begin("/sd", false); + } +#elif defined (BUILD_SDMMC_1) + #warning SDMMC : 1 - bit mode is not always working.If you experience issues, try other modes. + if (iSD_->cardType() == CARD_NONE) + { + iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); + Serial.println("SDCard: 1-Bit Mode."); + cardInitialized = iSD_->begin("/sd", true); + } +#elif defined (BUILD_SDSPI) +#error You chose to run the sd card in SPI mode. This is not implemented yet. +#else + Serial.println("SDCard: interface not available."); + return false; +#endif // dataPinsDefined + if (cardInitialized) + { + if (iSD_->cardType() != CARD_NONE) + { + Serial.println("SDCard: Mounted."); + return true; + } + else + { + Serial.println("SDCard: Mounting failed."); + iSD_->end(); + } + } + return false; +} + +#else + +SDCard::SDCard() {} +SDCard::~SDCard() {} +void SDCard::SD2nvMemory(nvMemory* nvMem) {}; +bool SDCard::loadConfigFile(TSettings* Settings) { return false; } +bool SDCard::initSDcard() { return false; } +void unmount() {} + +#endif //BUILD_SDMMC \ No newline at end of file diff --git a/nvMemory.cpp b/nvMemory.cpp new file mode 100644 index 0000000..d4f6901 --- /dev/null +++ b/nvMemory.cpp @@ -0,0 +1,145 @@ +#include "src/drivers/storage/nvMemory.h" + +#ifdef NVMEM_SPIFFS + +#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; + json[JSON_KEY_TIMEZONE] = Settings->saveStats; + + // 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 + File configFile = SPIFFS.open(JSON_CONFIG_FILE, "r"); + if (configFile) + { + Serial.println("SPIFS: Loading config file"); + StaticJsonDocument<512> json; + DeserializationError error = deserializeJson(json, configFile); + configFile.close(); + serializeJsonPretty(json, Serial); + Serial.print('\n'); + if (!error) + { + 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(); + if (json.containsKey(JSON_KEY_STATS2NV)) + Settings->saveStats = json[JSON_KEY_STATS2NV].as(); + return true; + } + else + { + // Error loading JSON data + Serial.println("SPIFS: Error parsing config file!"); + } + } + else + { + Serial.println("SPIFS: Error opening config file!"); + } + } + else + { + Serial.println("SPIFS: No config file available!"); + } + } + 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 + +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 //NVMEM_TYPE \ No newline at end of file diff --git a/src/drivers/storage/SDCard.h b/src/drivers/storage/SDCard.h index 1bcdf65..baba932 100644 --- a/src/drivers/storage/SDCard.h +++ b/src/drivers/storage/SDCard.h @@ -1,6 +1,8 @@ #ifndef _SDCARD_H_ #define _SDCARD_H_ +#include "..\devices\device.h" + #if defined (SDMMC_D0) && defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3) #define BUILD_SDMMC_4 #include @@ -14,7 +16,6 @@ #include #include -#include "..\devices\device.h" #include "storage.h" #include "nvMemory.h" @@ -38,150 +39,4 @@ private: #endif }; - -#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) || defined (BUILD_SDSPI) - -SDCard::SDCard() -{ -#if defined (BUILD_SDMMC_1) || defined(BUILD_SDMMC_4) - iSD_ = &SD_MMC; -#elif defined (BUILD_SDSPI) -#error You chose to run the sd card in SPI mode. This is not implemented yet. -#endif -} - -SDCard::~SDCard() -{ - unmount(); -} - -void SDCard::SD2nvMemory(nvMemory* nvMem) -{ - TSettings Settings; - if (loadConfigFile(&Settings)) - { - nvMem->saveConfig(&Settings); - WiFi.begin(Settings.WifiSSID, Settings.WifiPW); - Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); - ESP.restart(); - } -} - -bool SDCard::loadConfigFile(TSettings* Settings) -{ - // Load existing configuration file - // Read configuration from FS json - - if (initSDcard()) - { - if (iSD_->exists(JSON_CONFIG_FILE)) - { - // The file exists, reading and loading - File configFile = iSD_->open(JSON_CONFIG_FILE, "r"); - if (configFile) - { - StaticJsonDocument<512> json; - DeserializationError error = deserializeJson(json, configFile); - configFile.close(); - Serial.println("SDCard: Loading config file"); - serializeJsonPretty(json, Serial); - Serial.print('\n'); - unmount(); - if (!error) - { - strcpy(Settings->WifiSSID, json[JSON_KEY_SSID] | Settings->WifiSSID); - strcpy(Settings->WifiPW, json[JSON_KEY_PASW] | Settings->WifiPW); - 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("SDCard: Error parsing config file!"); - } - } - else - { - Serial.println("SDCard: Error opening config file!"); - } - } - else - { - Serial.println("SDCard: No config file available!"); - } - unmount(); - } - return false; -} - -void SDCard::unmount() -{ - iSD_->end(); - Serial.println("SDCard: Unmounted"); -} - -bool SDCard::initSDcard() -{ - if(iSD_->cardType() != CARD_NONE) - { - Serial.println("SDCard: Already mounted."); - return true; - } - Serial.println("SDCard: Mounting card."); - - bool cardInitialized = false; -#if defined (BUILD_SDMMC_4) - if (iSD_->cardType() == CARD_NONE) - { - iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); - Serial.println("SDCard: 4-Bit Mode."); - cardInitialized = iSD_->begin("/sd", false); - } -#elif defined (BUILD_SDMMC_1) -#warning SDMMC: 1-bit mode is not always working. If you experience issues, try other modes. - if (iSD_->cardType() == CARD_NONE) - { - iSD_->setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); - Serial.println("SDCard: 1-Bit Mode."); - cardInitialized = iSD_->begin("/sd", true); - } -#elif defined (BUILD_SDSPI) -#error You chose to run the sd card in SPI mode. This is not implemented yet. -#else - Serial.println("SDCard: interface not available."); - return false; -#endif // dataPinsDefined - if (cardInitialized) - { - if(iSD_->cardType() != CARD_NONE) - { - Serial.println("SDCard: Mounted."); - return true; - } - else - { - Serial.println("SDCard: Mounting failed."); - iSD_->end(); - } - } - return false; -} - -#else - -SDCard::SDCard() {} -SDCard::~SDCard() {} -void SDCard::SD2nvMemory(nvMemory* nvMem) {}; -bool SDCard::loadConfigFile(TSettings* Settings) { return false; } -bool SDCard::initSDcard() { return false; } -void unmount() {} - -#endif //BUILD_SDMMC - - #endif // _SDCARD_H_ diff --git a/src/drivers/storage/nvMemory.h b/src/drivers/storage/nvMemory.h index e2ea2c4..163431c 100644 --- a/src/drivers/storage/nvMemory.h +++ b/src/drivers/storage/nvMemory.h @@ -22,150 +22,9 @@ private: }; #ifdef NVMEM_SPIFFS - #define ESP_DRD_USE_SPIFFS true - -#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; - json[JSON_KEY_TIMEZONE] = Settings->saveStats; - - // 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 - File configFile = SPIFFS.open(JSON_CONFIG_FILE, "r"); - if (configFile) - { - Serial.println("SPIFS: Loading config file"); - StaticJsonDocument<512> json; - DeserializationError error = deserializeJson(json, configFile); - configFile.close(); - serializeJsonPretty(json, Serial); - Serial.print('\n'); - if (!error) - { - 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(); - if (json.containsKey(JSON_KEY_STATS2NV)) - Settings->saveStats = json[JSON_KEY_STATS2NV].as(); - return true; - } - else - { - // Error loading JSON data - Serial.println("SPIFS: Error parsing config file!"); - } - } - else - { - Serial.println("SPIFS: Error opening config file!"); - } - } - else - { - Serial.println("SPIFS: No config file available!"); - } - } - 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 implementation! - -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 //NVMEM_TYPE #endif // _NVMEMORY_H_