readFromConfig() update

This commit is contained in:
cschwinne
2021-06-25 01:26:15 +02:00
parent 660de0b4e5
commit 1e5420e6a7
18 changed files with 80 additions and 38 deletions

View File

@@ -427,7 +427,7 @@ class Animated_Staircase : public Usermod {
/*
* Reads the configuration to internal flash memory before setup() is called.
*/
void readFromConfig(JsonObject& root) {
bool readFromConfig(JsonObject& root) {
bool oldUseUSSensorTop = useUSSensorTop;
bool oldUseUSSensorBottom = useUSSensorBottom;
int8_t oldTopAPin = topPIRorTriggerPin;
@@ -435,6 +435,8 @@ class Animated_Staircase : public Usermod {
int8_t oldBottomAPin = bottomPIRorTriggerPin;
int8_t oldBottomBPin = bottomEchoPin;
bool configComplete = true;
JsonObject staircase = root[FPSTR(_name)];
if (!staircase.isNull()) {
if (staircase[FPSTR(_enabled)].is<bool>()) {
@@ -468,6 +470,7 @@ class Animated_Staircase : public Usermod {
bottomMaxDist = min(150,max(30,staircase[FPSTR(_bottomEchoCm)].as<int>())); // max distance ~1.5m (a lag of 9ms may be expected)
} else {
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
configComplete = false;
}
if (!initDone) {
// first run: reading from cfg.json
@@ -490,6 +493,7 @@ class Animated_Staircase : public Usermod {
}
if (changed) setup();
}
return configComplete;
}
/*

View File

@@ -133,13 +133,28 @@ class MyExampleUsermod : public Usermod {
* readFromConfig() is called BEFORE setup(). This means you can use your persistent values in setup() (e.g. pin assignments, buffer sizes),
* but also that if you want to write persistent values to a dynamic buffer, you'd need to allocate it here instead of in setup.
* If you don't know what that is, don't fret. It most likely doesn't affect your use case :)
*
* Return true in case your config was complete, or false if you'd like WLED to save your defaults to disk
*
* This function is guaranteed to be called on boot, but could also be called every time settings are updated
*/
void readFromConfig(JsonObject& root)
bool readFromConfig(JsonObject& root)
{
JsonObject top = root["top"];
userVar0 = 42; //set your variables to their boot default value (this can also be done when declaring the variable)
JsonObject top = root["exampleUsermod"];
if (!top.isNull()) {
userVar0 = top["great"] | 42; //The value right of the pipe "|" is the default value in case your setting was not present in cfg.json (e.g. first boot)
bool configComplete = true;
//check if value is there
if (top.containsKey("great")) {
//convert value to the correct type
userVar0 = top["great"].as<int>();
} else configComplete = false;
if (configComplete) return true;
}
return false;
}

View File

@@ -149,11 +149,13 @@ Delay <input type=\"number\" min=\"5\" max=\"300\" value=\"";
/**
* restore the changeable values
*/
void readFromConfig(JsonObject &root)
bool readFromConfig(JsonObject &root)
{
JsonObject top = root["FixUnreachableNetServices"];
if (top.isNull()) return false;
m_pingDelayMs = top["PingDelayMs"] | m_pingDelayMs;
m_pingDelayMs = max(5000UL, min(18000000UL, m_pingDelayMs));
return true;
}
/**

View File

@@ -321,13 +321,13 @@ public:
* restore the changeable values
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
*/
void readFromConfig(JsonObject &root)
bool readFromConfig(JsonObject &root)
{
bool oldEnabled = enabled;
int8_t oldPin = PIRsensorPin;
JsonObject top = root[FPSTR(_name)];
if (top.isNull()) return;
if (top.isNull()) return false;
if (top["pin"] != nullptr) {
PIRsensorPin = min(39,max(-1,top["pin"].as<int>())); // check bounds
@@ -399,6 +399,8 @@ public:
DEBUG_PRINTLN(F("PIR config (re)loaded."));
}
}
return true;
}
/**

View File

@@ -169,7 +169,7 @@ public:
/**
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
*/
void readFromConfig(JsonObject &root)
bool readFromConfig(JsonObject &root)
{
// we look for JSON object.
JsonObject top = root[FPSTR(_name)];
@@ -196,7 +196,9 @@ public:
else
{
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
return false;
}
return true;
}
};

View File

@@ -249,11 +249,13 @@ class UsermodTemperature : public Usermod {
/**
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
*/
void readFromConfig(JsonObject &root) {
bool readFromConfig(JsonObject &root) {
// we look for JSON object: {"Temperature": {"pin": 0, "degC": true}}
JsonObject top = root[FPSTR(_name)];
int8_t newTemperaturePin = temperaturePin;
bool configComplete = true;
if (!top.isNull() && top["pin"] != nullptr) {
if (top[FPSTR(_enabled)].is<bool>()) {
disabled = !top[FPSTR(_enabled)].as<bool>();
@@ -274,13 +276,14 @@ class UsermodTemperature : public Usermod {
DEBUG_PRINTLN(F("Temperature config (re)loaded."));
} else {
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
configComplete = false;
}
if (!initDone) {
// first run: reading from cfg.json
temperaturePin = newTemperaturePin;
} else {
// changing paramters from settings page
// changing parameters from settings page
if (newTemperaturePin != temperaturePin) {
// deallocate pin and release memory
delete oneWire;
@@ -290,6 +293,7 @@ class UsermodTemperature : public Usermod {
setup();
}
}
return configComplete;
}
uint16_t getId()

View File

@@ -348,11 +348,11 @@ class MultiRelay : public Usermod {
* restore the changeable values
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
*/
void readFromConfig(JsonObject &root) {
bool readFromConfig(JsonObject &root) {
int8_t oldPin[MULTI_RELAY_MAX_RELAYS];
JsonObject top = root[FPSTR(_name)];
if (top.isNull()) return;
if (top.isNull()) return false;
if (top[FPSTR(_enabled)] != nullptr) {
if (top[FPSTR(_enabled)].is<bool>()) {
@@ -413,6 +413,7 @@ class MultiRelay : public Usermod {
}
DEBUG_PRINTLN(F("MultiRelay config (re)loaded."));
}
return true;
}
/**

View File

@@ -82,18 +82,6 @@ class StairwayWipeUsermod : public Usermod {
//if (root["bri"] == 255) Serial.println(F("Don't burn down your garage!"));
}
void addToConfig(JsonObject& root)
{
JsonObject top = root.createNestedObject("exampleUsermod");
top["great"] = userVar0; //save this var persistently whenever settings are saved
}
void readFromConfig(JsonObject& root)
{
JsonObject top = root["top"];
userVar0 = top["great"] | 42; //The value right of the pipe "|" is the default value in case your setting was not present in cfg.json (e.g. first boot)
}
uint16_t getId()
{
return USERMOD_ID_EXAMPLE;

View File

@@ -198,12 +198,12 @@ class AutoSaveUsermod : public Usermod {
* but also that if you want to write persistent values to a dynamic buffer, you'd need to allocate it here instead of in setup.
* If you don't know what that is, don't fret. It most likely doesn't affect your use case :)
*/
void readFromConfig(JsonObject& root) {
bool readFromConfig(JsonObject& root) {
// we look for JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}}
JsonObject top = root[FPSTR(_name)];
if (top.isNull()) {
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
return;
return false;
}
if (top[FPSTR(_autoSaveEnabled)].is<bool>()) {
@@ -225,6 +225,7 @@ class AutoSaveUsermod : public Usermod {
applyAutoSaveOnBoot = (bool)(str!="off"); // off is guaranteed to be present
}
DEBUG_PRINTLN(F("Autosave config (re)loaded."));
return true;
}
/*

View File

@@ -615,12 +615,14 @@ class FourLineDisplayUsermod : public Usermod {
* but also that if you want to write persistent values to a dynamic buffer, you'd need to allocate it here instead of in setup.
* If you don't know what that is, don't fret. It most likely doesn't affect your use case :)
*/
void readFromConfig(JsonObject& root) {
bool readFromConfig(JsonObject& root) {
bool needsRedraw = false;
DisplayType newType = type;
int8_t newScl = sclPin;
int8_t newSda = sdaPin;
bool configComplete = true;
JsonObject top = root[FPSTR(_name)];
if (!top.isNull() && top["pin"] != nullptr) {
newScl = top["pin"][0];
@@ -653,6 +655,7 @@ class FourLineDisplayUsermod : public Usermod {
DEBUG_PRINTLN(F("4 Line Display config (re)loaded."));
} else {
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
configComplete = false;
}
if (!initDone) {
@@ -684,6 +687,8 @@ class FourLineDisplayUsermod : public Usermod {
setFlipMode(flip);
if (needsRedraw && !wakeDisplay()) redraw(true);
}
return configComplete;
}
/*