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

Controls Label in flash and other fixes

This commit is contained in:
Nikola Kirov 2024-01-06 14:10:38 +02:00
parent e5f53d5118
commit 9184ae0e8b
4 changed files with 514 additions and 414 deletions

View File

@ -650,7 +650,16 @@ uint16_t ESPUIClass::addControl(
Control * ctrl = 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) if (auto_update_values && ctrl)
ctrl->auto_update_value = true; ctrl->auto_update_value = true;
return addControl(type, label, value, color, parentControl, ctrl); return addControl(ctrl);
}
uint16_t ESPUIClass::addControl(
ControlType type, const __FlashStringHelper* label, const String& value, ControlColor color, uint16_t parentControl)
{
Control* ctrl = new Control(type, label, nullptr, value, color, true, parentControl);
if (auto_update_values && ctrl)
ctrl->auto_update_value = true;
return addControl(ctrl);
} }
uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t ESPUIClass::addControl(ControlType type, const char* label, const String& value, ControlColor color,
@ -662,8 +671,16 @@ uint16_t ESPUIClass::addControl(ControlType type, const char* label, const Strin
return id; return id;
} }
uint16_t ESPUIClass::addControl( uint16_t ESPUIClass::addControl(ControlType type, const __FlashStringHelper* label, const String& value, ControlColor color,
ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl, Control* control) uint16_t parentControl, std::function<void(Control*, int)> callback)
{
uint16_t id = addControl(type, label, value, color, parentControl);
// set the original style callback
getControl(id)->callback = callback;
return id;
}
uint16_t ESPUIClass::addControl(Control* control)
{ {
#ifdef ESP32 #ifdef ESP32
xSemaphoreTake(ControlsSemaphore, portMAX_DELAY); xSemaphoreTake(ControlsSemaphore, portMAX_DELAY);
@ -788,6 +805,10 @@ uint16_t ESPUIClass::button(const char* label, std::function<void(Control*, int)
return addControl(ControlType::Button, label, value, color, Control::noParent, callback); return addControl(ControlType::Button, label, value, color, Control::noParent, callback);
} }
uint16_t ESPUIClass::button(const __FlashStringHelper* label, const __FlashStringHelper* text, std::function<void(Control*, int)> callback, uint16_t parentControl, ControlColor color){
return addControl(ControlType::Button, label, text, color, parentControl, callback);
}
uint16_t ESPUIClass::switcher(const char* label, std::function<void(Control*, int)> callback, ControlColor color, bool startState) uint16_t ESPUIClass::switcher(const char* label, std::function<void(Control*, int)> callback, ControlColor color, bool startState)
{ {
return addControl(ControlType::Switcher, label, startState ? "1" : "0", color, Control::noParent, callback); return addControl(ControlType::Switcher, label, startState ? "1" : "0", color, Control::noParent, callback);
@ -1015,7 +1036,25 @@ void ESPUIClass::updateControlLabel(Control* control, const char* value, int cli
#endif #endif
return; return;
} }
control->label = value; control->label_r = value;
control->lablel_is_in_flash = 0;
updateControl(control, clientId);
}
void ESPUIClass::updateControlLabel(Control* control, const __FlashStringHelper* value, int clientId)
{
if (!control)
{
#if defined(DEBUG_ESPUI)
if (verbosity)
{
ESPU_DBGf_P(PSTR("Error: updateControlLabel Control: There is no control with the requested ID \n"));
}
#endif
return;
}
control->label_f = value;
control->lablel_is_in_flash = 1;
updateControl(control, clientId); updateControl(control, clientId);
} }

View File

