From ba06ff5fbf9e734466bb4b159661daf3694c255f Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Tue, 28 Nov 2017 13:48:58 +0100 Subject: [PATCH] Adding Slider Related functions and cleaning up --- src/ESPUI.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++--------- src/ESPUI.h | 8 +++++ 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index 9f88c98..8852a4b 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -1,7 +1,47 @@ #include "ESPUI.h" + +#include "uploadDataIndex.h" +#include "uploadDataControls.h" +#include "uploadDataStyle.h" + +#include "uploadDataZepto.h" +#include "uploadDataNormalize.h" + #include #include +void ESPUIClass::prepareFileSystem(){ +// this function should only be used once, maybe even halt the system +/* +Needed Things +index.htm HTML_INDEX + +style/style.css +stye/normalize.css + +js/controls.js +js/zepto.js + +*/ +if(!SPIFFS.begin()){ + if(debug) Serial.println("SPIFFS Mount Failed"); + return; +} + +/* +listDir(SPIFFS, "/", 0); +writeFile(SPIFFS, "/hello.txt", "Hello "); +appendFile(SPIFFS, "/hello.txt", "World!\n"); +readFile(SPIFFS, "/hello.txt"); +deleteFile(SPIFFS, "/foo.txt"); +renameFile(SPIFFS, "/hello.txt", "/foo.txt"); +readFile(SPIFFS, "/foo.txt"); +testFileIO(SPIFFS, "/test.txt"); +*/ + +} + + // Handle Websockets Communication void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { @@ -22,48 +62,37 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, for (size_t i = 0; i < len; i++) { msg += (char)data[i]; } + + Control *c = ESPUI.controls[msg.substring(msg.lastIndexOf(':') + 1).toInt()]; + if (msg.startsWith("bdown:")) { - Control *c = ESPUI.controls[msg.substring(6).toInt()]; c->callback(*c, B_DOWN); } else if (msg.startsWith("bup:")) { - Control *c = ESPUI.controls[msg.substring(4).toInt()]; c->callback(*c, B_UP); } else if (msg.startsWith("pfdown:")) { - Control *c = ESPUI.controls[msg.substring(7).toInt()]; c->callback(*c, P_FOR_DOWN); } else if (msg.startsWith("pfup:")) { - Control *c = ESPUI.controls[msg.substring(5).toInt()]; c->callback(*c, P_FOR_UP); } else if (msg.startsWith("pldown:")) { - Control *c = ESPUI.controls[msg.substring(7).toInt()]; c->callback(*c, P_LEFT_DOWN); } else if (msg.startsWith("plup:")) { - Control *c = ESPUI.controls[msg.substring(5).toInt()]; c->callback(*c, P_LEFT_UP); } else if (msg.startsWith("prdown:")) { - Control *c = ESPUI.controls[msg.substring(7).toInt()]; c->callback(*c, P_RIGHT_DOWN); } else if (msg.startsWith("prup:")) { - Control *c = ESPUI.controls[msg.substring(5).toInt()]; c->callback(*c, P_RIGHT_UP); } else if (msg.startsWith("pbdown:")) { - Control *c = ESPUI.controls[msg.substring(7).toInt()]; c->callback(*c, P_BACK_DOWN); } else if (msg.startsWith("pbup:")) { - Control *c = ESPUI.controls[msg.substring(5).toInt()]; c->callback(*c, P_BACK_UP); } else if (msg.startsWith("pcdown:")) { - Control *c = ESPUI.controls[msg.substring(7).toInt()]; c->callback(*c, P_CENTER_DOWN); } else if (msg.startsWith("pcup:")) { - Control *c = ESPUI.controls[msg.substring(5).toInt()]; c->callback(*c, P_CENTER_UP); } else if (msg.startsWith("sactive:")) { - Control *c = ESPUI.controls[msg.substring(8).toInt()]; ESPUI.updateSwitcher(c->id, true); c->callback(*c, S_ACTIVE); } else if (msg.startsWith("sinactive:")) { - Control *c = ESPUI.controls[msg.substring(10).toInt()]; ESPUI.updateSwitcher(c->id, false); c->callback(*c, S_INACTIVE); } @@ -93,6 +122,29 @@ void ESPUIClass::label(const char *label, int color, String value) { cIndex++; } +// TODO: this still needs a range setting +void ESPUIClass::slider(const char *label, void (*callBack)(Control, int), int color, String value) { + if (labelExists(label)) { + if (debug) + Serial.println("UI ERROR: Element " + String(label) + + " exists, skipping creating element!"); + return; + } + + Control *newSL = new Control(); + newSL->type = UI_SLIDER; + newSL->label = label; + newSL->color = color; + if (value != "") + newSL->value = value; + else + newSL->value = ""; // TODO: init with half value + newSL->callback = callBack; + newSL->id = cIndex; + controls[cIndex] = newSL; + cIndex++; +} + void ESPUIClass::button(const char *label, void (*callBack)(Control, int), int color, String value) { if (labelExists(label)) { @@ -253,6 +305,7 @@ void ESPUIClass::jsonDom(AsyncWebSocketClient *client) { } void ESPUIClass::begin(const char *_title) { + ui_title = _title; server = new AsyncWebServer(80); ws = new AsyncWebSocket("/ws"); diff --git a/src/ESPUI.h b/src/ESPUI.h index 80a26e0..0fb8be8 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -36,6 +36,8 @@ typedef struct Control { #define UI_CPAD 5 #define UPDATE_LABEL 6 #define UPDATE_SWITCH 7 +#define UI_SLIDER 8 +#define UPDATE_SLIDER 9 // Values #define B_DOWN -1 @@ -71,6 +73,8 @@ class ESPUIClass { public: void begin(const char *_title); // Setup servers and page + void prepareFileSystem(); // Initially preps the filesystem and loads a lot of stuff into SPIFFS + // Creating Elements void label(const char *label, int color, String value = ""); // Create Label void button(const char *label, void (*callBack)(Control, int), int color, @@ -80,6 +84,7 @@ public: int color); // Create Toggle Button void pad(const char *label, bool centerButton, void (*callBack)(Control, int), int color); // Create Pad Control + void slider(const char *label, void (*callBack)(Control, int), int color, String value); // Create Slider Control // Update Elements void print(int id, String value); @@ -88,6 +93,9 @@ public: void updateSwitcher(int id, bool nValue); void updateSwitcher(String label, bool nValue); + void updateSlider(int id, int nValue); + void updateSlider(String label, int nValue); + // Variables --- const char *ui_title = "ESPUI"; // Store UI Title and Header Name int cIndex = 0; // Control index