1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-12-23 05:50:49 +00:00

First part of extending callback to include a user parm.

Converted addControl to no longer use C Style function definitions. Now using C++ function overload to support multiple invocation modes.
This commit is contained in:
Martin Mueller 2022-06-11 00:42:25 -04:00
parent 9cb962122c
commit 02e847a31e
2 changed files with 86 additions and 31 deletions

View File

@ -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)
{
@ -1443,4 +1470,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;

View File

@ -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,16 @@ 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 +180,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,9 +252,13 @@ 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