mirror of
https://github.com/s00500/ESPUI.git
synced 2025-06-14 02:30:41 +00:00
Merge branch 's00500:master' into master
This commit is contained in:
187
src/ESPUI.cpp
187
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)
|
||||
{
|
||||
@ -628,38 +655,30 @@ uint16_t ESPUIClass::addControl(ControlType type, const char* label, const Strin
|
||||
|
||||
bool ESPUIClass::removeControl(uint16_t id, bool force_reload_ui)
|
||||
{
|
||||
if (nullptr == this->controls)
|
||||
return false;
|
||||
Control* PreviousControl = nullptr;
|
||||
Control* CurrentControl = this->controls;
|
||||
|
||||
Control* it = this->controls;
|
||||
|
||||
if (id == it->id)
|
||||
while(nullptr != CurrentControl)
|
||||
{
|
||||
this->controls = it->next;
|
||||
delete it;
|
||||
this->controlCount--;
|
||||
if (force_reload_ui)
|
||||
if (id == CurrentControl->id)
|
||||
{
|
||||
jsonReload();
|
||||
break;
|
||||
}
|
||||
PreviousControl = CurrentControl;
|
||||
CurrentControl = CurrentControl->next;
|
||||
}
|
||||
|
||||
if (nullptr != CurrentControl)
|
||||
{
|
||||
if(nullptr == PreviousControl)
|
||||
{
|
||||
this->controls = CurrentControl->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
jsonDom(0);
|
||||
PreviousControl->next = CurrentControl->next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Control* it_next = it->next;
|
||||
while (nullptr != it_next && id != it_next->id)
|
||||
{
|
||||
it = it_next;
|
||||
it_next = it_next->next;
|
||||
}
|
||||
|
||||
if (nullptr != it_next)
|
||||
{
|
||||
it->next = it_next->next;
|
||||
delete it_next;
|
||||
delete CurrentControl;
|
||||
this->controlCount--;
|
||||
if (force_reload_ui)
|
||||
{
|
||||
@ -685,10 +704,16 @@ uint16_t ESPUIClass::graph(const char* label, ControlColor color)
|
||||
return addControl(ControlType::Graph, label, "", color);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::slider(
|
||||
const char* label, void (*callback)(Control*, int), ControlColor color, int value, int min, int max)
|
||||
uint16_t ESPUIClass::slider(const char* label, void (*callback)(Control*, int), ControlColor color, int value, int min, int max)
|
||||
{
|
||||
uint16_t sliderId = addControl(ControlType::Slider, label, String(value), color, Control::noParent, callback);
|
||||
uint16_t id = slider(label, nullptr, color, value, min, max, nullptr);
|
||||
getControl(id)->callback = callback;
|
||||
return id;
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::slider(const char* label, void (*callback)(Control*, int, void*), ControlColor color, int value, int min, int max, void* userData)
|
||||
{
|
||||
uint16_t sliderId = addControl(ControlType::Slider, label, String(value), color, Control::noParent, callback, userData);
|
||||
addControl(ControlType::Min, label, String(min), ControlColor::None, sliderId);
|
||||
addControl(ControlType::Max, label, String(max), ControlColor::None, sliderId);
|
||||
|
||||
@ -700,22 +725,42 @@ uint16_t ESPUIClass::button(const char* label, void (*callback)(Control*, int),
|
||||
return addControl(ControlType::Button, label, value, color, Control::noParent, callback);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::button(const char* label, void (*callback)(Control*, int, void*), ControlColor color, const String& value, void* UserData)
|
||||
{
|
||||
return addControl(ControlType::Button, label, value, color, Control::noParent, callback, UserData);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::switcher(const char* label, void (*callback)(Control*, int), ControlColor color, bool startState)
|
||||
{
|
||||
return addControl(ControlType::Switcher, label, startState ? "1" : "0", color, Control::noParent, callback);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::switcher(const char* label, void (*callback)(Control*, int, void*), ControlColor color, bool startState, void* UserData)
|
||||
{
|
||||
return addControl(ControlType::Switcher, label, startState ? "1" : "0", color, Control::noParent, callback, UserData);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::pad(const char* label, void (*callback)(Control*, int), ControlColor color)
|
||||
{
|
||||
return addControl(ControlType::Pad, label, "", color, Control::noParent, callback);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::pad(const char* label, void (*callback)(Control*, int, void*), ControlColor color, void* UserData)
|
||||
{
|
||||
return addControl(ControlType::Pad, label, "", color, Control::noParent, callback, UserData);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::padWithCenter(const char* label, void (*callback)(Control*, int), ControlColor color)
|
||||
{
|
||||
return addControl(ControlType::PadWithCenter, label, "", color, Control::noParent, callback);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::number(
|
||||
const char* label, void (*callback)(Control*, int), ControlColor color, int number, int min, int max)
|
||||
uint16_t ESPUIClass::padWithCenter(const char* label, void (*callback)(Control*, int, void*), ControlColor color, void* UserData)
|
||||
{
|
||||
return addControl(ControlType::PadWithCenter, label, "", color, Control::noParent, callback, UserData);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::number(const char* label, void (*callback)(Control*, int), ControlColor color, int number, int min, int max)
|
||||
{
|
||||
uint16_t numberId = addControl(ControlType::Number, label, String(number), color, Control::noParent, callback);
|
||||
addControl(ControlType::Min, label, String(min), ControlColor::None, numberId);
|
||||
@ -723,6 +768,14 @@ uint16_t ESPUIClass::number(
|
||||
return numberId;
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::number(const char* label, void (*callback)(Control*, int, void*), ControlColor color, int number, int min, int max, void* UserData)
|
||||
{
|
||||
uint16_t numberId = addControl(ControlType::Number, label, String(number), color, Control::noParent, callback, UserData);
|
||||
addControl(ControlType::Min, label, String(min), ControlColor::None, numberId);
|
||||
addControl(ControlType::Max, label, String(max), ControlColor::None, numberId);
|
||||
return numberId;
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::gauge(const char* label, ControlColor color, int number, int min, int max)
|
||||
{
|
||||
uint16_t numberId = addControl(ControlType::Gauge, label, String(number), color, Control::noParent);
|
||||
@ -740,11 +793,21 @@ uint16_t ESPUIClass::accelerometer(const char* label, void (*callback)(Control*,
|
||||
return addControl(ControlType::Accel, label, "", color, Control::noParent, callback);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::accelerometer(const char* label, void (*callback)(Control*, int, void*), ControlColor color, void* UserData)
|
||||
{
|
||||
return addControl(ControlType::Accel, label, "", color, Control::noParent, callback, UserData);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::text(const char* label, void (*callback)(Control*, int), ControlColor color, const String& value)
|
||||
{
|
||||
return addControl(ControlType::Text, label, value, color, Control::noParent, callback);
|
||||
}
|
||||
|
||||
uint16_t ESPUIClass::text(const char* label, void (*callback)(Control*, int, void*), ControlColor color, const String& value, void* UserData)
|
||||
{
|
||||
return addControl(ControlType::Text, label, value, color, Control::noParent, callback, UserData);
|
||||
}
|
||||
|
||||
Control* ESPUIClass::getControl(uint16_t id)
|
||||
{
|
||||
Control* control = this->controls;
|
||||
@ -878,7 +941,6 @@ void ESPUIClass::setEnabled(uint16_t id, bool enabled, int clientId) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ESPUIClass::setVertical(uint16_t id, bool vert) {
|
||||
Control* control = getControl(id);
|
||||
if (control)
|
||||
@ -1451,4 +1513,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;
|
||||
|
59
src/ESPUI.h
59
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,13 @@ 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 +177,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,27 +249,37 @@ 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
|
||||
uint16_t button(const char* label, void (*callback)(Control*, int), ControlColor color,
|
||||
const String& value = ""); // Create Event Button
|
||||
uint16_t switcher(const char* label, void (*callback)(Control*, int), ControlColor color,
|
||||
bool startState = false); // Create Toggle Button
|
||||
uint16_t pad(const char* label, void (*callback)(Control*, int),
|
||||
ControlColor color); // Create Pad Control
|
||||
uint16_t padWithCenter(const char* label, void (*callback)(Control*, int),
|
||||
ControlColor color); // Create Pad Control with Centerbutton
|
||||
// Create Event Button
|
||||
uint16_t button(const char* label, void (*callback)(Control*, int), ControlColor color, const String& value = "");
|
||||
uint16_t button(const char* label, void (*callback)(Control*, int, void*), ControlColor color, const String& value, void* UserData);
|
||||
|
||||
uint16_t slider(const char* label, void (*callback)(Control*, int), ControlColor color, int value, int min = 0,
|
||||
int max = 100); // Create Slider Control
|
||||
uint16_t number(const char* label, void (*callback)(Control*, int), ControlColor color, int value, int min = 0,
|
||||
int max = 100); // Create a Number Input Control
|
||||
uint16_t text(const char* label, void (*callback)(Control*, int), ControlColor color,
|
||||
const String& value = ""); // Create a Text Input Control
|
||||
uint16_t switcher(const char* label, void (*callback)(Control*, int), ControlColor color, bool startState = false); // Create Toggle Button
|
||||
uint16_t switcher(const char* label, void (*callback)(Control*, int, void*), ControlColor color, bool startState, void* UserData); // Create Toggle Button
|
||||
|
||||
uint16_t pad(const char* label, void (*callback)(Control*, int), ControlColor color); // Create Pad Control
|
||||
uint16_t pad(const char* label, void (*callback)(Control*, int, void*), ControlColor color, void* UserData); // Create Pad Control
|
||||
|
||||
uint16_t padWithCenter(const char* label, void (*callback)(Control*, int), ControlColor color); // Create Pad Control with Centerbutton
|
||||
uint16_t padWithCenter(const char* label, void (*callback)(Control*, int, void*), ControlColor color, void* UserData); // Create Pad Control with Centerbutton
|
||||
|
||||
uint16_t slider(const char* label, void (*callback)(Control*, int), ControlColor color, int value, int min = 0, int max = 100); // Create Slider Control
|
||||
uint16_t slider(const char* label, void (*callback)(Control*, int, void*), ControlColor color, int value, int min, int max, void* UserData); // Create Slider Control
|
||||
|
||||
uint16_t number(const char* label, void (*callback)(Control*, int), ControlColor color, int value, int min = 0, int max = 100); // Create a Number Input Control
|
||||
uint16_t number(const char* label, void (*callback)(Control*, int, void*), ControlColor color, int value, int min, int max, void* UserData); // Create a Number Input Control
|
||||
|
||||
uint16_t text(const char* label, void (*callback)(Control*, int), ControlColor color, const String& value = ""); // Create a Text Input Control
|
||||
uint16_t text(const char* label, void (*callback)(Control*, int, void*), ControlColor color, const String& value, void* UserData); // Create a Text Input Control
|
||||
|
||||
// Output only
|
||||
uint16_t label(const char* label, ControlColor color,
|
||||
@ -273,6 +291,7 @@ public:
|
||||
|
||||
// Input only
|
||||
uint16_t accelerometer(const char* label, void (*callback)(Control*, int), ControlColor color);
|
||||
uint16_t accelerometer(const char* label, void (*callback)(Control*, int, void*), ControlColor color, void* UserData);
|
||||
|
||||
// Update Elements
|
||||
|
||||
|
Reference in New Issue
Block a user