@ -132,13 +132,18 @@ public:
uint16_t addControl(ControlType type, const char* label, const String& value); 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 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);
uint16_t addControl(ControlType type, const __FlashStringHelper* 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, std::function<void(Control*, int)> callback); uint16_t addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl, std::function<void(Control*, int)> callback);
uint16_t addControl(ControlType type, const __FlashStringHelper * label, const String& value, ControlColor color, uint16_t parentControl, std::function<void(Control*, int)> callback);
bool removeControl(uint16_t id, bool force_rebuild_ui = false); bool removeControl(uint16_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, ControlColor color, const String& value = ""); uint16_t button(const char* label, std::function<void(Control*, int)> callback, ControlColor color, const String& value = "");
uint16_t button(const __FlashStringHelper* label, const __FlashStringHelper* value,
std::function<void(Control*, int)> callback, uint16_t parentControl = Control::noParent, ControlColor color = ControlColor::Dark);
uint16_t switcher(const char* label, std::function<void(Control*, int)> callback, ControlColor color, bool startState = false); // Create Toggle Button uint16_t switcher(const char* label, std::function<void(Control*, int)> callback, ControlColor color, bool startState = false); // Create Toggle Button
uint16_t pad(const char* label, std::function<void(Control*, int)> callback, ControlColor color); // Create Pad Control uint16_t pad(const char* label, std::function<void(Control*, int)> callback, ControlColor color); // Create Pad Control
uint16_t padWithCenter(const char* label, std::function<void(Control*, int)> callback, ControlColor color); // Create Pad Control with Centerbutton uint16_t padWithCenter(const char* label, std::function<void(Control*, int)> callback, ControlColor color); // Create Pad Control with Centerbutton
@ -168,6 +173,7 @@ public:
void updateControlLabel(uint16_t control, const char * value, int clientId = -1); void updateControlLabel(uint16_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 updateControlLabel(Control* control, const __FlashStringHelper* value, int clientId = -1);
void updateControl(uint16_t id, int clientId = -1); void updateControl(uint16_t id, int clientId = -1);
void updateControl(Control* control, int clientId = -1); void updateControl(Control* control, int clientId = -1);
@ -285,7 +291,7 @@ protected:
bool basicAuth = true; bool basicAuth = true;
uint16_t controlCount = 0; uint16_t controlCount = 0;
uint16_t addControl(ControlType type, const char* label, const String& value, ControlColor color, uint16_t parentControl, Control* control); uint16_t addControl(Control* control);
#define ClientUpdateType_t ESPUIclient::ClientUpdateType_t #define ClientUpdateType_t ESPUIclient::ClientUpdateType_t
void NotifyClients(ClientUpdateType_t newState); void NotifyClients(ClientUpdateType_t newState);

View File

@ -5,35 +5,52 @@ 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, Control::Control(ControlType type, const char* label, std::function<void(Control*, int)> callback, const String& value,
ControlColor color, bool visible, uint16_t parentControl) ControlColor color, bool visible, uint16_t parentControl)
: type(type), : label_r(label),
label(label),
callback(callback), callback(callback),
next(nullptr),
value(value), value(value),
type(type),
color(color), color(color),
visible(visible),
wide(false),
vertical(false),
enabled(true),
auto_update_value(false),
parentControl(parentControl), parentControl(parentControl),
next(nullptr) options(CTRL_OPT_ENABLED),
ControlChangeID(1)
{ {
this->visible = visible;
id = ++idCounter;
}
Control::Control(ControlType type, const __FlashStringHelper* label, std::function<void(Control*, int)> callback,
const String& value, ControlColor color, bool visible, uint16_t parentControl)
: label_f(label),
callback(callback),
next(nullptr),
value(value),
type(type),
color(color),
parentControl(parentControl),
options(CTRL_OPT_ENABLED | CTRL_OPT_LABEL_IN_FLASH),
ControlChangeID(1)
{
this->visible = visible;
id = ++idCounter; id = ++idCounter;
ControlChangeID = 1;
} }
Control::Control(const Control& Control) Control::Control(const Control& Control)
: type(Control.type), : label_r(Control.label_r),
id(Control.id),
label(Control.label),
callback(Control.callback), callback(Control.callback),
value(Control.value),
color(Control.color),
visible(Control.visible),
parentControl(Control.parentControl),
next(Control.next), next(Control.next),
value(Control.value),
type(Control.type),
color(Control.color),
id(Control.id),
parentControl(Control.parentControl),
visible(Control.visible),
ControlChangeID(Control.ControlChangeID) ControlChangeID(Control.ControlChangeID)
{ } {
options = Control.options;
if (lablel_is_in_flash)
label_f = Control.label_f;
}
void Control::SendCallback(int type) void Control::SendCallback(int type)
{ {
@ -97,7 +114,10 @@ void Control::MarshalControl(JsonObject & _item, bool refresh, uint32_t Starting
item[F("type")] = uint32_t(TempType); item[F("type")] = uint32_t(TempType);
} }
item[F("label")] = label; if (lablel_is_in_flash)
item[F("label")] = label_f;
else
item[F("label")] = label_r;
item[F ("value")] = (ControlType::Password == type) ? F ("--------") : value.substring(StartingOffset, length + StartingOffset); item[F ("value")] = (ControlType::Password == type) ? F ("--------") : value.substring(StartingOffset, length + StartingOffset);
item[F("visible")] = visible; item[F("visible")] = visible;
item[F("color")] = (int)color; item[F("color")] = (int)color;

View File

@ -49,27 +49,54 @@ enum ControlColor : uint8_t
None = 0xFF None = 0xFF
}; };
#define CTRL_OPT_ENABLED 0x01
#define CTRL_OPT_VISIBLE 0x02
#define CTRL_OPT_LABEL_IN_FLASH 0x04
#define CTRL_OPT_AUTO_UPDATE_VALUE 0x08
#define CTRL_OPT_WIDE 0x10
#define CTRL_OPT_VERTICAL 0x20
class Control class Control
{ {
public: public:
ControlType type; static constexpr uint16_t noParent = 0xffff;
uint16_t id; // just mirroring the id here for practical reasons
const char* label; // Pointers
union
{
const char* label_r;
const __FlashStringHelper* label_f;
};
std::function<void(Control*, int)> callback; std::function<void(Control*, int)> callback;
Control* next;
// Strings
String value; String value;
ControlColor color;
bool visible;
bool wide;
bool vertical;
bool enabled;
bool auto_update_value;
uint16_t parentControl;
String panelStyle; String panelStyle;
String elementStyle; String elementStyle;
String inputType; String inputType;
Control* next;
static constexpr uint16_t noParent = 0xffff; //enums
ControlType type;
ControlColor color;
// uint16_t
uint16_t id; // just mirroring the id here for practical reasons
uint16_t parentControl;
union
{
struct
{
uint16_t enabled : 1 ;
uint16_t visible : 1;
uint16_t lablel_is_in_flash : 1;
uint16_t auto_update_value : 1;
uint16_t wide : 1;
uint16_t vertical : 1;
};
uint16_t options;
};
Control(ControlType type, Control(ControlType type,
const char* label, const char* label,
@ -79,6 +106,14 @@ public:
bool visible, bool visible,
uint16_t parentControl); uint16_t parentControl);
Control(ControlType type,
const __FlashStringHelper* label,
std::function<void(Control*, int)> callback,
const String& value,
ControlColor color,
bool visible,
uint16_t parentControl);
Control(const Control& Control); Control(const Control& Control);
void SendCallback(int type); void SendCallback(int type);
@ -93,7 +128,7 @@ public:
private: private:
bool _ToBeDeleted = false; bool _ToBeDeleted = false;
uint32_t ControlChangeID = 0; uint32_t ControlChangeID;
}; };
#define UI_TITLE ControlType::Title #define UI_TITLE ControlType::Title