Use DEBUG_ESPUI to en/disable debug code from being compiled

Include clang-format file for formatting code and format code
This commit is contained in:
Moritz Wirger 2020-09-28 10:40:31 +02:00
parent db4164f621
commit 1e5ee117c5
3 changed files with 1216 additions and 999 deletions

58
.clang-format Executable file
View File

@ -0,0 +1,58 @@
---
# Based on Webkit style
BasedOnStyle: Webkit
IndentWidth: 4
ColumnLimit: 120
---
Language: Cpp
Standard: Cpp11
# Pointers aligned to the left
DerivePointerAlignment: false
PointerAlignment: Left
AccessModifierOffset: -4
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakTemplateDeclarations: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakConstructorInitializers: BeforeColon
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
Cpp11BracedListStyle: true
FixNamespaceComments: true
IncludeBlocks: Regroup
IncludeCategories:
# C++ standard headers (no .h)
- Regex: '<[[:alnum:]_-]+>'
Priority: 1
# Extenal libraries (with .h)
- Regex: '<[[:alnum:]_./-]+>'
Priority: 2
# Headers from same folder
- Regex: '"[[:alnum:]_.-]+"'
Priority: 3
# Headers from other folders
- Regex: '"[[:alnum:]_/.-]+"'
Priority: 4
IndentCaseLabels: false
NamespaceIndentation: All
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterTemplateKeyword: true
SpacesInAngles: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
UseTab: Never

View File

