Да
#include <FS.h>
bool cleanConfig(){
putLogLn("cleanConfig begin");
if(!SPIFFS.format()){
putLogLn(" SPIFFS format failed");
return false;
}
putLogLn(" SPIFFS format done");
config_t config_default; // создаем переменную с дефолтовыми значениями
config=config_default;
putLogLn(" config reset to default");
putLogLn("cleanConfig end");
return true;
}
// загружаем данные конфига из нергонезависимой памяти
// https://esp8266.ru/arduino-ide-esp8266/#eeprom
// http://wikihandbk.com/wiki/Arduino:%D0%91%D0%B8%D0%B1%D0%BB%D0%B8%D0%BE%D1%82%D0%B5%D0%BA%D0%B8/EEPROM/get()
uint8_t loadConfig(){
// putLogLn("loadConfig begin");
// putLogLn(" cfg addr=" +String((int)&config, DEC));
if(!SPIFFS.begin()){
putLogLn(" SPIFFS mount failed");
return false;
}
// putLogLn(" SPIFFS mount");
if (!SPIFFS.exists(settingsFilename)){
putLogLn(" Config file not exsist");
// if (!settings_save()){
// return false;
// }
}
// putLogLn(" SPIFFS file found");
File spconfig = SPIFFS.open(settingsFilename, "r");
if (!spconfig){
putLogLn(" Could'n read file");
return false;
}
// putLogLn(" SPIFFS opened");
// putLogLn(" config_t size=" +String(sizeof(config_t), DEC));
// putLogLn(" SPIFFS confg size=" +String(sizeof(config), DEC));
// putLogLn(" SPIFFS file size=" +String(spconfig.size(), DEC));
// putLogLn(" SPIFFS file position=" +String(spconfig.position(), DEC));
long unsigned int bytesRead = spconfig.read((uint8_t*)&config, sizeof(config_t));
// putLogLn(" SPIFFS file position=" +String(spconfig.position(), DEC));
// putLogLn(" SPIFFS read byte=" +String(bytesRead, DEC));
spconfig.close();
// putLogLn(" SPIFFS close");
SPIFFS.end();
// putLogLn(" SPIFFS end");
if (bytesRead != sizeof(config_t)){
putLogLn(" read bytes not valid size");
// загруженный конфиг по длинне отличается от структуры конфига. может добавились/удалили параметры. сбрасываем конфиг к дефолту.
cleanConfig();
return false;
}else{
putLogLn("loadConfig done");
return true;
}
}
// https://www.youtube.com/watch?v=pcfzf3NT6SI
bool saveConfig(){
// putLogLn("saveConfig begin");
// putLogLn(" cfg size=" +String(sizeof(config_t), DEC));
// putLogLn(" cfg addr=" +String((int)&config, DEC));
if(!SPIFFS.begin()){
putLogLn("SPIFFS mount failed");
return false;
}
// putLogLn(" SPIFFS mount");
File spconfig = SPIFFS.open(settingsFilename, "w");
if (!spconfig){
putLogLn("File open failed");
return false;
}
// putLogLn(" SPIFFS opened");
// putLogLn(" SPIFFS confg size=" +String(sizeof(config), DEC));
// putLogLn(" SPIFFS file size=" +String(spconfig.size(), DEC));
// putLogLn(" SPIFFS file position=" +String(spconfig.position(), DEC));
config.save_config=1; // ставим флаг что записали конфиг
long unsigned int bytesWrited = spconfig.write((uint8_t *)&config, sizeof(config_t));
// putLogLn(" SPIFFS writed byte=" +String(bytesWrited,DEC));
spconfig.close();
// putLogLn(" SPIFFS close");
SPIFFS.end();
// putLogLn(" SPIFFS end");
if (bytesWrited != sizeof(config_t)){
putLogLn(" writen != settings");
return false;
}else{
putLogLn("saveConfig done");
// putLogLn("saveConfig end");
return true;
}
}