diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index 0525775..065da77 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -475,9 +475,9 @@ void onWsEvent( return; } - if (c->callback == nullptr) + if (false == c->HasCallback()) { - #if defined(DEBUG_ESPUI) +#if defined(DEBUG_ESPUI) if (ESPUI.verbosity) { Serial.print(F("No callback found for ID ")); @@ -490,97 +490,97 @@ void onWsEvent( if (msg.startsWith(F("bdown:"))) { - c->callback(c, B_DOWN); + c->SendCallback(B_DOWN); } else if (msg.startsWith(F("bup:"))) { - c->callback(c, B_UP); + c->SendCallback(B_UP); } else if (msg.startsWith(F("pfdown:"))) { - c->callback(c, P_FOR_DOWN); + c->SendCallback(P_FOR_DOWN); } else if (msg.startsWith(F("pfup:"))) { - c->callback(c, P_FOR_UP); + c->SendCallback(P_FOR_UP); } else if (msg.startsWith(F("pldown:"))) { - c->callback(c, P_LEFT_DOWN); + c->SendCallback(P_LEFT_DOWN); } else if (msg.startsWith(F("plup:"))) { - c->callback(c, P_LEFT_UP); + c->SendCallback(P_LEFT_UP); } else if (msg.startsWith(F("prdown:"))) { - c->callback(c, P_RIGHT_DOWN); + c->SendCallback(P_RIGHT_DOWN); } else if (msg.startsWith(F("prup:"))) { - c->callback(c, P_RIGHT_UP); + c->SendCallback(P_RIGHT_UP); } else if (msg.startsWith(F("pbdown:"))) { - c->callback(c, P_BACK_DOWN); + c->SendCallback(P_BACK_DOWN); } else if (msg.startsWith(F("pbup:"))) { - c->callback(c, P_BACK_UP); + c->SendCallback(P_BACK_UP); } else if (msg.startsWith(F("pcdown:"))) { - c->callback(c, P_CENTER_DOWN); + c->SendCallback(P_CENTER_DOWN); } else if (msg.startsWith(F("pcup:"))) { - c->callback(c, P_CENTER_UP); + c->SendCallback(P_CENTER_UP); } else if (msg.startsWith(F("sactive:"))) { c->value = "1"; ESPUI.updateControl(c, client->id()); - c->callback(c, S_ACTIVE); + c->SendCallback(S_ACTIVE); } else if (msg.startsWith(F("sinactive:"))) { c->value = "0"; ESPUI.updateControl(c, client->id()); - c->callback(c, S_INACTIVE); + c->SendCallback(S_INACTIVE); } else if (msg.startsWith(F("slvalue:"))) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); ESPUI.updateControl(c, client->id()); - c->callback(c, SL_VALUE); + c->SendCallback(SL_VALUE); } else if (msg.startsWith(F("nvalue:"))) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); ESPUI.updateControl(c, client->id()); - c->callback(c, N_VALUE); + c->SendCallback(N_VALUE); } else if (msg.startsWith(F("tvalue:"))) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); ESPUI.updateControl(c, client->id()); - c->callback(c, T_VALUE); + c->SendCallback(T_VALUE); } else if (msg.startsWith("tabvalue:")) { - c->callback(c, client->id()); + c->SendCallback(client->id()); } else if (msg.startsWith(F("svalue:"))) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); ESPUI.updateControl(c, client->id()); - c->callback(c, S_VALUE); + c->SendCallback(S_VALUE); } else if (msg.startsWith(F("time:"))) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); ESPUI.updateControl(c, client->id()); - c->callback(c, TM_VALUE); + c->SendCallback(TM_VALUE); } else { @@ -600,10 +600,37 @@ void onWsEvent( } } -uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color, - uint16_t parentControl, void (*callback)(Control*, int)) +uint16_t ESPUIClass::addControl(ControlType type, const char* label) { - Control* control = new Control(type, label, callback, value, color, true, parentControl); + return addControl(type, label, String("")); +} + +uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value) +{ + return addControl(type, label, value, ControlColor::Turquoise); +} + +uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color) +{ + return addControl(type, label, value, color, Control::noParent); +} + +uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl) +{ + return addControl(type, label, value, color, parentControl, nullptr); +} + +uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl, void (*callback)(Control*, int)) +{ + uint16_t id = addControl(type, label, value, color, parentControl, nullptr, nullptr); + // set the original style callback + getControl(id)->callback = callback; + return id; +} + +uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl, void (*callback)(Control*, int, void *), void * UserData) +{ + Control* control = new Control(type, label, callback, UserData, value, color, true, parentControl); if (this->controls == nullptr) { @@ -1443,4 +1470,17 @@ void ESPUIClass::setVerbosity(Verbosity v) this->verbosity = v; } +void Control::SendCallback(int type) +{ + if(callback) + { + callback(this, type); + } + + if (extendedCallback) + { + extendedCallback(this, type, user); + } +} + ESPUIClass ESPUI; diff --git a/src/ESPUI.h b/src/ESPUI.h index a8bbcc3..c6daba6 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -137,6 +137,8 @@ public: uint16_t id; // just mirroring the id here for practical reasons const char* label; void (*callback)(Control*, int); + void (*extendedCallback)(Control*, int, void*); + void* user; String value; ControlColor color; bool visible; @@ -151,11 +153,16 @@ public: static constexpr uint16_t noParent = 0xffff; - Control(ControlType type, const char* label, void (*callback)(Control*, int), const String& value, - ControlColor color, bool visible = true, uint16_t parentControl = Control::noParent) + Control( + ControlType type, + const char* label, + void (*callback)(Control*, int, void*), + void* UserData, const String& value, ControlColor color, bool visible, uint16_t parentControl) : type(type), label(label), - callback(callback), + callback(nullptr), + extendedCallback(callback), + user(UserData), value(value), color(color), visible(visible), @@ -173,12 +180,16 @@ public: id(control.id), label(control.label), callback(control.callback), + extendedCallback(control.extendedCallback), + user(control.user), value(control.value), color(control.color), visible(control.visible), parentControl(control.parentControl), next(control.next) { } + void SendCallback(int type); + bool HasCallback() { return ((nullptr != callback) || (nullptr != extendedCallback)); } private: static uint16_t idCounter; @@ -241,9 +252,13 @@ public: // stuff into LITTLEFS void list(); // Lists LITTLEFS directory - uint16_t addControl(ControlType type, const char* label, const String& value = String(""), - ControlColor color = ControlColor::Turquoise, uint16_t parentControl = Control::noParent, - void (*callback)(Control*, int) = nullptr); + uint16_t addControl(ControlType type, const char* label); + uint16_t addControl(ControlType type, const char* label, const String& value); + uint16_t addControl(ControlType type, const char* label, const String& value, ControlColor color); + uint16_t addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl); + uint16_t addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl, void (*callback)(Control*, int)); + uint16_t addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl, void (*callback)(Control*, int, void *), void* UserData); + bool removeControl(uint16_t id, bool force_reload_ui = false); // create Elements