1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-24 22:40:54 +00:00

Moved management of the control list to control manager

This commit is contained in:
Martin 2024-07-18 11:53:57 -04:00
parent 89d592911e
commit 5c4a202808
2 changed files with 118 additions and 245 deletions

View File

@ -16,6 +16,7 @@
#if ESP8266 #if ESP8266
#include <umm_malloc/umm_heap_select.h> #include <umm_malloc/umm_heap_select.h>
#endif #endif
#include "ESPUIcontrolMgr.h"
static String heapInfo(const __FlashStringHelper* mode) static String heapInfo(const __FlashStringHelper* mode)
{ {
@ -429,7 +430,7 @@ void ESPUIClass::onWsEvent(
AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)
{ {
// Serial.println(String("ESPUIClass::OnWsEvent: type: ") + String(type)); // Serial.println(String("ESPUIClass::OnWsEvent: type: ") + String(type));
RemoveToBeDeletedControls(); ESPUIcontrolMgr.RemoveToBeDeletedControls();
if (WS_EVT_DISCONNECT == type) if (WS_EVT_DISCONNECT == type)
{ {
@ -470,247 +471,129 @@ void ESPUIClass::onWsEvent(
return; return;
} }
uint16_t ESPUIClass::addControl(Control::Type type, const char* label) Control::ControlId_t ESPUIClass::addControl(Control::Type type, const char* label)
{ {
return addControl(type, label, String("")); return addControl(type, label, emptyString);
} }
uint16_t ESPUIClass::addControl(Control::Type type, const char* label, const String& value) Control::ControlId_t ESPUIClass::addControl(Control::Type type, const char* label, const String& value)
{ {
return addControl(type, label, value, Control::Color::Turquoise); return addControl(type, label, value, Control::Color::Turquoise);
} }
uint16_t ESPUIClass::addControl(Control::Type type, const char* label, const String& value, Control::Color color) Control::ControlId_t ESPUIClass::addControl(Control::Type type, const char* label, const String& value, Control::Color color)
{ {
return addControl(type, label, value, color, Control::noParent); return addControl(type, label, value, color, Control::noParent);
} }
uint16_t ESPUIClass::addControl(Control::Type type, const char* label, const String& value, Control::Color color, uint16_t parentControl) Control::ControlId_t ESPUIClass::addControl(Control::Type type,
const char* label, const String& value, Control::Color color, Control::ControlId_t parentControlId)
{ {
return addControl(type, label, value, color, parentControl, new Control(type, label, nullptr, value, color, true, parentControl)); return addControl(type, label, value, color, parentControlId, (std::function<void(Control*, int)>)nullptr);
} }
uint16_t ESPUIClass::addControl(Control::Type type, const char* label, const String& value, Control::Color color, Control::ControlId_t ESPUIClass::addControl(Control::Type type,
uint16_t parentControl, std::function<void(Control*, int)> callback) const char* label, const String& value, Control::Color color,
Control::ControlId_t parentControlId, std::function<void(Control*, int)> callback)
{ {
uint16_t id = addControl(type, label, value, color, parentControl); Control::ControlId_t id = ESPUIcontrolMgr.addControl(type, label, value, color, parentControlId, true, callback);
// set the original style callback NotifyClients(ClientUpdateType_t::RebuildNeeded);
getControl(id)->callback = callback;
return id; return id;
} }
uint16_t ESPUIClass::addControl(Control::Type type, const char* label, const String& value, Control::Color color, uint16_t parentControl, Control* control) bool ESPUIClass::removeControl(Control::ControlId_t id, bool force_rebuild_ui)
{ {
#ifdef ESP32 bool Response = ESPUIcontrolMgr.removeControl(id);
xSemaphoreTake(ControlsSemaphore, portMAX_DELAY);
#endif // def ESP32
if (controls == nullptr)
{
controls = control;
}
else
{
Control* iterator = controls;
while (iterator->next != nullptr)
{
iterator = iterator->next;
}
iterator->next = control;
}
controlCount++;
#ifdef ESP32
xSemaphoreGive(ControlsSemaphore);
#endif // def ESP32
NotifyClients(ClientUpdateType_t::RebuildNeeded);
return control->id;
}
bool ESPUIClass::removeControl(uint16_t id, bool force_rebuild_ui)
{
bool Response = false;
Control* control = getControl(id);
if (control)
{
Response = true;
control->DeleteControl();
controlCount--;
if (force_rebuild_ui) if (force_rebuild_ui)
{ {
jsonReload(); ESPUI.jsonReload();
} }
else else
{ {
NotifyClients(ClientUpdateType_t::RebuildNeeded); ESPUI.NotifyClients(ClientUpdateType_t::RebuildNeeded);
} }
}
#ifdef DEBUG_ESPUI
else
{
// Serial.println(String("Could not Remove Control ") + String(id));
}
#endif // def DEBUG_ESPUI
return Response; return Response;
} } // removeControl
void ESPUIClass::RemoveToBeDeletedControls() Control::ControlId_t ESPUIClass::label(const char* label, Control::Color color, const String& value)
{
#ifdef ESP32
xSemaphoreTake(ControlsSemaphore, portMAX_DELAY);
#endif // def ESP32
Control* PreviousControl = nullptr;
Control* CurrentControl = controls;
while (nullptr != CurrentControl)
{
Control* NextControl = CurrentControl->next;
if (CurrentControl->ToBeDeleted())
{
if (CurrentControl == controls)
{
// this is the root control
controls = NextControl;
}
else
{
PreviousControl->next = NextControl;
}
delete CurrentControl;
CurrentControl = NextControl;
}
else
{
PreviousControl = CurrentControl;
CurrentControl = NextControl;
}
}
#ifdef ESP32
xSemaphoreGive(ControlsSemaphore);
#endif // def ESP32
}
uint16_t ESPUIClass::label(const char* label, Control::Color color, const String& value)
{ {
return addControl(Control::Type::Label, label, value, color); return addControl(Control::Type::Label, label, value, color);
} }
uint16_t ESPUIClass::graph(const char* label, Control::Color color) Control::ControlId_t ESPUIClass::graph(const char* label, Control::Color color)
{ {
return addControl(Control::Type::Graph, label, "", color); return addControl(Control::Type::Graph, label, "", color);
} }
uint16_t ESPUIClass::slider( Control::ControlId_t ESPUIClass::slider(
const char* label, std::function<void(Control*, int)> callback, Control::Color color, int value, int min, int max) const char* label, std::function<void(Control*, int)> callback, Control::Color color, int value, int min, int max)
{ {
uint16_t sliderId Control::ControlId_t sliderId
= addControl(Control::Type::Slider, label, String(value), color, Control::noParent, callback); = addControl(Control::Type::Slider, label, String(value), color, Control::noParent, callback);
addControl(Control::Type::Min, label, String(min), Control::Color::None, sliderId); addControl(Control::Type::Min, label, String(min), Control::Color::None, sliderId);
addControl(Control::Type::Max, label, String(max), Control::Color::None, sliderId); addControl(Control::Type::Max, label, String(max), Control::Color::None, sliderId);
return sliderId; return sliderId;
} }
uint16_t ESPUIClass::button(const char* label, std::function<void(Control*, int)> callback, Control::Color color, const String& value) Control::ControlId_t ESPUIClass::button(const char* label, std::function<void(Control*, int)> callback, Control::Color color, const String& value)
{ {
return addControl(Control::Type::Button, label, value, color, Control::noParent, callback); return addControl(Control::Type::Button, label, value, color, Control::noParent, callback);
} }
uint16_t ESPUIClass::switcher(const char* label, std::function<void(Control*, int)> callback, Control::Color color, bool startState) Control::ControlId_t ESPUIClass::switcher(const char* label, std::function<void(Control*, int)> callback, Control::Color color, bool startState)
{ {
return addControl(Control::Type::Switcher, label, startState ? "1" : "0", color, Control::noParent, callback); return addControl(Control::Type::Switcher, label, startState ? "1" : "0", color, Control::noParent, callback);
} }
uint16_t ESPUIClass::pad(const char* label, std::function<void(Control*, int)> callback, Control::Color color) Control::ControlId_t ESPUIClass::pad(const char* label, std::function<void(Control*, int)> callback, Control::Color color)
{ {
return addControl(Control::Type::Pad, label, "", color, Control::noParent, callback); return addControl(Control::Type::Pad, label, "", color, Control::noParent, callback);
} }
uint16_t ESPUIClass::padWithCenter(const char* label, std::function<void(Control*, int)> callback, Control::Color color) Control::ControlId_t ESPUIClass::padWithCenter(const char* label, std::function<void(Control*, int)> callback, Control::Color color)
{ {
return addControl(Control::Type::PadWithCenter, label, "", color, Control::noParent, callback); return addControl(Control::Type::PadWithCenter, label, "", color, Control::noParent, callback);
} }
uint16_t ESPUIClass::number( Control::ControlId_t ESPUIClass::number(
const char* label, std::function<void(Control*, int)> callback, Control::Color color, int number, int min, int max) const char* label, std::function<void(Control*, int)> callback, Control::Color color, int number, int min, int max)
{ {
uint16_t numberId = addControl(Control::Type::Number, label, String(number), color, Control::noParent, callback); Control::ControlId_t numberId = addControl(Control::Type::Number, label, String(number), color, Control::noParent, callback);
addControl(Control::Type::Min, label, String(min), Control::Color::None, numberId); addControl(Control::Type::Min, label, String(min), Control::Color::None, numberId);
addControl(Control::Type::Max, label, String(max), Control::Color::None, numberId); addControl(Control::Type::Max, label, String(max), Control::Color::None, numberId);
return numberId; return numberId;
} }
uint16_t ESPUIClass::gauge(const char* label, Control::Color color, int number, int min, int max) Control::ControlId_t ESPUIClass::gauge(const char* label, Control::Color color, int number, int min, int max)
{ {
uint16_t numberId = addControl(Control::Type::Gauge, label, String(number), color, Control::noParent); Control::ControlId_t numberId = addControl(Control::Type::Gauge, label, String(number), color, Control::noParent);
addControl(Control::Type::Min, label, String(min), Control::Color::None, numberId); addControl(Control::Type::Min, label, String(min), Control::Color::None, numberId);
addControl(Control::Type::Max, label, String(max), Control::Color::None, numberId); addControl(Control::Type::Max, label, String(max), Control::Color::None, numberId);
return numberId; return numberId;
} }
uint16_t ESPUIClass::separator(const char* label) Control::ControlId_t ESPUIClass::separator(const char* label)
{ {
return addControl(Control::Type::Separator, label, "", Control::Color::Alizarin, Control::noParent, nullptr); return addControl(Control::Type::Separator, label, "", Control::Color::Alizarin, Control::noParent, nullptr);
} }
uint16_t ESPUIClass::fileDisplay(const char* label, Control::Color color, String filename) Control::ControlId_t ESPUIClass::fileDisplay(const char* label, Control::Color color, String filename)
{ {
return addControl(Control::Type::FileDisplay, label, filename, color, Control::noParent); return addControl(Control::Type::FileDisplay, label, filename, color, Control::noParent);
} }
uint16_t ESPUIClass::accelerometer(const char* label, std::function<void(Control*, int)> callback, Control::Color color) Control::ControlId_t ESPUIClass::accelerometer(const char* label, std::function<void(Control*, int)> callback, Control::Color color)
{ {
return addControl(Control::Type::Accel, label, "", color, Control::noParent, callback); return addControl(Control::Type::Accel, label, "", color, Control::noParent, callback);
} }
uint16_t ESPUIClass::text(const char* label, std::function<void(Control*, int)> callback, Control::Color color, const String& value) Control::ControlId_t ESPUIClass::text(const char* label, std::function<void(Control*, int)> callback, Control::Color color, const String& value)
{ {
return addControl(Control::Type::Text, label, value, color, Control::noParent, callback); return addControl(Control::Type::Text, label, value, color, Control::noParent, callback);
} }
Control* ESPUIClass::getControl(uint16_t id) Control* ESPUIClass::getControl(Control::ControlId_t id) {return ESPUIcontrolMgr.getControl(id);}
{ Control* ESPUIClass::getControlNoLock(Control::ControlId_t id) {return ESPUIcontrolMgr.getControlNoLock(id);}
#ifdef ESP32
xSemaphoreTake(ControlsSemaphore, portMAX_DELAY);
Control* Response = getControlNoLock(id);
xSemaphoreGive(ControlsSemaphore);
return Response;
#else
return getControlNoLock(id);
#endif // !def ESP32
}
// WARNING: Anytime you walk the chain of controllers, the protection semaphore
// MUST be locked. This function assumes that the semaphore is locked
// at the time it is called. Make sure YOU locked it :)
Control* ESPUIClass::getControlNoLock(uint16_t id)
{
Control* Response = nullptr;
Control* control = controls;
while (nullptr != control)
{
if (control->id == id)
{
if (!control->ToBeDeleted())
{
Response = control;
}
break;
}
control = control->next;
}
return Response;
}
void ESPUIClass::updateControl(Control* control, int) void ESPUIClass::updateControl(Control* control, int)
{ {
@ -733,7 +616,7 @@ uint32_t ESPUIClass::GetNextControlChangeId()
return ++ControlChangeID; return ++ControlChangeID;
} }
void ESPUIClass::setPanelStyle(uint16_t id, const String& style, int clientId) void ESPUIClass::setPanelStyle(Control::ControlId_t id, const String& style, int clientId)
{ {
Control* control = getControl(id); Control* control = getControl(id);
if (control) if (control)
@ -743,7 +626,7 @@ void ESPUIClass::setPanelStyle(uint16_t id, const String& style, int clientId)
} }
} }
void ESPUIClass::setElementStyle(uint16_t id, const String& style, int clientId) void ESPUIClass::setElementStyle(Control::ControlId_t id, const String& style, int clientId)
{ {
Control* control = getControl(id); Control* control = getControl(id);
if (control) if (control)
@ -753,7 +636,7 @@ void ESPUIClass::setElementStyle(uint16_t id, const String& style, int clientId)
} }
} }
void ESPUIClass::setInputType(uint16_t id, const String& type, int clientId) void ESPUIClass::setInputType(Control::ControlId_t id, const String& type, int clientId)
{ {
Control* control = getControl(id); Control* control = getControl(id);
if (control) if (control)
@ -763,7 +646,7 @@ void ESPUIClass::setInputType(uint16_t id, const String& type, int clientId)
} }
} }
void ESPUIClass::setPanelWide(uint16_t id, bool wide) void ESPUIClass::setPanelWide(Control::ControlId_t id, bool wide)
{ {
Control* control = getControl(id); Control* control = getControl(id);
if (control) if (control)
@ -772,7 +655,7 @@ void ESPUIClass::setPanelWide(uint16_t id, bool wide)
} }
} }
void ESPUIClass::setEnabled(uint16_t id, bool enabled, int clientId) void ESPUIClass::setEnabled(Control::ControlId_t id, bool enabled, int clientId)
{ {
Control* control = getControl(id); Control* control = getControl(id);
if (control) if (control)
@ -783,7 +666,7 @@ void ESPUIClass::setEnabled(uint16_t id, bool enabled, int clientId)
} }
} }
void ESPUIClass::setVertical(uint16_t id, bool vert) void ESPUIClass::setVertical(Control::ControlId_t id, bool vert)
{ {
Control* control = getControl(id); Control* control = getControl(id);
if (control) if (control)
@ -792,7 +675,7 @@ void ESPUIClass::setVertical(uint16_t id, bool vert)
} }
} }
void ESPUIClass::updateControl(uint16_t id, int clientId) void ESPUIClass::updateControl(Control::ControlId_t id, int clientId)
{ {
Control* control = getControl(id); Control* control = getControl(id);
@ -821,7 +704,7 @@ void ESPUIClass::updateControlValue(Control* control, const String& value, int c
updateControl(control, clientId); updateControl(control, clientId);
} }
void ESPUIClass::updateControlValue(uint16_t id, const String& value, int clientId) void ESPUIClass::updateControlValue(Control::ControlId_t id, const String& value, int clientId)
{ {
Control* control = getControl(id); Control* control = getControl(id);
@ -839,7 +722,7 @@ void ESPUIClass::updateControlValue(uint16_t id, const String& value, int client
updateControlValue(control, value, clientId); updateControlValue(control, value, clientId);
} }
void ESPUIClass::updateControlLabel(uint16_t id, const char* value, int clientId) void ESPUIClass::updateControlLabel(Control::ControlId_t id, const char* value, int clientId)
{ {
updateControlLabel(getControl(id), value, clientId); updateControlLabel(getControl(id), value, clientId);
} }
@ -860,7 +743,7 @@ void ESPUIClass::updateControlLabel(Control* control, const char* value, int cli
updateControl(control, clientId); updateControl(control, clientId);
} }
void ESPUIClass::updateVisibility(uint16_t id, bool visibility, int clientId) void ESPUIClass::updateVisibility(Control::ControlId_t id, bool visibility, int clientId)
{ {
Control* control = getControl(id); Control* control = getControl(id);
if (control) if (control)
@ -870,57 +753,57 @@ void ESPUIClass::updateVisibility(uint16_t id, bool visibility, int clientId)
} }
} }
void ESPUIClass::print(uint16_t id, const String& value) void ESPUIClass::print(Control::ControlId_t id, const String& value)
{ {
updateControlValue(id, value); updateControlValue(id, value);
} }
void ESPUIClass::updateLabel(uint16_t id, const String& value) void ESPUIClass::updateLabel(Control::ControlId_t id, const String& value)
{ {
updateControlValue(id, value); updateControlValue(id, value);
} }
void ESPUIClass::updateButton(uint16_t id, const String& value) void ESPUIClass::updateButton(Control::ControlId_t id, const String& value)
{ {
updateControlValue(id, value); updateControlValue(id, value);
} }
void ESPUIClass::updateSlider(uint16_t id, int nValue, int clientId) void ESPUIClass::updateSlider(Control::ControlId_t id, int nValue, int clientId)
{ {
updateControlValue(id, String(nValue), clientId); updateControlValue(id, String(nValue), clientId);
} }
void ESPUIClass::updateSwitcher(uint16_t id, bool nValue, int clientId) void ESPUIClass::updateSwitcher(Control::ControlId_t id, bool nValue, int clientId)
{ {
updateControlValue(id, String(nValue ? "1" : "0"), clientId); updateControlValue(id, String(nValue ? "1" : "0"), clientId);
} }
void ESPUIClass::updateNumber(uint16_t id, int number, int clientId) void ESPUIClass::updateNumber(Control::ControlId_t id, int number, int clientId)
{ {
updateControlValue(id, String(number), clientId); updateControlValue(id, String(number), clientId);
} }
void ESPUIClass::updateText(uint16_t id, const String& text, int clientId) void ESPUIClass::updateText(Control::ControlId_t id, const String& text, int clientId)
{ {
updateControlValue(id, text, clientId); updateControlValue(id, text, clientId);
} }
void ESPUIClass::updateSelect(uint16_t id, const String& text, int clientId) void ESPUIClass::updateSelect(Control::ControlId_t id, const String& text, int clientId)
{ {
updateControlValue(id, text, clientId); updateControlValue(id, text, clientId);
} }
void ESPUIClass::updateGauge(uint16_t id, int number, int clientId) void ESPUIClass::updateGauge(Control::ControlId_t id, int number, int clientId)
{ {
updateControlValue(id, String(number), clientId); updateControlValue(id, String(number), clientId);
} }
void ESPUIClass::updateTime(uint16_t id, int clientId) void ESPUIClass::updateTime(Control::ControlId_t id, int clientId)
{ {
updateControl(id, clientId); updateControl(id, clientId);
} }
void ESPUIClass::clearGraph(uint16_t id, int clientId) void ESPUIClass::clearGraph(Control::ControlId_t id, int clientId)
{ {
do // once do // once
{ {
@ -935,14 +818,14 @@ void ESPUIClass::clearGraph(uint16_t id, int clientId)
root[F("type")] = (int)Control::Type::Graph + Control::Type::UpdateOffset; root[F("type")] = (int)Control::Type::Graph + Control::Type::UpdateOffset;
root[F("value")] = 0; root[F("value")] = 0;
root[F("id")] = control->id; root[F("id")] = control->GetId();
SendJsonDocToWebSocket(document, clientId); SendJsonDocToWebSocket(document, clientId);
} while (false); } while (false);
} }
void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId) void ESPUIClass::addGraphPoint(Control::ControlId_t id, int nValue, int clientId)
{ {
do // once do // once
{ {
@ -957,7 +840,7 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
root[F("type")] = (int)Control::Type::GraphPoint; root[F("type")] = (int)Control::Type::GraphPoint;
root[F("value")] = nValue; root[F("value")] = nValue;
root[F("id")] = control->id; root[F("id")] = control->GetId();
SendJsonDocToWebSocket(document, clientId); SendJsonDocToWebSocket(document, clientId);

View File

@ -49,6 +49,8 @@
#endif #endif
#include <ESPUIcontrolMgr.h>
#define FILE_WRITING "w" #define FILE_WRITING "w"
// Message Types (and control types) // Message Types (and control types)
@ -102,10 +104,6 @@ class ESPUIClass
public: public:
ESPUIClass() ESPUIClass()
{ {
#ifdef ESP32
ControlsSemaphore = xSemaphoreCreateMutex();
xSemaphoreGive(ControlsSemaphore);
#endif // def ESP32
} }
unsigned int jsonUpdateDocumentSize = 2000; unsigned int jsonUpdateDocumentSize = 2000;
#ifdef ESP8266 #ifdef ESP8266
@ -132,117 +130,116 @@ public:
void list(); // Lists LITTLEFS directory void list(); // Lists LITTLEFS directory
void writeFile(const char* path, const char* data); void writeFile(const char* path, const char* data);
uint16_t addControl(Control::Type type, const char* label); Control::ControlId_t addControl(Control::Type type, const char* label);
uint16_t addControl(Control::Type type, const char* label, const String& value); Control::ControlId_t addControl(Control::Type type, const char* label, const String& value);
uint16_t addControl(Control::Type type, const char* label, const String& value, Control::Color color); Control::ControlId_t addControl(Control::Type type, const char* label, const String& value, Control::Color color);
uint16_t addControl(Control::Type type, const char* label, const String& value, Control::Color color, uint16_t parentControl); Control::ControlId_t addControl(Control::Type type, const char* label, const String& value, Control::Color color, Control::ControlId_t parentControlId);
uint16_t addControl(Control::Type type, const char* label, const String& value, Control::Color color, uint16_t parentControl, std::function<void(Control*, int)> callback); Control::ControlId_t addControl(Control::Type type, const char* label, const String& value, Control::Color color, Control::ControlId_t parentControlId, std::function<void(Control*, int)> callback);
bool removeControl(uint16_t id, bool force_rebuild_ui = false); bool removeControl(Control::ControlId_t id, bool force_rebuild_ui = false);
// create Elements // create Elements
// Create Event Button // Create Event Button
uint16_t button(const char* label, std::function<void(Control*, int)> callback, Control::Color color, const String& value = ""); Control::ControlId_t button(const char* label, std::function<void(Control*, int)> callback, Control::Color color, const String& value = "");
uint16_t switcher(const char* label, std::function<void(Control*, int)> callback, Control::Color color, bool startState = false); // Create Toggle Button Control::ControlId_t switcher(const char* label, std::function<void(Control*, int)> callback, Control::Color color, bool startState = false); // Create Toggle Button
uint16_t pad(const char* label, std::function<void(Control*, int)> callback, Control::Color color); // Create Pad Control Control::ControlId_t pad(const char* label, std::function<void(Control*, int)> callback, Control::Color color); // Create Pad Control
uint16_t padWithCenter(const char* label, std::function<void(Control*, int)> callback, Control::Color color); // Create Pad Control with Centerbutton Control::ControlId_t padWithCenter(const char* label, std::function<void(Control*, int)> callback, Control::Color color); // Create Pad Control with Centerbutton
uint16_t slider(const char* label, std::function<void(Control*, int)> callback, Control::Color color, int value, int min = 0, int max = 100); // Create Slider Control Control::ControlId_t slider(const char* label, std::function<void(Control*, int)> callback, Control::Color color, int value, int min = 0, int max = 100); // Create Slider Control
uint16_t number(const char* label, std::function<void(Control*, int)> callback, Control::Color color, int value, int min = 0, int max = 100); // Create a Number Input Control Control::ControlId_t number(const char* label, std::function<void(Control*, int)> callback, Control::Color color, int value, int min = 0, int max = 100); // Create a Number Input Control
uint16_t text(const char* label, std::function<void(Control*, int)> callback, Control::Color color, const String& value = ""); // Create a Text Input Control Control::ControlId_t text(const char* label, std::function<void(Control*, int)> callback, Control::Color color, const String& value = ""); // Create a Text Input Control
// Output only // Output only
uint16_t label(const char* label, Control::Color color, Control::ControlId_t label(const char* label, Control::Color color,
const String& value = ""); // Create Label const String& value = ""); // Create Label
uint16_t graph(const char* label, Control::Color color); // Create Graph display Control::ControlId_t graph(const char* label, Control::Color color); // Create Graph display
uint16_t gauge(const char* label, Control::Color color, int value, int min = 0, Control::ControlId_t gauge(const char* label, Control::Color color, int value, int min = 0,
int max = 100); // Create Gauge display int max = 100); // Create Gauge display
uint16_t separator(const char* label); //Create separator Control::ControlId_t separator(const char* label); //Create separator
uint16_t fileDisplay(const char* label, Control::Color color, String filename); Control::ControlId_t fileDisplay(const char* label, Control::Color color, String filename);
// Input only // Input only
uint16_t accelerometer(const char* label, std::function<void(Control*, int)> callback, Control::Color color); Control::ControlId_t accelerometer(const char* label, std::function<void(Control*, int)> callback, Control::Color color);
// Update Elements // Update Elements
Control* getControl(uint16_t id); Control* getControl(Control::ControlId_t id);
Control* getControlNoLock(uint16_t id); Control* getControlNoLock(Control::ControlId_t id);
// Update Elements // Update Elements
void updateControlValue(uint16_t id, const String& value, int clientId = -1); void updateControlValue(Control::ControlId_t id, const String& value, int clientId = -1);
void updateControlValue(Control* control, const String& value, int clientId = -1); void updateControlValue(Control* control, const String& value, int clientId = -1);
void updateControlLabel(uint16_t control, const char * value, int clientId = -1); void updateControlLabel(Control::ControlId_t control, const char * value, int clientId = -1);
void updateControlLabel(Control* control, const char * value, int clientId = -1); void updateControlLabel(Control* control, const char * value, int clientId = -1);
void updateControl(uint16_t id, int clientId = -1); void updateControl(Control::ControlId_t id, int clientId = -1);
void updateControl(Control* control, int clientId = -1); void updateControl(Control* control, int clientId = -1);
void print(uint16_t id, const String& value); void print(Control::ControlId_t id, const String& value);
void updateLabel(uint16_t id, const String& value); void updateLabel(Control::ControlId_t id, const String& value);
void updateButton(uint16_t id, const String& value); void updateButton(Control::ControlId_t id, const String& value);
void updateSwitcher(uint16_t id, bool nValue, int clientId = -1); void updateSwitcher(Control::ControlId_t id, bool nValue, int clientId = -1);
void updateSlider(uint16_t id, int nValue, int clientId = -1); void updateSlider(Control::ControlId_t id, int nValue, int clientId = -1);
void updateNumber(uint16_t id, int nValue, int clientId = -1); void updateNumber(Control::ControlId_t id, int nValue, int clientId = -1);
void updateText(uint16_t id, const String& nValue, int clientId = -1); void updateText(Control::ControlId_t id, const String& nValue, int clientId = -1);
void updateSelect(uint16_t id, const String& nValue, int clientId = -1); void updateSelect(Control::ControlId_t id, const String& nValue, int clientId = -1);
void updateGauge(uint16_t id, int number, int clientId); void updateGauge(Control::ControlId_t id, int number, int clientId);
void updateTime(uint16_t id, int clientId = -1); void updateTime(Control::ControlId_t id, int clientId = -1);
void clearGraph(uint16_t id, int clientId = -1); void clearGraph(Control::ControlId_t id, int clientId = -1);
void addGraphPoint(uint16_t id, int nValue, int clientId = -1); void addGraphPoint(Control::ControlId_t id, int nValue, int clientId = -1);
void setPanelStyle(uint16_t id, const String& style, int clientId = -1); void setPanelStyle(Control::ControlId_t id, const String& style, int clientId = -1);
void setElementStyle(uint16_t id, const String& style, int clientId = -1); void setElementStyle(Control::ControlId_t id, const String& style, int clientId = -1);
void setInputType(uint16_t id, const String& type, int clientId = -1); void setInputType(Control::ControlId_t id, const String& type, int clientId = -1);
void setPanelWide(uint16_t id, bool wide); void setPanelWide(Control::ControlId_t id, bool wide);
void setVertical(uint16_t id, bool vert = true); void setVertical(Control::ControlId_t id, bool vert = true);
void setEnabled(uint16_t id, bool enabled = true, int clientId = -1); void setEnabled(Control::ControlId_t id, bool enabled = true, int clientId = -1);
void updateVisibility(uint16_t id, bool visibility, int clientId = -1); void updateVisibility(Control::ControlId_t id, bool visibility, int clientId = -1);
// Variables // Variables
const char* ui_title = "ESPUI"; // Store UI Title and Header Name const char* ui_title = "ESPUI"; // Store UI Title and Header Name
Control* controls = nullptr;
void jsonReload(); void jsonReload();
void jsonDom(uint16_t startidx, AsyncWebSocketClient* client = nullptr, bool Updating = false); void jsonDom(uint16_t startidx, AsyncWebSocketClient* client = nullptr, bool Updating = false);
Verbosity verbosity = Verbosity::Quiet; Verbosity verbosity = Verbosity::Quiet;
uint32_t GetNextControlChangeId(); uint32_t GetNextControlChangeId();
// emulate former extended callback API by using an intermediate lambda (no deprecation) // emulate former extended callback API by using an intermediate lambda (no deprecation)
uint16_t addControl(Control::Type type, const char* label, const String& value, Control::Color color, uint16_t parentControl, std::function<void(Control*, int, void*)> callback, void* userData) Control::ControlId_t addControl(Control::Type type, const char* label, const String& value, Control::Color color, Control::ControlId_t parentControl, std::function<void(Control*, int, void*)> callback, void* userData)
{ {
return addControl(type, label, value, color, parentControl, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }); return addControl(type, label, value, color, parentControl, [callback, userData](Control* sender, int type){ callback(sender, type, userData); });
} }
uint16_t button(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, const String& value, void* userData) Control::ControlId_t button(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, const String& value, void* userData)
{ {
return button(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color, value); return button(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color, value);
} }
uint16_t switcher(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, bool startState, void* userData) Control::ControlId_t switcher(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, bool startState, void* userData)
{ {
return switcher(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color, startState); return switcher(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color, startState);
} }
uint16_t pad(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, void* userData) Control::ControlId_t pad(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, void* userData)
{ {
return pad(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color); return pad(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color);
} }
uint16_t padWithCenter(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, void* userData) Control::ControlId_t padWithCenter(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, void* userData)
{ {
return padWithCenter(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color); return padWithCenter(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color);
} }
uint16_t slider(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, int value, int min, int max, void* userData) Control::ControlId_t slider(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, int value, int min, int max, void* userData)
{ {
return slider(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color, value, min, max); return slider(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color, value, min, max);
} }
uint16_t number(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, int value, int min, int max, void* userData) Control::ControlId_t number(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, int value, int min, int max, void* userData)
{ {
return number(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color, value, min, max); return number(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color, value, min, max);
} }
uint16_t text(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, const String& value, void* userData) Control::ControlId_t text(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, const String& value, void* userData)
{ {
return text(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); } , color, value); return text(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); } , color, value);
} }
uint16_t accelerometer(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, void* userData) Control::ControlId_t accelerometer(const char* label, std::function<void(Control*, int, void*)> callback, Control::Color color, void* userData)
{ {
return accelerometer(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color); return accelerometer(label, [callback, userData](Control* sender, int type){ callback(sender, type, userData); }, color);
} }
@ -264,21 +261,14 @@ protected:
friend class ESPUIclient; friend class ESPUIclient;
friend class ESPUIcontrol; friend class ESPUIcontrol;
#ifdef ESP32
SemaphoreHandle_t ControlsSemaphore = NULL;
#endif // def ESP32
void RemoveToBeDeletedControls();
AsyncWebServer* server; AsyncWebServer* server;
AsyncWebSocket* ws; AsyncWebSocket* ws;
const char* basicAuthUsername = nullptr; const char* basicAuthUsername = nullptr;
const char* basicAuthPassword = nullptr; const char* basicAuthPassword = nullptr;
bool basicAuth = true; bool basicAuth = true;
uint16_t controlCount = 0;
uint16_t addControl(Control::Type type, const char* label, const String& value, Control::Color color, uint16_t parentControl, Control* control); Control::ControlId_t addControl(Control::Type type, const char* label, const String& value, Control::Color color, Control::ControlId_t parentControl, Control* control);
#define ClientUpdateType_t ESPUIclient::ClientUpdateType_t #define ClientUpdateType_t ESPUIclient::ClientUpdateType_t
void NotifyClients(ClientUpdateType_t newState); void NotifyClients(ClientUpdateType_t newState);