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(
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;

View File

@ -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<uint32_t, ESPUIclient*> MapOfClients;

View File

@ -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<void(Control*, int)> callback,
const String& value, ControlColor color, bool visible, uint16_t parentControl)
Control::Control(ControlType type, const char* label, std::function<void(Control*, int)> 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::function<void(Control
wide(false),
vertical(false),
enabled(true),
auto_update_value(false),
parentControl(parentControl),
next(nullptr)
{
@ -154,120 +155,102 @@ void Control::onWsEvent(String & cmd, String& data)
{
// Serial.println(String(F("Control::onWsEvent")));
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")))
{
SendCallback(B_DOWN);
break;
arg = B_DOWN;
}
if (cmd.equals(F("bup")))
else if (cmd.equals(F("bup")))
{
SendCallback(B_UP);
break;
arg = B_UP;
}
if (cmd.equals(F("pfdown")))
else if (cmd.equals(F("pfdown")))
{
SendCallback(P_FOR_DOWN);
break;
arg = P_FOR_DOWN;
}
if (cmd.equals(F("pfup")))
else if (cmd.equals(F("pfup")))
{
SendCallback(P_FOR_UP);
break;
arg = P_FOR_UP;
}
if (cmd.equals(F("pldown")))
else if (cmd.equals(F("pldown")))
{
SendCallback(P_LEFT_DOWN);
break;
arg = P_LEFT_DOWN;
}
else if (cmd.equals(F("plup")))
{
SendCallback(P_LEFT_UP);
arg = P_LEFT_UP;
}
else if (cmd.equals(F("prdown")))
{
SendCallback(P_RIGHT_DOWN);
arg = P_RIGHT_DOWN;
}
else if (cmd.equals(F("prup")))
{
SendCallback(P_RIGHT_UP);
arg = P_RIGHT_UP;
}
else if (cmd.equals(F("pbdown")))
{
SendCallback(P_BACK_DOWN);
arg = P_BACK_DOWN;
}
else if (cmd.equals(F("pbup")))
{
SendCallback(P_BACK_UP);
arg = P_BACK_UP;
}
else if (cmd.equals(F("pcdown")))
{
SendCallback(P_CENTER_DOWN);
arg = P_CENTER_DOWN;
}
else if (cmd.equals(F("pcup")))
{
SendCallback(P_CENTER_UP);
arg = P_CENTER_UP;
}
else if (cmd.equals(F("sactive")))
{
if (auto_update_value)
value = "1";
SendCallback(S_ACTIVE);
arg = S_ACTIVE;
}
else if (cmd.equals(F("sinactive")))
{
if (auto_update_value)
value = "0";
// updateControl(c, client->id());
SendCallback(S_INACTIVE);
arg = S_INACTIVE;
}
else if (cmd.equals(F("slvalue")))
{
if (auto_update_value)
value = data;
// updateControl(c, client->id());
SendCallback(SL_VALUE);
arg = SL_VALUE;
}
else if (cmd.equals(F("nvalue")))
{
if (auto_update_value)
value = data;
// updateControl(c, client->id());
SendCallback(N_VALUE);
arg = N_VALUE;
}
else if (cmd.equals(F("tvalue")))
{
if (auto_update_value)
value = data;
// updateControl(c, client->id());
SendCallback(T_VALUE);
arg = T_VALUE;
}
else if (cmd.equals(F("tabvalue")))
{
SendCallback(0);
arg = 0;
}
else if (cmd.equals(F("svalue")))
{
if (auto_update_value)
value = data;
// updateControl(c, client->id());
SendCallback(S_VALUE);
arg = S_VALUE;
}
else if (cmd.equals(F("time")))
{
if (auto_update_value)
value = data;
// updateControl(c, client->id());
SendCallback(TM_VALUE);
arg = TM_VALUE;
}
else
{
@ -277,6 +260,23 @@ void Control::onWsEvent(String & cmd, String& data)
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);
}

View File

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