diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..95c7aa0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,9 @@ +set(includedirs src) + +set(req arduino ESPAsyncWebServer ArduinoJson) + +idf_component_register( + INCLUDE_DIRS ${includedirs} + SRC_DIRS src + SRCS ${SOURCES} + REQUIRES ${req}) diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index 8fdb806..341ec88 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -630,7 +630,10 @@ uint16_t ESPUIClass::addControl(ControlType type, const char* label, const Strin uint16_t ESPUIClass::addControl( ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl) { - return addControl(type, label, value, color, parentControl, new Control(type, label, nullptr, value, color, true, parentControl)); + Control * ctrl = new Control(type, label, nullptr, value, color, true, parentControl); + if (auto_update_values && ctrl) + ctrl->auto_update_value = true; + return addControl(type, label, value, color, parentControl, ctrl); } uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color, @@ -1103,7 +1106,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId) } while (false); } -bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId) +bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, int clientId) { bool Response = false; diff --git a/src/ESPUI.h b/src/ESPUI.h index 74862ba..7bf0577 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -105,6 +105,7 @@ public: bool sliderContinuous = false; void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len); bool captivePortal = true; + bool auto_update_values = false; void setVerbosity(Verbosity verbosity); void begin(const char* _title, const char* username = nullptr, const char* password = nullptr, @@ -256,7 +257,7 @@ protected: void NotifyClients(ClientUpdateType_t newState); void NotifyClient(uint32_t WsClientId, ClientUpdateType_t newState); - bool SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId); + bool SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, int clientId); std::map MapOfClients; diff --git a/src/ESPUIcontrol.cpp b/src/ESPUIcontrol.cpp index 75606de..0773be5 100644 --- a/src/ESPUIcontrol.cpp +++ b/src/ESPUIcontrol.cpp @@ -3,8 +3,8 @@ static uint16_t idCounter = 0; static const String ControlError = "*** ESPUI ERROR: Could not transfer control ***"; -Control::Control(ControlType type, const char* label, std::function callback, - const String& value, ControlColor color, bool visible, uint16_t parentControl) +Control::Control(ControlType type, const char* label, std::function callback, const String& value, + ControlColor color, bool visible, uint16_t parentControl) : type(type), label(label), callback(callback), @@ -14,6 +14,7 @@ Control::Control(ControlType type, const char* label, std::functionid()); - SendCallback(S_INACTIVE); + if (auto_update_value) + value = "0"; + arg = S_INACTIVE; } else if (cmd.equals(F("slvalue"))) { - value = data; - // updateControl(c, client->id()); - SendCallback(SL_VALUE); + if (auto_update_value) + value = data; + arg = SL_VALUE; } else if (cmd.equals(F("nvalue"))) { - value = data; - // updateControl(c, client->id()); - SendCallback(N_VALUE); + if (auto_update_value) + value = data; + arg = N_VALUE; } else if (cmd.equals(F("tvalue"))) { - value = data; - // updateControl(c, client->id()); - SendCallback(T_VALUE); + if (auto_update_value) + value = data; + arg = T_VALUE; } else if (cmd.equals(F("tabvalue"))) { - SendCallback(0); + arg = 0; } else if (cmd.equals(F("svalue"))) { - value = data; - // updateControl(c, client->id()); - SendCallback(S_VALUE); + if (auto_update_value) + value = data; + arg = S_VALUE; } else if (cmd.equals(F("time"))) { - value = data; - // updateControl(c, client->id()); - SendCallback(TM_VALUE); + if (auto_update_value) + value = data; + arg = TM_VALUE; } else { - #if defined(DEBUG_ESPUI) - if (ESPUI.verbosity) - { - Serial.println(F("Control::onWsEvent:Malformed message from the websocket")); - } - #endif + #if defined(DEBUG_ESPUI) + if (ESPUI.verbosity) + { + Serial.println(F("Control::onWsEvent:Malformed message from the websocket")); + } + #endif + break; } + + if (!HasCallback()) + { +#if defined(DEBUG_ESPUI) + if (ESPUI.verbosity) + { + Serial.println(String(F("Control::onWsEvent:No callback found for ID ")) + String(id)); + } +#endif + } + else + { + // Serial.println("Control::onWsEvent:Generating callback"); + SendCallback(arg); + } + } while (false); } diff --git a/src/ESPUIcontrol.h b/src/ESPUIcontrol.h index 194d697..fa556c7 100644 --- a/src/ESPUIcontrol.h +++ b/src/ESPUIcontrol.h @@ -62,6 +62,7 @@ public: bool wide; bool vertical; bool enabled; + bool auto_update_value; uint16_t parentControl; String panelStyle; String elementStyle;