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:
@@ -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);
|
||||
|
||||
@@ -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++) {
|
||||
|
||||
@@ -380,7 +380,7 @@ void registerUsermods()
|
||||
#endif
|
||||
|
||||
#ifdef USERMOD_WEATHER
|
||||
usermods.add(new WeatherUsermod());
|
||||
usermods.add(new WeatherUsermod("Weather", true));
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user