split declaration+implementation

This commit is contained in:
elmo128 2023-09-17 14:52:48 +02:00
parent 711cf00c43
commit 2e366e931b
4 changed files with 292 additions and 288 deletions

145
SDCard.cpp Normal file
View File

@ -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<int>();
if (json.containsKey(JSON_KEY_TIMEZONE))
Settings->Timezone = json[JSON_KEY_TIMEZONE].as<int>();
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

145
nvMemory.cpp Normal file
View File

@ -0,0 +1,145 @@
#include "src/drivers/storage/nvMemory.h"
#ifdef NVMEM_SPIFFS
#include <SPIFFS.h>
#include <FS.h>
#include <ArduinoJson.h>
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<int>();
if (json.containsKey(JSON_KEY_TIMEZONE))
Settings->Timezone = json[JSON_KEY_TIMEZONE].as<int>();
if (json.containsKey(JSON_KEY_STATS2NV))
Settings->saveStats = json[JSON_KEY_STATS2NV].as<bool>();
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

View File

@ -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 <SD_MMC.h>
@ -14,7 +16,6 @@
#include <FS.h>
#include <ArduinoJson.h>
#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<int>();
if (json.containsKey(JSON_KEY_TIMEZONE))
Settings->Timezone = json[JSON_KEY_TIMEZONE].as<int>();
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_

View File

@ -22,150 +22,9 @@ private:
};
#ifdef NVMEM_SPIFFS
#define ESP_DRD_USE_SPIFFS true
#include <SPIFFS.h>
#include <FS.h>
#include <ArduinoJson.h>
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<int>();
if (json.containsKey(JSON_KEY_TIMEZONE))
Settings->Timezone = json[JSON_KEY_TIMEZONE].as<int>();
if (json.containsKey(JSON_KEY_STATS2NV))
Settings->saveStats = json[JSON_KEY_STATS2NV].as<bool>();
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_