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:
Ewoud
2023-03-16 15:41:23 +01:00
parent e3c359a4a4
commit c928df9d70
7 changed files with 34 additions and 32 deletions

View File

@@ -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;

View File

@@ -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));

View File

@@ -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=&quot;https://mm.kno.wled.ge/moonmodules/Weather&quot;\" 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"},

View File

@@ -263,6 +263,7 @@ class Usermod {
um_data_t *um_data; // um_data should be allocated using new in (derived) Usermod's setup() or constructor
bool enabled = false; //WLEDMM
const char *_name; //WLEDMM
bool initDone = false; //WLEDMM
public:
Usermod(const char *_name = nullptr, bool enabled=false) { um_data = nullptr; this->_name = _name; this->enabled=enabled;}
virtual ~Usermod() { if (um_data) delete um_data; }
@@ -276,8 +277,14 @@ class Usermod {
virtual void addToJsonState(JsonObject& obj) {} // add JSON objects for WLED state
virtual void addToJsonInfo(JsonObject& obj) {} // add JSON objects for UI Info page
virtual void readFromJsonState(JsonObject& obj) {} // process JSON messages received from web server
virtual void addToConfig(JsonObject& obj) {} // add JSON entries that go to cfg.json
virtual bool readFromConfig(JsonObject& obj) { return true; } // Note as of 2021-06 readFromConfig() now needs to return a bool, see usermod_v2_example.h
virtual void addToConfig(JsonObject& obj) { // add JSON entries that go to cfg.json
JsonObject top = obj.createNestedObject(FPSTR(_name)); // WLEDMM: set enabled and _name
top[FPSTR("enabled")] = enabled;
}
virtual bool readFromConfig(JsonObject& obj) { // Note as of 2021-06 readFromConfig() now needs to return a bool, see usermod_v2_example.h
JsonObject top = obj[FPSTR(_name)]; // WLEDMM: get enabled and _name
return !top.isNull() && getJsonValue(top[FPSTR("enabled")], enabled);
}
virtual void onMqttConnect(bool sessionPresent) {} // fired when MQTT connection is established (so usermod can subscribe)
virtual bool onMqttMessage(char* topic, char* payload) { return false; } // fired upon MQTT message received (wled topic)
virtual void onUpdateBegin(bool) {} // fired prior to and after unsuccessful firmware update
@@ -297,7 +304,7 @@ class UsermodManager {
bool getUMData(um_data_t **um_data, uint8_t mod_id = USERMOD_ID_RESERVED); // USERMOD_ID_RESERVED will poll all usermods
void setup();
void connected();
void appendConfigData();
// void appendConfigData(); //WLEDMM not used
void addToJsonState(JsonObject& obj);
void addToJsonInfo(JsonObject& obj);
void readFromJsonState(JsonObject& obj);

View File

@@ -8,7 +8,7 @@ void UsermodManager::setup() { for (byte i = 0; i < numMods; i++) um
void UsermodManager::connected() { for (byte i = 0; i < numMods; i++) ums[i]->connected(); }
void UsermodManager::loop() { for (byte i = 0; i < numMods; i++) ums[i]->loop(); }
void UsermodManager::handleOverlayDraw() { for (byte i = 0; i < numMods; i++) ums[i]->handleOverlayDraw(); }
void UsermodManager::appendConfigData() { for (byte i = 0; i < numMods; i++) ums[i]->appendConfigData(); }
// void UsermodManager::appendConfigData() { for (byte i = 0; i < numMods; i++) ums[i]->appendConfigData(); } //WLEDMM not used
bool UsermodManager::handleButton(uint8_t b) {
bool overrideIO = false;
for (byte i = 0; i < numMods; i++) {

View File

@@ -380,7 +380,7 @@ void registerUsermods()
#endif
#ifdef USERMOD_WEATHER
usermods.add(new WeatherUsermod());
usermods.add(new WeatherUsermod("Weather", true));
#endif

View File

@@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2303160
#define VERSION 2303161
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG