Add hasp_nvs

This commit is contained in:
fvanroie 2022-05-07 00:32:10 +02:00
parent 8d2b27509d
commit 87c1c8ee0c
4 changed files with 49 additions and 23 deletions

25
src/hasp/hasp_nvs.cpp Normal file
View File

@ -0,0 +1,25 @@
#ifdef ESP32
#include "hasplib.h"
#include "hasp_nvs.h"
bool nvsUpdateString(Preferences& preferences, const char* key, JsonVariant value)
{
bool changed = false;
const char* val = value.as<const char*>();
if(!value.isNull()) { // Json key exists
if(preferences.isKey(key)) { // Nvs key exists
changed = preferences.getString(key, "") != String(val); // Value changed
} else
changed = true; // Nvs key doesnot exist, create it
if(changed) {
size_t len = preferences.putString(key, val);
LOG_VERBOSE(TAG_TIME, F(D_BULLET "Wrote %s => %s (%d bytes)"), key, val, len);
}
}
return changed;
}
#endif

16
src/hasp/hasp_nvs.h Normal file
View File

@ -0,0 +1,16 @@
/* MIT License - Copyright (c) 2019-2022 Francis Van Roie
For full license information read the LICENSE file in the project folder */
#ifndef HASP_NVS_H
#define HASP_NVS_H
#ifdef ESP32
#include "hasplib.h"
#include <Preferences.h>
bool nvsUpdateString(Preferences& preferences, const char* key, JsonVariant value);
#endif // ESP32
#endif // HASP_NVS_H

View File

@ -33,6 +33,10 @@
#include "hasp/lv_theme_hasp.h"
#ifdef ESP32
#include "hasp/hasp_nvs.h"
#endif
#include "ArduinoJson.h"
#if HASP_USE_FREETYPE > 0

View File

@ -91,25 +91,6 @@ bool timeGetConfig(const JsonObject& settings)
return changed;
}
bool nvsUpateString(Preferences& preferences, const char* key, JsonVariant value)
{
bool changed = false;
const char* val = value.as<const char*>();
if(!value.isNull()) { // Json key exists
if(preferences.isKey(key)) { // Nvs key exists
changed = preferences.getString(key, "") != String(val); // Value changed
} else
changed = true; // Nvs key doesnot exist, create it
if(changed) {
size_t len = preferences.putString(key, val);
LOG_VERBOSE(TAG_TIME, F(D_BULLET "Wrote %s => %s (%d bytes)"), key, val, len);
}
}
return changed;
}
bool timeSetConfig(const JsonObject& settings)
{
Preferences preferences;
@ -119,10 +100,10 @@ bool timeSetConfig(const JsonObject& settings)
bool changed = false;
char key[16] = "tz";
changed |= nvsUpateString(preferences, key, settings[key]);
changed |= nvsUpateString(preferences, "ntp1", settings["ntp"][0]);
changed |= nvsUpateString(preferences, "ntp2", settings["ntp"][1]);
changed |= nvsUpateString(preferences, "ntp3", settings["ntp"][2]);
changed |= nvsUpdateString(preferences, key, settings[key]);
changed |= nvsUpdateString(preferences, "ntp1", settings["ntp"][0]);
changed |= nvsUpdateString(preferences, "ntp2", settings["ntp"][1]);
changed |= nvsUpdateString(preferences, "ntp3", settings["ntp"][2]);
preferences.end();
timeSetup();