mirror of https://github.com/arendst/Tasmota.git
Refactor File Settings Demo
This commit is contained in:
parent
7e04a9f9d9
commit
f9ec44fb0b
|
@ -79,32 +79,25 @@ void CmndDrvText(void) {
|
||||||
* Driver Settings load and save
|
* Driver Settings load and save
|
||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
|
||||||
uint32_t GetDrvDemoSettingsCrc32(void) {
|
uint32_t DrvDemoSettingsCrc32(void) {
|
||||||
// Use Tasmota CRC calculation function
|
// Use Tasmota CRC calculation function
|
||||||
return GetCfgCrc32((uint8_t*)&Drv98Settings +4, sizeof(Drv98Settings) -4); // Skip crc32
|
return GetCfgCrc32((uint8_t*)&Drv98Settings +4, sizeof(Drv98Settings) -4); // Skip crc32
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrvDemoPreInit(void) {
|
void DrvDemoSettingsDefault(void) {
|
||||||
// Init default values in case file is not found
|
// Init default values in case file is not found
|
||||||
|
|
||||||
|
AddLog(LOG_LEVEL_INFO, PSTR("DRV: " D_USE_DEFAULTS));
|
||||||
|
|
||||||
memset(&Drv98Settings, 0x00, sizeof(Drv98Settings));
|
memset(&Drv98Settings, 0x00, sizeof(Drv98Settings));
|
||||||
Drv98Settings.version = DRV98_VERSION;
|
Drv98Settings.version = DRV98_VERSION;
|
||||||
// Init any other parameter in struct Drv98Settings
|
// Init any other parameter in struct Drv98Settings
|
||||||
snprintf_P(Drv98Settings.drv_text[0], sizeof(Drv98Settings.drv_text[0]), PSTR("Azalea"));
|
snprintf_P(Drv98Settings.drv_text[0], sizeof(Drv98Settings.drv_text[0]), PSTR("Azalea"));
|
||||||
|
}
|
||||||
|
|
||||||
// Try to load file /drvset098
|
void DrvDemoSettingsDelta(void) {
|
||||||
char filename[20];
|
|
||||||
// Use for sensors:
|
|
||||||
// snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_SENSOR), XSNS_98);
|
|
||||||
// Use for drivers:
|
|
||||||
snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), XDRV_98);
|
|
||||||
|
|
||||||
AddLog(LOG_LEVEL_INFO, PSTR("DRV: About to load settings from file %s"), filename);
|
|
||||||
|
|
||||||
#ifdef USE_UFILESYS
|
|
||||||
|
|
||||||
bool result = TfsLoadFile(filename, (uint8_t*)&Drv98Settings, sizeof(Drv98Settings));
|
|
||||||
if (result) {
|
|
||||||
// Fix possible setting deltas
|
// Fix possible setting deltas
|
||||||
|
|
||||||
if (Drv98Settings.version != DRV98_VERSION) { // Fix version dependent changes
|
if (Drv98Settings.version != DRV98_VERSION) { // Fix version dependent changes
|
||||||
|
|
||||||
if (Settings.version < 0x01010100) {
|
if (Settings.version < 0x01010100) {
|
||||||
|
@ -120,23 +113,45 @@ void DrvDemoPreInit(void) {
|
||||||
Drv98Settings.version = DRV98_VERSION;
|
Drv98Settings.version = DRV98_VERSION;
|
||||||
DrvDemoSettingsSave();
|
DrvDemoSettingsSave();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DrvDemoSettingsLoad(void) {
|
||||||
|
// Called from FUNC_PRE_INIT once at restart
|
||||||
|
|
||||||
|
// Init default values in case file is not found
|
||||||
|
DrvDemoSettingsDefault();
|
||||||
|
|
||||||
|
// Try to load file /.drvset098
|
||||||
|
char filename[20];
|
||||||
|
// Use for sensors:
|
||||||
|
// snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_SENSOR), XSNS_98);
|
||||||
|
// Use for drivers:
|
||||||
|
snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), XDRV_98);
|
||||||
|
|
||||||
|
AddLog(LOG_LEVEL_INFO, PSTR("DRV: About to load settings from file %s"), filename);
|
||||||
|
|
||||||
|
#ifdef USE_UFILESYS
|
||||||
|
if (TfsLoadFile(filename, (uint8_t*)&Drv98Settings, sizeof(Drv98Settings))) {
|
||||||
|
// Fix possible setting deltas
|
||||||
|
DrvDemoSettingsDelta();
|
||||||
} else {
|
} else {
|
||||||
// File system not ready: No flash space reserved for file system
|
// File system not ready: No flash space reserved for file system
|
||||||
AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not ready or file not found"));
|
AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not ready or file not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not enabled"));
|
AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not enabled"));
|
||||||
|
|
||||||
#endif // USE_UFILESYS
|
#endif // USE_UFILESYS
|
||||||
Drv98Settings.crc32 = GetDrvDemoSettingsCrc32();
|
|
||||||
|
Drv98Settings.crc32 = DrvDemoSettingsCrc32();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrvDemoSettingsSave(void) {
|
void DrvDemoSettingsSave(void) {
|
||||||
if (GetDrvDemoSettingsCrc32() != Drv98Settings.crc32) {
|
// Called from FUNC_SAVE_SETTINGS every SaveData second and at restart
|
||||||
// Try to save file /drvset098
|
|
||||||
Drv98Settings.crc32 = GetDrvDemoSettingsCrc32();
|
if (DrvDemoSettingsCrc32() != Drv98Settings.crc32) {
|
||||||
|
// Try to save file /.drvset098
|
||||||
|
Drv98Settings.crc32 = DrvDemoSettingsCrc32();
|
||||||
|
|
||||||
char filename[20];
|
char filename[20];
|
||||||
// Use for sensors:
|
// Use for sensors:
|
||||||
// snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_SENSOR), XSNS_98);
|
// snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_SENSOR), XSNS_98);
|
||||||
|
@ -146,15 +161,12 @@ void DrvDemoSettingsSave(void) {
|
||||||
AddLog(LOG_LEVEL_INFO, PSTR("DRV: About to save settings to file %s"), filename);
|
AddLog(LOG_LEVEL_INFO, PSTR("DRV: About to save settings to file %s"), filename);
|
||||||
|
|
||||||
#ifdef USE_UFILESYS
|
#ifdef USE_UFILESYS
|
||||||
bool result = TfsSaveFile(filename, (const uint8_t*)&Drv98Settings, sizeof(Drv98Settings));
|
if (!TfsSaveFile(filename, (const uint8_t*)&Drv98Settings, sizeof(Drv98Settings))) {
|
||||||
if (!result) {
|
|
||||||
// File system not ready: No flash space reserved for file system
|
// File system not ready: No flash space reserved for file system
|
||||||
AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not ready or unable to save file"));
|
AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not ready or unable to save file"));
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
||||||
AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not enabled"));
|
AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not enabled"));
|
||||||
|
|
||||||
#endif // USE_UFILESYS
|
#endif // USE_UFILESYS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,14 +179,14 @@ bool Xdrv98(uint8_t function) {
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
switch (function) {
|
switch (function) {
|
||||||
case FUNC_COMMAND:
|
|
||||||
result = DecodeCommand(kDrvDemoCommands, DrvDemoCommand);
|
|
||||||
break;
|
|
||||||
case FUNC_SAVE_SETTINGS:
|
case FUNC_SAVE_SETTINGS:
|
||||||
DrvDemoSettingsSave();
|
DrvDemoSettingsSave();
|
||||||
break;
|
break;
|
||||||
|
case FUNC_COMMAND:
|
||||||
|
result = DecodeCommand(kDrvDemoCommands, DrvDemoCommand);
|
||||||
|
break;
|
||||||
case FUNC_PRE_INIT:
|
case FUNC_PRE_INIT:
|
||||||
DrvDemoPreInit();
|
DrvDemoSettingsLoad();
|
||||||
break;
|
break;
|
||||||
case FUNC_SAVE_BEFORE_RESTART:
|
case FUNC_SAVE_BEFORE_RESTART:
|
||||||
// !!! DO NOT USE AS IT'S FUNCTION IS BETTER HANDLED BY FUNC_SAVE_SETTINGS !!!
|
// !!! DO NOT USE AS IT'S FUNCTION IS BETTER HANDLED BY FUNC_SAVE_SETTINGS !!!
|
||||||
|
|
Loading…
Reference in New Issue