Usermod class vars pt3:
Moved initDone, addToConfig and readFromConfig to Usermod superclass
Updated cleanup procedure:
Part 1
- remove bool enabled = false/true (now default false)
- remove static const char _name[] and _enabled[]
- add constructor which calls superclass (temp?): XXXUsermod(const char *name, bool enabled):Usermod(name, enabled) {}
- replace _enabled with "enabled"
- remove const char PROGMEM init for _name[] and _enabled[]
Part 2
- Remove bool initDone = false;
- addToConfig: replace createNestedObject with Usermod::addToConfig(root); JsonObject top = root[FPSTR(_name)];
- readFromConfig: replace !top.isNull and enabled with bool configComplete = Usermod::readFromConfig(root);JsonObject top = root[FPSTR(_name)];
See Temperature, MPU6050 and weather as examples (rest to be done)
This commit is contained in:
@@ -21,7 +21,6 @@ class UsermodTemperature : public Usermod {
|
||||
|
||||
private:
|
||||
|
||||
bool initDone = false;
|
||||
OneWire *oneWire;
|
||||
// GPIO pin used for sensor (with a default compile-time fallback)
|
||||
int8_t temperaturePin = TEMPERATURE_PIN;
|
||||
@@ -322,9 +321,10 @@ class UsermodTemperature : public Usermod {
|
||||
* addToConfig() (called from set.cpp) stores persistent properties to cfg.json
|
||||
*/
|
||||
void addToConfig(JsonObject &root) {
|
||||
Usermod::addToConfig(root);
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
|
||||
// we add JSON object: {"Temperature": {"pin": 0, "degC": true}}
|
||||
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
|
||||
top[FPSTR("enabled")] = enabled;
|
||||
top["pin"] = temperaturePin; // usermodparam
|
||||
top["degC"] = degC; // usermodparam
|
||||
top[FPSTR(_readInterval)] = readingInterval / 1000;
|
||||
@@ -339,17 +339,18 @@ class UsermodTemperature : public Usermod {
|
||||
* The function should return true if configuration was successfully loaded or false if there was no configuration.
|
||||
*/
|
||||
bool readFromConfig(JsonObject &root) {
|
||||
bool configComplete = Usermod::readFromConfig(root);
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
|
||||
// we look for JSON object: {"Temperature": {"pin": 0, "degC": true}}
|
||||
int8_t newTemperaturePin = temperaturePin;
|
||||
DEBUG_PRINT(FPSTR(_name));
|
||||
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
if (top.isNull()) {
|
||||
DEBUG_PRINTLN(F(": No config found. (Using defaults.)"));
|
||||
return false;
|
||||
}
|
||||
|
||||
enabled = top[FPSTR("enabled")] | enabled;
|
||||
newTemperaturePin = top["pin"] | newTemperaturePin;
|
||||
degC = top["degC"] | degC;
|
||||
readingInterval = top[FPSTR(_readInterval)] | readingInterval/1000;
|
||||
|
||||
@@ -93,7 +93,6 @@ void IRAM_ATTR dmpDataReady() {
|
||||
class MPU6050Driver : public Usermod {
|
||||
private:
|
||||
MPU6050 mpu;
|
||||
bool initDone = false;
|
||||
unsigned long lastUMRun = millis();
|
||||
|
||||
// MPU control/status vars
|
||||
@@ -354,14 +353,14 @@ class MPU6050Driver : public Usermod {
|
||||
//{
|
||||
//}
|
||||
|
||||
void addToConfig(JsonObject& root)
|
||||
{
|
||||
JsonObject top = root.createNestedObject(FPSTR(_name));
|
||||
top[FPSTR("enabled")] = enabled;
|
||||
//JsonObject interruptPin = top.createNestedObject(FPSTR(_INT_pin));
|
||||
//interruptPin["pin"] = INTERRUPT_PIN;
|
||||
DEBUG_PRINTLN(F("MPU6050 IMU config saved."));
|
||||
}
|
||||
// void addToConfig(JsonObject& root)
|
||||
// {
|
||||
// Usermod::addToConfig(root);
|
||||
// JsonObject top = root[FPSTR(_name)];
|
||||
// // //JsonObject interruptPin = top.createNestedObject(FPSTR(_INT_pin));
|
||||
// // //interruptPin["pin"] = INTERRUPT_PIN;
|
||||
// // DEBUG_PRINTLN(F("MPU6050 IMU config saved."));
|
||||
// }
|
||||
|
||||
//WLEDMM: add appendConfigData
|
||||
void appendConfigData()
|
||||
@@ -380,6 +379,7 @@ class MPU6050Driver : public Usermod {
|
||||
|
||||
bool readFromConfig(JsonObject& root)
|
||||
{
|
||||
bool configComplete = Usermod::readFromConfig(root);
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
|
||||
if (top.isNull()) {
|
||||
@@ -388,8 +388,6 @@ class MPU6050Driver : public Usermod {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool configComplete = !top.isNull();
|
||||
configComplete &= getJsonValue(top[FPSTR("enabled")], enabled);
|
||||
//configComplete &= getJsonValue(top[FPSTR(_INT_pin)]["pin"], INTERRUPT_PIN);
|
||||
|
||||
DEBUG_PRINT(FPSTR(_name));
|
||||
|
||||
@@ -142,7 +142,6 @@ void httpGet(WiFiClient &client, const char *url, char *errorMessage) {
|
||||
class WeatherUsermod : public Usermod {
|
||||
private:
|
||||
// strings to reduce flash memory usage (used more than twice)
|
||||
static const char _name[]; //usermod name
|
||||
String apiKey = ""; //config var
|
||||
|
||||
unsigned long lastTime = 0; //will be used to download new forecast every hour
|
||||
@@ -150,6 +149,7 @@ class WeatherUsermod : public Usermod {
|
||||
bool isConnected = false; //only call openweathermap if connected
|
||||
|
||||
public:
|
||||
WeatherUsermod(const char *name, bool enabled):Usermod(name, enabled) {} //WLEDMM: this shouldn't be necessary (passthrough of constructor), maybe because Usermod is an abstract class
|
||||
|
||||
void setup() {
|
||||
strip.addEffect(255, &mode_2DWeather, _data_FX_MODE_2DWEATHER);
|
||||
@@ -256,7 +256,6 @@ class WeatherUsermod : public Usermod {
|
||||
/*
|
||||
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
|
||||
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
|
||||
* Below it is shown how this could be used for e.g. a light sensor
|
||||
*/
|
||||
void addToJsonInfo(JsonObject& root)
|
||||
{
|
||||
@@ -292,7 +291,8 @@ class WeatherUsermod : public Usermod {
|
||||
|
||||
void addToConfig(JsonObject& root)
|
||||
{
|
||||
JsonObject top = root.createNestedObject(FPSTR(_name));
|
||||
Usermod::addToConfig(root);
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
top[F("apiKey")] = apiKey;
|
||||
top[F("units")] = weather_units;
|
||||
top[F("minTemp")] = weather_minTemp;
|
||||
@@ -302,10 +302,9 @@ class WeatherUsermod : public Usermod {
|
||||
|
||||
bool readFromConfig(JsonObject& root)
|
||||
{
|
||||
bool configComplete = Usermod::readFromConfig(root);
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
|
||||
bool configComplete = !top.isNull();
|
||||
|
||||
configComplete &= getJsonValue(top[F("apiKey")], apiKey);
|
||||
configComplete &= getJsonValue(top[F("units")], weather_units);
|
||||
configComplete &= getJsonValue(top[F("minTemp")], weather_minTemp);
|
||||
@@ -317,8 +316,8 @@ class WeatherUsermod : public Usermod {
|
||||
|
||||
void appendConfigData()
|
||||
{
|
||||
oappend(SET_F("addInfo('Weather:help',0,'<button onclick=\"location.href="https://mm.kno.wled.ge/moonmodules/Weather"\" type=\"button\">?</button>');"));
|
||||
|
||||
oappend(SET_F("addHB('Weather');")); // WLEDMM
|
||||
|
||||
oappend(SET_F("dd=addDropdown('Weather','units');"));
|
||||
oappend(SET_F("addOption(dd,'Kelvin',0);"));
|
||||
oappend(SET_F("addOption(dd,'Celcius',1);"));
|
||||
@@ -349,9 +348,6 @@ class WeatherUsermod : public Usermod {
|
||||
}
|
||||
};
|
||||
|
||||
// strings to reduce flash memory usage (used more than twice)
|
||||
const char WeatherUsermod::_name[] PROGMEM = "Weather";
|
||||
|
||||
// example openweathermap data
|
||||
// {"cod":"200","message":0,"cnt":40,"list":[
|
||||
// {"dt":1663945200,"main":{"temp":18.05,"feels_like":17.79,"temp_min":17.64,"temp_max":18.05,"pressure":1014,"sea_level":1014,"grnd_level":1013,"humidity":72,"temp_kf":0.41},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":100},"wind":{"speed":4.32,"deg":238,"gust":5.6},"visibility":10000,"pop":0.48,"rain":{"3h":0.15},"sys":{"pod":"d"},"dt_txt":"2022-09-23 15:00:00"},
|
||||
|
||||
Reference in New Issue
Block a user