1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-24 17:30:55 +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; return;
} }
if (c->callback == nullptr) if (false == c->HasCallback())
{ {
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
Serial.print(F("No callback found for ID ")); Serial.print(F("No callback found for ID "));
@ -490,97 +490,97 @@ void onWsEvent(
if (msg.startsWith(F("bdown:"))) if (msg.startsWith(F("bdown:")))
{ {
c->callback(c, B_DOWN); c->SendCallback(B_DOWN);
} }
else if (msg.startsWith(F("bup:"))) else if (msg.startsWith(F("bup:")))
{ {
c->callback(c, B_UP); c->SendCallback(B_UP);
} }
else if (msg.startsWith(F("pfdown:"))) else if (msg.startsWith(F("pfdown:")))
{ {
c->callback(c, P_FOR_DOWN); c->SendCallback(P_FOR_DOWN);
} }
else if (msg.startsWith(F("pfup:"))) else if (msg.startsWith(F("pfup:")))
{ {
c->callback(c, P_FOR_UP); c->SendCallback(P_FOR_UP);
} }
else if (msg.startsWith(F("pldown:"))) else if (msg.startsWith(F("pldown:")))
{ {
c->callback(c, P_LEFT_DOWN); c->SendCallback(P_LEFT_DOWN);
} }
else if (msg.startsWith(F("plup:"))) else if (msg.startsWith(F("plup:")))
{ {
c->callback(c, P_LEFT_UP); c->SendCallback(P_LEFT_UP);
} }
else if (msg.startsWith(F("prdown:"))) else if (msg.startsWith(F("prdown:")))
{ {
c->callback(c, P_RIGHT_DOWN); c->SendCallback(P_RIGHT_DOWN);
} }
else if (msg.startsWith(F("prup:"))) else if (msg.startsWith(F("prup:")))
{ {
c->callback(c, P_RIGHT_UP); c->SendCallback(P_RIGHT_UP);
} }
else if (msg.startsWith(F("pbdown:"))) else if (msg.startsWith(F("pbdown:")))
{ {
c->callback(c, P_BACK_DOWN); c->SendCallback(P_BACK_DOWN);
} }
else if (msg.startsWith(F("pbup:"))) else if (msg.startsWith(F("pbup:")))
{ {
c->callback(c, P_BACK_UP); c->SendCallback(P_BACK_UP);
} }
else if (msg.startsWith(F("pcdown:"))) else if (msg.startsWith(F("pcdown:")))
{ {
c->callback(c, P_CENTER_DOWN); c->SendCallback(P_CENTER_DOWN);
} }
else if (msg.startsWith(F("pcup:"))) else if (msg.startsWith(F("pcup:")))
{ {
c->callback(c, P_CENTER_UP); c->SendCallback(P_CENTER_UP);
} }
else if (msg.startsWith(F("sactive:"))) else if (msg.startsWith(F("sactive:")))
{ {
c->value = "1"; c->value = "1";
ESPUI.updateControl(c, client->id()); ESPUI.updateControl(c, client->id());
c->callback(c, S_ACTIVE); c->SendCallback(S_ACTIVE);
} }
else if (msg.startsWith(F("sinactive:"))) else if (msg.startsWith(F("sinactive:")))
{ {
c->value = "0"; c->value = "0";
ESPUI.updateControl(c, client->id()); ESPUI.updateControl(c, client->id());
c->callback(c, S_INACTIVE); c->SendCallback(S_INACTIVE);
} }
else if (msg.startsWith(F("slvalue:"))) else if (msg.startsWith(F("slvalue:")))
{ {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c, client->id()); ESPUI.updateControl(c, client->id());
c->callback(c, SL_VALUE); c->SendCallback(SL_VALUE);
} }
else if (msg.startsWith(F("nvalue:"))) else if (msg.startsWith(F("nvalue:")))
{ {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c, client->id()); ESPUI.updateControl(c, client->id());
c->callback(c, N_VALUE); c->SendCallback(N_VALUE);
} }
else if (msg.startsWith(F("tvalue:"))) else if (msg.startsWith(F("tvalue:")))
{ {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c, client->id()); ESPUI.updateControl(c, client->id());
c->callback(c, T_VALUE); c->SendCallback(T_VALUE);
} }
else if (msg.startsWith("tabvalue:")) else if (msg.startsWith("tabvalue:"))
{ {
c->callback(c, client->id()); c->SendCallback(client->id());
} }
else if (msg.startsWith(F("svalue:"))) else if (msg.startsWith(F("svalue:")))
{ {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c, client->id()); ESPUI.updateControl(c, client->id());
c->callback(c, S_VALUE); c->SendCallback(S_VALUE);
} }
else if (msg.startsWith(F("time:"))) else if (msg.startsWith(F("time:")))
{ {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c, client->id()); ESPUI.updateControl(c, client->id());
c->callback(c, TM_VALUE); c->SendCallback(TM_VALUE);
} }
else else
{ {
@ -600,10 +600,37 @@ void onWsEvent(
} }
} }
uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t ESPUIClass::addControl(ControlType type, const char* label)
uint16_t parentControl, void (*callback)(Control*, int))
{ {
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) if (this->controls == nullptr)
{ {
@ -1443,4 +1470,17 @@ void ESPUIClass::setVerbosity(Verbosity v)
this->verbosity = v; this->verbosity = v;
} }
void Control::SendCallback(int type)
{
if(callback)
{
callback(this, type);
}
if (extendedCallback)
{
extendedCallback(this, type, user);
}
}
ESPUIClass ESPUI; ESPUIClass ESPUI;

View File

@ -137,6 +137,8 @@ public:
uint16_t id; // just mirroring the id here for practical reasons uint16_t id; // just mirroring the id here for practical reasons
const char* label; const char* label;
void (*callback)(Control*, int); void (*callback)(Control*, int);
void (*extendedCallback)(Control*, int, void*);
void* user;
String value; String value;
ControlColor color; ControlColor color;
bool visible; bool visible;
@ -151,11 +153,16 @@ public:
static constexpr uint16_t noParent = 0xffff; static constexpr uint16_t noParent = 0xffff;
Control(ControlType type, const char* label, void (*callback)(Control*, int), const String& value, Control(
ControlColor color, bool visible = true, uint16_t parentControl = Control::noParent) ControlType type,
const char* label,
void (*callback)(Control*, int, void*),
void* UserData, const String& value, ControlColor color, bool visible, uint16_t parentControl)
: type(type), : type(type),
label(label), label(label),
callback(callback), callback(nullptr),
extendedCallback(callback),
user(UserData),
value(value), value(value),
color(color), color(color),
visible(visible), visible(visible),
@ -173,12 +180,16 @@ public:
id(control.id), id(control.id),
label(control.label), label(control.label),
callback(control.callback), callback(control.callback),
extendedCallback(control.extendedCallback),
user(control.user),
value(control.value), value(control.value),
color(control.color), color(control.color),
visible(control.visible), visible(control.visible),
parentControl(control.parentControl), parentControl(control.parentControl),
next(control.next) next(control.next)
{ } { }
void SendCallback(int type);
bool HasCallback() { return ((nullptr != callback) || (nullptr != extendedCallback)); }
private: private:
static uint16_t idCounter; static uint16_t idCounter;
@ -241,9 +252,13 @@ public:
// stuff into LITTLEFS // stuff into LITTLEFS
void list(); // Lists LITTLEFS directory void list(); // Lists LITTLEFS directory
uint16_t addControl(ControlType type, const char* label, const String& value = String(""), uint16_t addControl(ControlType type, const char* label);
ControlColor color = ControlColor::Turquoise, uint16_t parentControl = Control::noParent, uint16_t addControl(ControlType type, const char* label, const String& value);
void (*callback)(Control*, int) = nullptr); 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); bool removeControl(uint16_t id, bool force_reload_ui = false);
// create Elements // create Elements