1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-22 14:20:53 +00:00

Add "autoupdate value" option for input controls

This commit is contained in:
Nikola Kirov 2023-11-30 14:54:50 +02:00
parent 8b64b185a4
commit 31911b3969
5 changed files with 83 additions and 69 deletions

9
CMakeLists.txt Normal file
View File

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

View File

@ -630,7 +630,10 @@ uint16_t ESPUIClass::addControl(ControlType type, const char* label, const Strin
uint16_t ESPUIClass::addControl( uint16_t ESPUIClass::addControl(
ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl) 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, 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); } while (false);
} }
bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, uint16_t clientId) bool ESPUIClass::SendJsonDocToWebSocket(ArduinoJson::DynamicJsonDocument& document, int clientId)
{ {
bool Response = false; bool Response = false;

View File

@ -105,6 +105,7 @@ public:
bool sliderContinuous = false; bool sliderContinuous = false;
void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len); void onWsEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len);
bool captivePortal = true; bool captivePortal = true;
bool auto_update_values = false;
void setVerbosity(Verbosity verbosity); void setVerbosity(Verbosity verbosity);
void begin(const char* _title, const char* username = nullptr, const char* password = nullptr, void begin(const char* _title, const char* username = nullptr, const char* password = nullptr,
@ -256,7 +257,7 @@ protected:
void NotifyClients(ClientUpdateType_t newState); void NotifyClients(ClientUpdateType_t newState);
void NotifyClient(uint32_t WsClientId, 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<uint32_t, ESPUIclient*> MapOfClients; std::map<uint32_t, ESPUIclient*> MapOfClients;

View File

@ -3,8 +3,8 @@
static uint16_t idCounter = 0; static uint16_t idCounter = 0;
static const String ControlError = "*** ESPUI ERROR: Could not transfer control ***"; static const String ControlError = "*** ESPUI ERROR: Could not transfer control ***";
Control::Control(ControlType type, const char* label, std::function<void(Control*, int)> callback, Control::Control(ControlType type, const char* label, std::function<void(Control*, int)> callback, const String& value,
const String& value, ControlColor color, bool visible, uint16_t parentControl) ControlColor color, bool visible, uint16_t parentControl)
: type(type), : type(type),
label(label), label(label),
callback(callback), callback(callback),
@ -14,6 +14,7 @@ Control::Control(ControlType type, const char* label, std::function<void(Control
wide(false), wide(false),
vertical(false), vertical(false),
enabled(true), enabled(true),
auto_update_value(false),
parentControl(parentControl), parentControl(parentControl),
next(nullptr) next(nullptr)
{ {
@ -154,129 +155,128 @@ void Control::onWsEvent(String & cmd, String& data)
{ {
// Serial.println(String(F("Control::onWsEvent"))); // Serial.println(String(F("Control::onWsEvent")));
SetControlChangedId(ESPUI.GetNextControlChangeId()); SetControlChangedId(ESPUI.GetNextControlChangeId());
if (!HasCallback())
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(String(F("Control::onWsEvent:No callback found for ID ")) + String(id));
}
#endif
break;
}
// Serial.println("Control::onWsEvent:Generating callback"); int arg = 0;
if (cmd.equals(F("bdown"))) if (cmd.equals(F("bdown")))
{ {
SendCallback(B_DOWN); arg = B_DOWN;
break;
} }
else if (cmd.equals(F("bup")))
if (cmd.equals(F("bup")))
{ {
SendCallback(B_UP); arg = B_UP;
break;
} }
else if (cmd.equals(F("pfdown")))
if (cmd.equals(F("pfdown")))
{ {
SendCallback(P_FOR_DOWN); arg = P_FOR_DOWN;
break;
} }
else if (cmd.equals(F("pfup")))
if (cmd.equals(F("pfup")))
{ {
SendCallback(P_FOR_UP); arg = P_FOR_UP;
break;
} }
else if (cmd.equals(F("pldown")))
if (cmd.equals(F("pldown")))
{ {
SendCallback(P_LEFT_DOWN); arg = P_LEFT_DOWN;
break;
} }
else if (cmd.equals(F("plup"))) else if (cmd.equals(F("plup")))
{ {
SendCallback(P_LEFT_UP); arg = P_LEFT_UP;
} }
else if (cmd.equals(F("prdown"))) else if (cmd.equals(F("prdown")))
{ {
SendCallback(P_RIGHT_DOWN); arg = P_RIGHT_DOWN;
} }
else if (cmd.equals(F("prup"))) else if (cmd.equals(F("prup")))
{ {
SendCallback(P_RIGHT_UP); arg = P_RIGHT_UP;
} }
else if (cmd.equals(F("pbdown"))) else if (cmd.equals(F("pbdown")))
{ {
SendCallback(P_BACK_DOWN); arg = P_BACK_DOWN;
} }
else if (cmd.equals(F("pbup"))) else if (cmd.equals(F("pbup")))
{ {
SendCallback(P_BACK_UP); arg = P_BACK_UP;
} }
else if (cmd.equals(F("pcdown"))) else if (cmd.equals(F("pcdown")))
{ {
SendCallback(P_CENTER_DOWN); arg = P_CENTER_DOWN;
} }
else if (cmd.equals(F("pcup"))) else if (cmd.equals(F("pcup")))
{ {
SendCallback(P_CENTER_UP); arg = P_CENTER_UP;
} }
else if (cmd.equals(F("sactive"))) else if (cmd.equals(F("sactive")))
{ {
value = "1"; if (auto_update_value)
SendCallback(S_ACTIVE); value = "1";
arg = S_ACTIVE;
} }
else if (cmd.equals(F("sinactive"))) else if (cmd.equals(F("sinactive")))
{ {
value = "0"; if (auto_update_value)
// updateControl(c, client->id()); value = "0";
SendCallback(S_INACTIVE); arg = S_INACTIVE;
} }
else if (cmd.equals(F("slvalue"))) else if (cmd.equals(F("slvalue")))
{ {
value = data; if (auto_update_value)
// updateControl(c, client->id()); value = data;
SendCallback(SL_VALUE); arg = SL_VALUE;
} }
else if (cmd.equals(F("nvalue"))) else if (cmd.equals(F("nvalue")))
{ {
value = data; if (auto_update_value)
// updateControl(c, client->id()); value = data;
SendCallback(N_VALUE); arg = N_VALUE;
} }
else if (cmd.equals(F("tvalue"))) else if (cmd.equals(F("tvalue")))
{ {
value = data; if (auto_update_value)
// updateControl(c, client->id()); value = data;
SendCallback(T_VALUE); arg = T_VALUE;
} }
else if (cmd.equals(F("tabvalue"))) else if (cmd.equals(F("tabvalue")))
{ {
SendCallback(0); arg = 0;
} }
else if (cmd.equals(F("svalue"))) else if (cmd.equals(F("svalue")))
{ {
value = data; if (auto_update_value)
// updateControl(c, client->id()); value = data;
SendCallback(S_VALUE); arg = S_VALUE;
} }
else if (cmd.equals(F("time"))) else if (cmd.equals(F("time")))
{ {
value = data; if (auto_update_value)
// updateControl(c, client->id()); value = data;
SendCallback(TM_VALUE); arg = TM_VALUE;
} }
else else
{ {
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
Serial.println(F("Control::onWsEvent:Malformed message from the websocket")); Serial.println(F("Control::onWsEvent:Malformed message from the websocket"));
} }
#endif #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); } while (false);
} }

View File

@ -62,6 +62,7 @@ public:
bool wide; bool wide;
bool vertical; bool vertical;
bool enabled; bool enabled;
bool auto_update_value;
uint16_t parentControl; uint16_t parentControl;
String panelStyle; String panelStyle;
String elementStyle; String elementStyle;