@ -1,48 +1,52 @@
#include "ESPUI.h"
#include "dataIndexHTML.h"
#include "dataNormalizeCSS.h"
#include "dataStyleCSS.h"
#include "dataControlsJS.h"
#include "dataGraphJS.h"
#include "dataSliderJS.h"
#include "dataTabbedcontentJS.h"
#include "dataZeptoJS.h"
#include <functional>
#include <ESPAsyncWebServer.h>
#include <functional>
#include "ESPUI.h"
#include "dataControlsJS.h"
#include "dataGraphJS.h"
#include "dataIndexHTML.h"
#include "dataNormalizeCSS.h"
#include "dataSliderJS.h"
#include "dataStyleCSS.h"
#include "dataTabbedcontentJS.h"
#include "dataZeptoJS.h"
uint16_t Control::idCounter = 0;
// ################# Spiffs functions
#if defined(ESP32)
void listDir(const char *dirname, uint8_t levels)
void listDir(const char* dirname, uint8_t levels)
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.printf_P(F("Listing directory: %s\n"), dirname);
}
#endif
File root = LittleFS.open(dirname);
if (!root)
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("Failed to open directory"));
}
#endif
return;
}
if (!root.isDirectory())
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("Not a directory"));
}
#endif
return;
}
@ -53,11 +57,13 @@ void listDir(const char *dirname, uint8_t levels)
{
if (file.isDirectory())
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F(" DIR : "));
Serial.println(file.name());
}
#endif
if (levels)
{
@ -66,6 +72,7 @@ void listDir(const char *dirname, uint8_t levels)
}
else
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F(" FILE: "));
@ -73,6 +80,7 @@ void listDir(const char *dirname, uint8_t levels)
Serial.print(F(" SIZE: "));
Serial.println(file.size());
}
#endif
}
file = root.openNextFile();
@ -80,7 +88,7 @@ void listDir(const char *dirname, uint8_t levels)
}
#else
void listDir(const char *dirname, uint8_t levels)
void listDir(const char* dirname, uint8_t levels)
{
// ignoring levels for esp8266
Serial.printf_P(PSTR("Listing directory: %s\n"), dirname);
@ -123,59 +131,73 @@ void ESPUIClass::list()
#endif
}
void deleteFile(const char *path)
void deleteFile(const char* path)
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(LittleFS.exists(path));
}
#endif
if (!LittleFS.exists(path))
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.printf_P(PSTR("File: %s does not exist, not deleting\n"), path);
}
#endif
return;
}
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.printf_P(PSTR("Deleting file: %s\n"), path);
}
#endif
if (LittleFS.remove(path))
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("File deleted"));
}
#endif
}
else
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("Delete failed"));
}
#endif
}
}
void writeFile(const char *path, const char *data)
void writeFile(const char* path, const char* data)
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.printf_P(PSTR("Writing file: %s\n"), path);
}
#endif
File file = LittleFS.open(path, FILE_WRITE);
if (!file)
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("Failed to open file for writing"));
}
#endif
return;
}
@ -184,34 +206,42 @@ void writeFile(const char *path, const char *data)
if (file.print(data))
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("File written"));
}
#endif
}
else
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("Write failed"));
}
#endif
}
#else
if (file.print(FPSTR(data)))
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("File written"));
}
#endif
}
else
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("Write failed"));
}
#endif
}
#endif
@ -224,38 +254,46 @@ void ESPUIClass::prepareFileSystem()
{
// this function should only be used once
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("About to prepare filesystem..."));
}
#endif
#if defined(ESP32)
LittleFS.format();
if (!LittleFS.begin(true))
{
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("SPIFFS Mount Failed"));
}
#endif
return;
}
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
listDir("/", 1);
Serial.println(F("SPIFFS Mount ESP32 Done"));
}
#endif
#else
LittleFS.format();
LittleFS.begin();
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("SPIFFS Mount ESP8266 Done"));
}
#endif
#endif
@ -270,10 +308,12 @@ void ESPUIClass::prepareFileSystem()
deleteFile("/js/graph.js");
deleteFile("/js/tabbedcontent.js");
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("Cleanup done"));
}
#endif
// Now write
writeFile("/index.htm", HTML_INDEX);
@ -288,17 +328,21 @@ void ESPUIClass::prepareFileSystem()
writeFile("/js/tabbedcontent.js", JS_TABBEDCONTENT);
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("Done Initializing filesystem :-)"));
}
#endif
#if defined(ESP32)
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
listDir("/", 1);
}
#endif
#endif
@ -306,54 +350,65 @@ void ESPUIClass::prepareFileSystem()
}
// Handle Websockets Communication
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
void onWsEvent(
AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len)
{
switch (type)
{
case WS_EVT_DISCONNECT:
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F("Disconnected!\n"));
}
#endif
break;
}
case WS_EVT_PONG:
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F("Received PONG!\n"));
}
#endif
break;
}
case WS_EVT_ERROR:
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F("WebSocket Error!\n"));
}
#endif
break;
}
case WS_EVT_CONNECT:
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F("Connected: "));
Serial.println(client->id());
}
#endif
ESPUI.jsonDom(client);
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("JSON Data Sent to Client!"));
}
#endif
}
break;
@ -369,6 +424,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
uint16_t id = msg.substring(msg.lastIndexOf(':') + 1).toInt();
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity >= Verbosity::VerboseJSON)
{
Serial.print(F("WS rec: "));
@ -376,27 +432,32 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
Serial.print(F("WS recognised ID: "));
Serial.println(id);
}
#endif
Control *c = ESPUI.getControl(id);
Control* c = ESPUI.getControl(id);
if (c == nullptr)
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F("No control found for ID "));
Serial.println(id);
}
#endif
return;
}
if (c->callback == nullptr)
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.print(F("No callback found for ID "));
Serial.println(id);
}
#endif
return;
}
@ -487,10 +548,12 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
}
else
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("Malformated message from the websocket"));
}
#endif
}
}
break;
@ -500,10 +563,10 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
}
}
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, const String& value, ControlColor color,
uint16_t parentControl, void (*callback)(Control*, int))
{
Control *control = new Control(type, label, callback, value, color, parentControl);
Control* control = new Control(type, label, callback, value, color, parentControl);
if (this->controls == nullptr)
{
@ -511,7 +574,7 @@ uint16_t ESPUIClass::addControl(ControlType type, const char *label, const Strin
}
else
{
Control *iterator = this->controls;
Control* iterator = this->controls;
while (iterator->next != nullptr)
{
@ -529,7 +592,7 @@ bool ESPUIClass::removeControl(uint16_t id, bool force_reload_ui)
if (nullptr == this->controls)
return false;
Control *it = this->controls;
Control* it = this->controls;
if (id == it->id)
{
@ -546,7 +609,7 @@ bool ESPUIClass::removeControl(uint16_t id, bool force_reload_ui)
return true;
}
Control *it_next = it->next;
Control* it_next = it->next;
while (nullptr != it_next && id != it_next->id)
{
it = it_next;
@ -571,11 +634,18 @@ bool ESPUIClass::removeControl(uint16_t id, bool force_reload_ui)
return false;
}
uint16_t ESPUIClass::label(const char *label, ControlColor color, const String& value) { return addControl(ControlType::Label, label, value, color); }
uint16_t ESPUIClass::label(const char* label, ControlColor color, const String& value)
{
return addControl(ControlType::Label, label, value, color);
}
uint16_t ESPUIClass::graph(const char *label, ControlColor color) { return addControl(ControlType::Graph, label, "", color); }
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);
addControl(ControlType::Min, label, String(min), ControlColor::None, sliderId);
@ -584,26 +654,27 @@ uint16_t ESPUIClass::slider(const char *label, void (*callback)(Control *, int),
return sliderId;
}
uint16_t ESPUIClass::button(const char *label, void (*callback)(Control *, int), ControlColor color, const String& value)
uint16_t ESPUIClass::button(const char* label, void (*callback)(Control*, int), ControlColor color, const String& value)
{
return addControl(ControlType::Button, label, value, color, Control::noParent, callback);
}
uint16_t ESPUIClass::switcher(const char *label, void (*callback)(Control *, int), ControlColor color, bool startState)
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::pad(const char *label, void (*callback)(Control *, int), ControlColor color)
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::padWithCenter(const char *label, void (*callback)(Control *, int), ControlColor color)
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::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);
@ -611,7 +682,7 @@ uint16_t ESPUIClass::number(const char *label, void (*callback)(Control *, int),
return numberId;
}
uint16_t ESPUIClass::gauge(const char *label, ControlColor color, int number, int min, int max)
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);
addControl(ControlType::Min, label, String(min), ControlColor::None, numberId);
@ -619,19 +690,19 @@ uint16_t ESPUIClass::gauge(const char *label, ControlColor color, int number, in
return numberId;
}
uint16_t ESPUIClass::accelerometer(const char *label, void (*callback)(Control *, int), ControlColor color)
uint16_t ESPUIClass::accelerometer(const char* label, void (*callback)(Control*, int), ControlColor color)
{
return addControl(ControlType::Accel, label, "", color, Control::noParent, callback);
}
uint16_t ESPUIClass::text(const char *label, void (*callback)(Control *, int), ControlColor color, const String& value)
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);
}
Control *ESPUIClass::getControl(uint16_t id)
Control* ESPUIClass::getControl(uint16_t id)
{
Control *control = this->controls;
Control* control = this->controls;
while (control != nullptr)
{
@ -646,7 +717,7 @@ Control *ESPUIClass::getControl(uint16_t id)
return nullptr;
}
void ESPUIClass::updateControl(Control *control, int clientId)
void ESPUIClass::updateControl(Control* control, int clientId)
{
if (!control)
{
@ -663,17 +734,21 @@ void ESPUIClass::updateControl(Control *control, int clientId)
root["color"] = (int)control->color;
serializeJson(document, json);
#if defined(DEBUG_ESPUI)
if (this->verbosity >= Verbosity::VerboseJSON)
{
Serial.println(json);
}
#endif
if (clientId < 0)
{
#if defined(DEBUG_ESPUI)
if (this->verbosity >= Verbosity::VerboseJSON)
{
Serial.println(F("TextAll"));
}
#endif
this->ws->textAll(json);
return;
}
@ -699,21 +774,23 @@ void ESPUIClass::updateControl(Control *control, int clientId)
void ESPUIClass::updateControl(uint16_t id, int clientId)
{
Control *control = getControl(id);
Control* control = getControl(id);
if (!control)
{
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.printf_P(PSTR("Error: There is no control with ID %d"), id);
}
#endif
return;
}
updateControl(control, clientId);
}
void ESPUIClass::updateControlValue(Control *control, const String& value, int clientId)
void ESPUIClass::updateControlValue(Control* control, const String& value, int clientId)
{
if (!control)
{
@ -726,41 +803,67 @@ void ESPUIClass::updateControlValue(Control *control, const String& value, int c
void ESPUIClass::updateControlValue(uint16_t id, const String& value, int clientId)
{
Control *control = getControl(id);
Control* control = getControl(id);
if (!control)
{
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.printf_P(PSTR("Error: There is no control with ID %d"), id);
}
#endif
return;
}
updateControlValue(control, value, clientId);
}
void ESPUIClass::print(uint16_t id, const String& value) { updateControlValue(id, value); }
void ESPUIClass::print(uint16_t id, const String& value)
{
updateControlValue(id, value);
}
void ESPUIClass::updateLabel(uint16_t id, const String& value) { updateControlValue(id, value); }
void ESPUIClass::updateLabel(uint16_t id, const String& value)
{
updateControlValue(id, value);
}
void ESPUIClass::updateSlider(uint16_t id, int nValue, int clientId) { updateControlValue(id, String(nValue), clientId); }
void ESPUIClass::updateSlider(uint16_t id, int nValue, int clientId)
{
updateControlValue(id, String(nValue), clientId);
}
void ESPUIClass::updateSwitcher(uint16_t id, bool nValue, int clientId) { updateControlValue(id, String(nValue ? "1" : "0"), clientId); }
void ESPUIClass::updateSwitcher(uint16_t id, bool nValue, int clientId)
{
updateControlValue(id, String(nValue ? "1" : "0"), clientId);
}
void ESPUIClass::updateNumber(uint16_t id, int number, int clientId) { updateControlValue(id, String(number), clientId); }
void ESPUIClass::updateNumber(uint16_t id, int number, int clientId)
{
updateControlValue(id, String(number), clientId);
}
void ESPUIClass::updateText(uint16_t id, const String& text, int clientId) { updateControlValue(id, text, clientId); }
void ESPUIClass::updateText(uint16_t id, const String& text, int clientId)
{
updateControlValue(id, text, clientId);
}
void ESPUIClass::updateSelect(uint16_t id, const String& text, int clientId) { updateControlValue(id, text, clientId); }
void ESPUIClass::updateSelect(uint16_t id, const String& text, int clientId)
{
updateControlValue(id, text, clientId);
}
void ESPUIClass::updateGauge(uint16_t id, int number, int clientId) { updateControlValue(id, String(number), clientId); }
void ESPUIClass::updateGauge(uint16_t id, int number, int clientId)
{
updateControlValue(id, String(number), clientId);
}
void ESPUIClass::clearGraph(uint16_t id, int clientId) {}
void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
{
Control *control = getControl(id);
Control* control = getControl(id);
if (!control)
{
return;
@ -775,10 +878,12 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
root["id"] = control->id;
serializeJson(document, json);
#if defined(DEBUG_ESPUI)
if (this->verbosity >= Verbosity::VerboseJSON)
{
Serial.println(json);
}
#endif
if (clientId < 0)
{
@ -797,10 +902,12 @@ void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
{
this->ws->client(tryId)->text(json);
#if defined(DEBUG_ESPUI)
if (this->verbosity >= Verbosity::VerboseJSON)
{
Serial.println(json);
}
#endif
}
count++;
@ -815,7 +922,7 @@ Initially this function used to send the control element data individually.
Due to a change in the ESPAsyncWebserver library this had top be changed to be
sent as one blob at the beginning. Therefore a new type is used as well
*/
void ESPUIClass::jsonDom(AsyncWebSocketClient *client)
void ESPUIClass::jsonDom(AsyncWebSocketClient* client)
{
String json;
DynamicJsonDocument document(jsonInitialDocumentSize);
@ -823,7 +930,7 @@ void ESPUIClass::jsonDom(AsyncWebSocketClient *client)
document["sliderContinuous"] = sliderContinuous;
JsonArray items = document.createNestedArray("controls");
Control *control = this->controls;
Control* control = this->controls;
JsonObject titleItem = items.createNestedObject();
titleItem["type"] = (int)UI_TITLE;
@ -864,10 +971,12 @@ void ESPUIClass::jsonDom(AsyncWebSocketClient *client)
// Send as one big bunch
serializeJson(document, json);
#if defined(DEBUG_ESPUI)
if (this->verbosity >= Verbosity::VerboseJSON)
{
Serial.println(json);
}
#endif
if (client != nullptr)
{
@ -888,15 +997,17 @@ void ESPUIClass::jsonReload()
root["type"] = (int)UI_RELOAD;
serializeJson(document, json);
#if defined(DEBUG_ESPUI)
if (this->verbosity >= Verbosity::VerboseJSON)
{
Serial.println(json);
}
#endif
this->ws->textAll(json);
}
void ESPUIClass::beginSPIFFS(const char *_title, const char *username, const char *password)
void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const char* password)
{
ui_title = _title;
this->basicAuthUsername = username;
@ -916,25 +1027,33 @@ void ESPUIClass::beginSPIFFS(const char *_title, const char *username, const cha
if (!LittleFS.begin())
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO PREPARE YOUR ESP!!!!!!!"));
Serial.println(F("SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO "
"PREPARE YOUR ESP!!!!!!!"));
}
#endif
return;
}
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
listDir("/", 1);
}
#endif
if (!LittleFS.exists("/index.htm"))
{
#if defined(DEBUG_ESPUI)
if (ESPUI.verbosity)
{
Serial.println(F("Please read the README!!!!!!!, Make sure to ESPUI.prepareFileSystem() once in an empty sketch"));
Serial.println(F("Please read the README!!!!!!!, Make sure to "
"ESPUI.prepareFileSystem() once in an empty sketch"));
}
#endif
return;
}
@ -956,7 +1075,7 @@ void ESPUIClass::beginSPIFFS(const char *_title, const char *username, const cha
}
// Heap for general Servertest
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
@ -965,17 +1084,19 @@ void ESPUIClass::beginSPIFFS(const char *_title, const char *username, const cha
request->send(200, "text/plain", String(ESP.getFreeHeap()) + " In SPIFFSmode");
});
server->onNotFound([](AsyncWebServerRequest *request) { request->send(404); });
server->onNotFound([](AsyncWebServerRequest* request) { request->send(404); });
server->begin();
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("UI Initialized"));
}
#endif
}
void ESPUIClass::begin(const char *_title, const char *username, const char *password)
void ESPUIClass::begin(const char* _title, const char* username, const char* password)
{
basicAuthUsername = username;
basicAuthPassword = password;
@ -1000,99 +1121,106 @@ void ESPUIClass::begin(const char *_title, const char *username, const char *pas
if (basicAuth && WS_AUTHENTICATION)
ws->setAuthentication(username, password);
server->on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", HTML_INDEX);
AsyncWebServerResponse* response = request->beginResponse_P(200, "text/html", HTML_INDEX);
request->send(response);
});
// Javascript files
server->on("/js/zepto.min.js", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/js/zepto.min.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP));
AsyncWebServerResponse* response
= request->beginResponse_P(200, "application/javascript", JS_ZEPTO_GZIP, sizeof(JS_ZEPTO_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
server->on("/js/controls.js", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/js/controls.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_CONTROLS_GZIP, sizeof(JS_CONTROLS_GZIP));
AsyncWebServerResponse* response
= request->beginResponse_P(200, "application/javascript", JS_CONTROLS_GZIP, sizeof(JS_CONTROLS_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
server->on("/js/slider.js", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/js/slider.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP));
AsyncWebServerResponse* response
= request->beginResponse_P(200, "application/javascript", JS_SLIDER_GZIP, sizeof(JS_SLIDER_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
server->on("/js/graph.js", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/js/graph.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_GRAPH_GZIP, sizeof(JS_GRAPH_GZIP));
AsyncWebServerResponse* response
= request->beginResponse_P(200, "application/javascript", JS_GRAPH_GZIP, sizeof(JS_GRAPH_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
server->on("/js/tabbedcontent.js", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/js/tabbedcontent.js", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "application/javascript", JS_TABBEDCONTENT_GZIP, sizeof(JS_TABBEDCONTENT_GZIP));
AsyncWebServerResponse* response = request->beginResponse_P(
200, "application/javascript", JS_TABBEDCONTENT_GZIP, sizeof(JS_TABBEDCONTENT_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
// Stylesheets
server->on("/css/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/css/style.css", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP));
AsyncWebServerResponse* response
= request->beginResponse_P(200, "text/css", CSS_STYLE_GZIP, sizeof(CSS_STYLE_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
server->on("/css/normalize.css", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/css/normalize.css", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
}
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP));
AsyncWebServerResponse* response
= request->beginResponse_P(200, "text/css", CSS_NORMALIZE_GZIP, sizeof(CSS_NORMALIZE_GZIP));
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
// Heap for general Servertest
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest* request) {
if (ESPUI.basicAuth && !request->authenticate(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword))
{
return request->requestAuthentication();
@ -1101,16 +1229,21 @@ void ESPUIClass::begin(const char *_title, const char *username, const char *pas
request->send(200, "text/plain", String(ESP.getFreeHeap()) + " In Memorymode");
});
server->onNotFound([](AsyncWebServerRequest *request) { request->send(404); });
server->onNotFound([](AsyncWebServerRequest* request) { request->send(404); });
server->begin();
#if defined(DEBUG_ESPUI)
if (this->verbosity)
{
Serial.println(F("UI Initialized"));
}
#endif
}
void ESPUIClass::setVerbosity(Verbosity v) { this->verbosity = v; }
void ESPUIClass::setVerbosity(Verbosity v)
{
this->verbosity = v;
}
ESPUIClass ESPUI;

View File

@ -11,11 +11,12 @@
#if defined(ESP32)
#include "LittleFS.h"
#include "WiFi.h"
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "LittleFS.h"
#include "WiFi.h"
#else
#include <ArduinoOTA.h>
@ -23,8 +24,8 @@
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <LittleFS.h>
#include <Hash.h>
#include <LittleFS.h>
#include <SPIFFSEditor.h>
#define FILE_WRITE "w"
@ -131,25 +132,38 @@ class Control
public:
ControlType type;
uint16_t id; // just mirroring the id here for practical reasons
const char *label;
void (*callback)(Control *, int);
const char* label;
void (*callback)(Control*, int);
String value;
ControlColor color;
uint16_t parentControl;
Control *next;
Control* next;
static constexpr uint16_t noParent = 0xffff;
Control(ControlType type, const char *label, void (*callback)(Control *, int), const String& value, ControlColor color,
uint16_t parentControl = Control::noParent)
: type(type), label(label), callback(callback), value(value), color(color), parentControl(parentControl), next(nullptr)
Control(ControlType type, const char* label, void (*callback)(Control*, int), const String& value,
ControlColor color, uint16_t parentControl = Control::noParent)
: type(type),
label(label),
callback(callback),
value(value),
color(color),
parentControl(parentControl),
next(nullptr)
{
id = idCounter++;
}
Control(const Control &control)
: type(control.type), id(control.id), label(control.label), callback(control.callback), value(control.value), color(control.color),
parentControl(control.parentControl), next(control.next) {}
Control(const Control& control)
: type(control.type),
id(control.id),
label(control.label),
callback(control.callback),
value(control.value),
color(control.color),
parentControl(control.parentControl),
next(control.next)
{}
private:
static uint16_t idCounter;
@ -200,45 +214,57 @@ public:
bool sliderContinuous;
void setVerbosity(Verbosity verbosity);
void begin(const char *_title, const char *username = nullptr, const char *password = nullptr); // Setup server and page in Memorymode
void beginSPIFFS(const char *_title, const char *username = nullptr, const char *password = nullptr); // Setup server and page in SPIFFSmode
void begin(const char* _title, const char* username = nullptr,
const char* password = nullptr); // Setup server and page in Memorymode
void beginSPIFFS(const char* _title, const char* username = nullptr,
const char* password = nullptr); // Setup server and page in SPIFFSmode
void prepareFileSystem(); // Initially preps the filesystem and loads a lot of stuff into SPIFFS
void prepareFileSystem(); // Initially preps the filesystem and loads a lot of
// stuff into SPIFFS
void list(); // Lists SPIFFS 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, const String& value = String(""),
ControlColor color = ControlColor::Turquoise, uint16_t parentControl = Control::noParent,
void (*callback)(Control*, int) = nullptr);
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
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
uint16_t slider(const char *label, void (*callback)(Control *, int), ControlColor color, int value, int min = 0,
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,
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 text(const char* label, void (*callback)(Control*, int), ControlColor color,
const String& value = ""); // Create a Text Input Control
// Output only
uint16_t label(const char *label, ControlColor color, const String& value = ""); // Create Label
uint16_t graph(const char *label, ControlColor color); // Create Graph display
uint16_t gauge(const char *label, ControlColor color, int value, int min = 0, int max = 100); // Create Gauge display
uint16_t label(const char* label, ControlColor color,
const String& value = ""); // Create Label
uint16_t graph(const char* label, ControlColor color); // Create Graph display
uint16_t gauge(const char* label, ControlColor color, int value, int min = 0,
int max = 100); // Create Gauge display
// Input only
uint16_t accelerometer(const char *label, void (*callback)(Control *, int), ControlColor color);
uint16_t accelerometer(const char* label, void (*callback)(Control*, int), ControlColor color);
// Update Elements
Control *getControl(uint16_t id);
Control* getControl(uint16_t id);
// Update Elements
void updateControlValue(uint16_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 updateControl(uint16_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 updateLabel(uint16_t id, const String& value);
@ -253,19 +279,19 @@ public:
void addGraphPoint(uint16_t id, int nValue, int clientId = -1);
// Variables
const char *ui_title = "ESPUI"; // Store UI Title and Header Name
Control *controls = nullptr;
const char* ui_title = "ESPUI"; // Store UI Title and Header Name
Control* controls = nullptr;
void jsonReload();
void jsonDom(AsyncWebSocketClient *client = nullptr);
void jsonDom(AsyncWebSocketClient* client = nullptr);
Verbosity verbosity;
AsyncWebServer *server;
AsyncWebSocket *ws;
AsyncWebServer* server;
AsyncWebSocket* ws;
private:
const char *basicAuthUsername = nullptr;
const char *basicAuthPassword = nullptr;
const char* basicAuthUsername = nullptr;
const char* basicAuthPassword = nullptr;
bool basicAuth = true;
};