Usermod: Implement shim for oappend

Use a static Print* to transform old oappend calls to print calls.
This commit is contained in:
Will Miles 2024-09-17 18:26:46 -04:00
parent 32f9616b6e
commit 16f61ea96d
2 changed files with 22 additions and 1 deletions

View File

@ -302,7 +302,7 @@ class Usermod {
virtual bool handleButton(uint8_t b) { return false; } // button overrides are possible here
virtual bool getUMData(um_data_t **data) { if (data) *data = nullptr; return false; }; // usermod data exchange [see examples for audio effects]
virtual void connected() {} // called when WiFi is (re)connected
virtual void appendConfigData(Print&) {} // helper function called from usermod settings page to add metadata for entry fields
virtual void appendConfigData(Print&); // helper function called from usermod settings page to add metadata for entry fields
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
@ -314,6 +314,16 @@ class Usermod {
virtual void onUpdateBegin(bool) {} // fired prior to and after unsuccessful firmware update
virtual void onStateChange(uint8_t mode) {} // fired upon WLED state change
virtual uint16_t getId() {return USERMOD_ID_UNSPECIFIED;}
// API shims
private:
static Print* oappend_shim;
// old form of appendConfigData; called by default appendConfigData(Print&) with oappend_shim set up
// private so it is not accidentally invoked except via Usermod::appendConfigData(Print&)
virtual void appendConfigData() {}
protected:
// Shim for oappend(), which used to exist in utils.cpp
template<typename T> static inline void oappend(const T& t) { oappend_shim->print(t); };
};
class UsermodManager {

View File

@ -68,3 +68,14 @@ bool UsermodManager::add(Usermod* um)
ums[numMods++] = um;
return true;
}
/* Usermod v2 interface shim for oappend */
Print* Usermod::oappend_shim = nullptr;
void Usermod::appendConfigData(Print& p) {
assert(!oappend_shim);
oappend_shim = &p;
this->appendConfigData();
oappend_shim = nullptr;
}