mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-27 05:06:44 +00:00
Add eeprom support
This commit is contained in:
parent
bf4c8e975a
commit
c1996a5eaf
@ -1,6 +1,7 @@
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "ArduinoJson.h"
|
|
||||||
#include "ArduinoLog.h"
|
#include "ArduinoLog.h"
|
||||||
|
#include "ArduinoJson.h"
|
||||||
|
#include "StreamUtils.h"
|
||||||
|
|
||||||
#include "hasp_config.h"
|
#include "hasp_config.h"
|
||||||
#include "hasp_debug.h"
|
#include "hasp_debug.h"
|
||||||
@ -13,13 +14,13 @@
|
|||||||
|
|
||||||
#include "hasp_conf.h"
|
#include "hasp_conf.h"
|
||||||
|
|
||||||
#if HASP_USE_SPIFFS>0
|
#if HASP_USE_SPIFFS > 0
|
||||||
#include <FS.h> // Include the SPIFFS library
|
#include <FS.h> // Include the SPIFFS library
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
#include "SPIFFS.h"
|
#include "SPIFFS.h"
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#if HASP_USE_EEPROM>0
|
#if HASP_USE_EEPROM > 0
|
||||||
#include "EEPROM.h"
|
#include "EEPROM.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -72,13 +73,17 @@ void configStartDebug(bool setupdebug, String & configFile)
|
|||||||
{
|
{
|
||||||
if(setupdebug) {
|
if(setupdebug) {
|
||||||
debugStart(); // Debug started, now we can use it; HASP header sent
|
debugStart(); // Debug started, now we can use it; HASP header sent
|
||||||
|
#if HASP_USE_SPIFFS > 0
|
||||||
Log.notice(F("FILE: [SUCCESS] SPI flash FS mounted"));
|
Log.notice(F("FILE: [SUCCESS] SPI flash FS mounted"));
|
||||||
#if HASP_USE_SPIFFS>0
|
|
||||||
spiffsInfo();
|
spiffsInfo();
|
||||||
spiffsList();
|
spiffsList();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
#if HASP_USE_SPIFFS > 0
|
||||||
Log.notice(F("CONF: Loading %s"), configFile.c_str());
|
Log.notice(F("CONF: Loading %s"), configFile.c_str());
|
||||||
|
#else
|
||||||
|
Log.notice(F("CONF: reading EEPROM"));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void configGetConfig(JsonDocument & settings, bool setupdebug = false)
|
void configGetConfig(JsonDocument & settings, bool setupdebug = false)
|
||||||
@ -86,9 +91,10 @@ void configGetConfig(JsonDocument & settings, bool setupdebug = false)
|
|||||||
String configFile((char *)0);
|
String configFile((char *)0);
|
||||||
configFile.reserve(128);
|
configFile.reserve(128);
|
||||||
configFile = String(FPSTR(HASP_CONFIG_FILE));
|
configFile = String(FPSTR(HASP_CONFIG_FILE));
|
||||||
|
DeserializationError error;
|
||||||
|
|
||||||
#if HASP_USE_SPIFFS>0
|
#if HASP_USE_SPIFFS > 0
|
||||||
File file = SPIFFS.open(configFile, "r");
|
File file = SPIFFS.open(configFile, "r");
|
||||||
|
|
||||||
if(file) {
|
if(file) {
|
||||||
size_t size = file.size();
|
size_t size = file.size();
|
||||||
@ -97,7 +103,7 @@ void configGetConfig(JsonDocument & settings, bool setupdebug = false)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DeserializationError error = deserializeJson(settings, file);
|
error = deserializeJson(settings, file);
|
||||||
if(!error) {
|
if(!error) {
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
@ -121,17 +127,26 @@ void configGetConfig(JsonDocument & settings, bool setupdebug = false)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
|
||||||
|
#if HASP_USE_EEPROM > 0
|
||||||
|
EepromStream eepromStream(0, 1024);
|
||||||
|
error = deserializeJson(settings, eepromStream);
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// File does not exist or error reading file
|
// File does not exist or error reading file
|
||||||
if(setupdebug) {
|
if(setupdebug) {
|
||||||
debugPreSetup(settings[F("debug")]);
|
debugPreSetup(settings[F("debug")]);
|
||||||
}
|
}
|
||||||
|
|
||||||
configStartDebug(setupdebug, configFile);
|
configStartDebug(setupdebug, configFile);
|
||||||
Log.error(F("CONF: Failed to load %s"), configFile.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#if HASP_USE_SPIFFS > 0
|
||||||
|
Log.error(F("CONF: Failed to load %s"), configFile.c_str());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
/*
|
||||||
void configBackupToEeprom()
|
void configBackupToEeprom()
|
||||||
{
|
{
|
||||||
#if HASP_USE_SPIFFS>0
|
#if HASP_USE_SPIFFS>0
|
||||||
@ -160,10 +175,9 @@ void configBackupToEeprom()
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
void configWriteConfig()
|
void configWriteConfig()
|
||||||
{
|
{
|
||||||
#if HASP_USE_SPIFFS>0
|
|
||||||
String configFile((char *)0);
|
String configFile((char *)0);
|
||||||
configFile.reserve(128);
|
configFile.reserve(128);
|
||||||
configFile = String(FPSTR(HASP_CONFIG_FILE));
|
configFile = String(FPSTR(HASP_CONFIG_FILE));
|
||||||
@ -253,6 +267,7 @@ void configWriteConfig()
|
|||||||
// changed |= otaGetConfig(settings[F("ota")].as<JsonObject>());
|
// changed |= otaGetConfig(settings[F("ota")].as<JsonObject>());
|
||||||
|
|
||||||
if(writefile) {
|
if(writefile) {
|
||||||
|
#if HASP_USE_SPIFFS > 0
|
||||||
File file = SPIFFS.open(configFile, "w");
|
File file = SPIFFS.open(configFile, "w");
|
||||||
if(file) {
|
if(file) {
|
||||||
Log.notice(F("CONF: Writing %s"), configFile.c_str());
|
Log.notice(F("CONF: Writing %s"), configFile.c_str());
|
||||||
@ -260,36 +275,70 @@ void configWriteConfig()
|
|||||||
file.close();
|
file.close();
|
||||||
if(size > 0) {
|
if(size > 0) {
|
||||||
Log.verbose(F("CONF: [SUCCESS] Saved %s"), configFile.c_str());
|
Log.verbose(F("CONF: [SUCCESS] Saved %s"), configFile.c_str());
|
||||||
configBackupToEeprom();
|
// configBackupToEeprom();
|
||||||
return;
|
} else {
|
||||||
|
Log.error(F("CONF: Failed to write %s"), configFile.c_str());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Log.error(F("CONF: Failed to write %s"), configFile.c_str());
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
Log.error(F("CONF: Failed to write %s"), configFile.c_str());
|
// Method 1
|
||||||
|
// Log.verbose(F("CONF: Writing to EEPROM"));
|
||||||
|
// EepromStream eepromStream(0, 1024);
|
||||||
|
// WriteBufferingStream bufferedWifiClient{eepromStream, 512};
|
||||||
|
// serializeJson(doc, bufferedWifiClient);
|
||||||
|
// bufferedWifiClient.flush(); // <- OPTIONAL
|
||||||
|
// eepromStream.flush(); // (for ESP)
|
||||||
|
|
||||||
|
#if defined(STM32F4xx)
|
||||||
|
// Method 2
|
||||||
|
Log.verbose(F("CONF: Writing to EEPROM"));
|
||||||
|
char buffer[1024 + 128];
|
||||||
|
size_t size = serializeJson(doc, buffer, sizeof(buffer));
|
||||||
|
if(size > 0) {
|
||||||
|
uint16_t i;
|
||||||
|
for(i = 0; i < size; i++) eeprom_buffered_write_byte(i, buffer[i]);
|
||||||
|
eeprom_buffered_write_byte(i, 0);
|
||||||
|
eeprom_buffer_flush();
|
||||||
|
Log.verbose(F("CONF: [SUCCESS] Saved EEPROM"));
|
||||||
|
} else {
|
||||||
|
Log.error(F("CONF: Failed to save config to EEPROM"));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Log.notice(F("CONF: Configuration was not changed"));
|
Log.notice(F("CONF: Configuration did not change"));
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void configSetup()
|
void configSetup()
|
||||||
{
|
{
|
||||||
#if HASP_USE_SPIFFS>0
|
DynamicJsonDocument settings(1024 + 128);
|
||||||
if(!SPIFFS.begin()) {
|
|
||||||
|
for(uint8_t i = 0; i < 2; i++) {
|
||||||
|
Serial.print(__FILE__);
|
||||||
|
Serial.println(__LINE__);
|
||||||
|
|
||||||
|
if(i == 0) {
|
||||||
|
#if HASP_USE_SPIFFS > 0
|
||||||
|
EepromStream eepromStream(0, 2048);
|
||||||
|
DeserializationError error = deserializeJson(settings, eepromStream);
|
||||||
|
#else
|
||||||
|
continue;
|
||||||
#endif
|
#endif
|
||||||
|
} else {
|
||||||
#if HASP_USE_SPIFFS>0
|
#if HASP_USE_SPIFFS > 0
|
||||||
} else {
|
if(!SPIFFS.begin()) {
|
||||||
|
Log.error(F("FILE: SPI flash init failed. Unable to mount FS: Using default settings..."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
DynamicJsonDocument settings(1024 + 128);
|
configGetConfig(settings, true);
|
||||||
Serial.print(__FILE__);
|
}
|
||||||
Serial.println(__LINE__);
|
|
||||||
|
|
||||||
configGetConfig(settings, true);
|
//#if HASP_USE_SPIFFS > 0
|
||||||
|
|
||||||
Log.error(F("FILE: SPI flash init failed. Unable to mount FS: Using default settings..."));
|
|
||||||
#if HASP_USE_SPIFFS>0
|
|
||||||
Log.verbose(F("Loading debug settings"));
|
Log.verbose(F("Loading debug settings"));
|
||||||
debugSetConfig(settings[F("debug")]);
|
debugSetConfig(settings[F("debug")]);
|
||||||
Log.verbose(F("Loading GUI settings"));
|
Log.verbose(F("Loading GUI settings"));
|
||||||
@ -317,10 +366,11 @@ void configSetup()
|
|||||||
Log.verbose(F("Loading HTTP settings"));
|
Log.verbose(F("Loading HTTP settings"));
|
||||||
httpSetConfig(settings[F("http")]);
|
httpSetConfig(settings[F("http")]);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif // Wifi
|
||||||
|
// }
|
||||||
|
Log.notice(F("User configuration loaded"));
|
||||||
}
|
}
|
||||||
Log.notice(F("User configuration loaded"));
|
//#endif
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void configOutput(const JsonObject & settings)
|
void configOutput(const JsonObject & settings)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user