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

#9 adding control id as return value

This commit is contained in:
Lukas Bachschwell 2018-04-17 19:54:07 +02:00
parent dda4e9e771
commit 40b13430cb
3 changed files with 67 additions and 49 deletions

View File

@ -51,7 +51,7 @@ Download the [Repository](https://github.com/s00500/ESPUI/archive/master.zip), G
ESPUI **NEEDS** its files burnt on the SPIFFS filesystem on the ESP. **Without this ESPUI will NOT work at all** ESPUI **NEEDS** its files burnt on the SPIFFS filesystem on the ESP. **Without this ESPUI will NOT work at all**
There are now two ways to do this: you can either use the upload tool or you use the library function `ESPUI.prepareFileSystem()` There are now two ways to do this: you can either use the upload tool or you use the library function `ESPUI.prepareFileSystem()`
#### Simple filesystem preparation (recomended) #### Simple filesystem preparation (recomended, but currently not working well on esp32, see issues)
Just open the example sketch **prepareFileSystem** and run it on the ESP, (give it 5 - 10 seconds), Just open the example sketch **prepareFileSystem** and run it on the ESP, (give it 5 - 10 seconds),
The library will create all needed files. The library will create all needed files.
@ -81,10 +81,13 @@ Now you are set to go and use any code you want to with this library
- ~~Setup SPIFFS using values in program memory~~ - ~~Setup SPIFFS using values in program memory~~
- ~~ESP8266 support~~ - ~~ESP8266 support~~
- Document slider - Document slider
- New images in docu
- proper return value (as int and not as string) for slider - proper return value (as int and not as string) for slider
- Maybe a slider range setting, meanwhile please use map() - Maybe a slider range setting, meanwhile please use map()
- Improve slider stability - Improve slider stability
- Improve general stability - Improve general stability
- Multiline Labels
- PlattformIO Integration
## Documentation ## Documentation

View File

@ -108,11 +108,19 @@ void writeFile(const char *path, const char *data) {
Serial.println("Failed to open file for writing"); Serial.println("Failed to open file for writing");
return; return;
} }
#if defined(ESP32)
if (file.print(data)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
#else
if (file.print(FPSTR(data))) { if (file.print(FPSTR(data))) {
Serial.println("File written"); Serial.println("File written");
} else { } else {
Serial.println("Write failed"); Serial.println("Write failed");
} }
#endif
file.close(); file.close();
} }
@ -140,15 +148,15 @@ void ESPUIClass::prepareFileSystem() {
// TODO: This is a workaround, have to find out why SPIFFS on ESP32 behaves // TODO: This is a workaround, have to find out why SPIFFS on ESP32 behaves
// incredibly strangely, see issue #6 // incredibly strangely, see issue #6
/* /*
deleteFile("/index.htm"); deleteFile("/index.htm");
deleteFile("/css/style.css"); deleteFile("/css/style.css");
deleteFile("/css/normalize.css"); deleteFile("/css/normalize.css");
deleteFile("/js/zepto.min.js"); deleteFile("/js/zepto.min.js");
deleteFile("/js/controls.js"); deleteFile("/js/controls.js");
deleteFile("/js/slider.js"); deleteFile("/js/slider.js");
*/ */
Serial.println("Cleanup done"); Serial.println("Cleanup done");
@ -202,7 +210,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
return; return;
} }
Control *c = Control *c =
ESPUI.controls[msg.substring(msg.lastIndexOf(':') + 1).toInt()]; ESPUI.controls[msg.substring(msg.lastIndexOf(':') + 1).toInt()];
if (msg.startsWith("bdown:")) { if (msg.startsWith("bdown:")) {
c->callback(*c, B_DOWN); c->callback(*c, B_DOWN);
@ -236,7 +244,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
c->callback(*c, S_INACTIVE); c->callback(*c, S_INACTIVE);
} else if (msg.startsWith("slvalue:")) { } else if (msg.startsWith("slvalue:")) {
int value = int value =
msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt(); msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
ESPUI.updateSlider(c->id, value, client->id()); ESPUI.updateSlider(c->id, value, client->id());
c->callback(*c, SL_VALUE); c->callback(*c, SL_VALUE);
} }
@ -244,7 +252,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
} }
} }
void ESPUIClass::label(const char *label, int color, String value) { int ESPUIClass::label(const char *label, int color, String value) {
if (labelExists(label)) { if (labelExists(label)) {
if (debug) if (debug)
Serial.println("UI ERROR: Element " + String(label) + Serial.println("UI ERROR: Element " + String(label) +
@ -264,11 +272,12 @@ void ESPUIClass::label(const char *label, int color, String value) {
newL->id = cIndex; newL->id = cIndex;
controls[cIndex] = newL; controls[cIndex] = newL;
cIndex++; cIndex++;
return cIndex - 1;
} }
// TODO: this still needs a range setting // TODO: this still needs a range setting
void ESPUIClass::slider(const char *label, void (*callBack)(Control, int), int ESPUIClass::slider(const char *label, void (*callBack)(Control, int),
int color, String value) { int color, String value) {
if (labelExists(label)) { if (labelExists(label)) {
if (debug) if (debug)
Serial.println("UI ERROR: Element " + String(label) + Serial.println("UI ERROR: Element " + String(label) +
@ -288,10 +297,11 @@ void ESPUIClass::slider(const char *label, void (*callBack)(Control, int),
newSL->id = cIndex; newSL->id = cIndex;
controls[cIndex] = newSL; controls[cIndex] = newSL;
cIndex++; cIndex++;
return cIndex - 1;
} }
void ESPUIClass::button(const char *label, void (*callBack)(Control, int), int ESPUIClass::button(const char *label, void (*callBack)(Control, int),
int color, String value) { int color, String value) {
if (labelExists(label)) { if (labelExists(label)) {
if (debug) if (debug)
Serial.println("UI ERROR: Element " + String(label) + Serial.println("UI ERROR: Element " + String(label) +
@ -313,10 +323,11 @@ void ESPUIClass::button(const char *label, void (*callBack)(Control, int),
newB->id = cIndex; newB->id = cIndex;
controls[cIndex] = newB; controls[cIndex] = newB;
cIndex++; cIndex++;
return cIndex - 1;
} }
void ESPUIClass::switcher(const char *label, bool startState, int ESPUIClass::switcher(const char *label, bool startState,
void (*callBack)(Control, int), int color) { void (*callBack)(Control, int), int color) {
if (labelExists(label)) { if (labelExists(label)) {
if (debug) if (debug)
Serial.println("UI ERROR: Element " + String(label) + Serial.println("UI ERROR: Element " + String(label) +
@ -333,10 +344,11 @@ void ESPUIClass::switcher(const char *label, bool startState,
newS->id = cIndex; newS->id = cIndex;
controls[cIndex] = newS; controls[cIndex] = newS;
cIndex++; cIndex++;
return cIndex - 1;
} }
void ESPUIClass::pad(const char *label, bool center, int ESPUIClass::pad(const char *label, bool center,
void (*callBack)(Control, int), int color) { void (*callBack)(Control, int), int color) {
if (labelExists(label)) { if (labelExists(label)) {
if (debug) if (debug)
Serial.println("UI ERROR: Element " + String(label) + Serial.println("UI ERROR: Element " + String(label) +
@ -355,6 +367,7 @@ void ESPUIClass::pad(const char *label, bool center,
newP->id = cIndex; newP->id = cIndex;
controls[cIndex] = newP; controls[cIndex] = newP;
cIndex++; cIndex++;
return cIndex - 1;
} }
void ESPUIClass::print(int id, String value) { void ESPUIClass::print(int id, String value) {
@ -510,7 +523,9 @@ void ESPUIClass::begin(const char *_title) {
}); });
server->onNotFound( server->onNotFound(
[](AsyncWebServerRequest *request) { request->send(404); }); [](AsyncWebServerRequest *request) {
request->send(404);
});
server->begin(); server->begin();
if (debug) if (debug)

View File

@ -86,44 +86,44 @@ typedef struct Control {
class ESPUIClass { class ESPUIClass {
public: public:
void begin(const char *_title); // Setup servers and page void begin(const char *_title); // Setup servers and page
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(); void list();
// Creating Elements // Creating Elements
void label(const char *label, int color, String value = ""); // Create Label int label(const char *label, int color, String value = ""); // Create Label
void button(const char *label, void (*callBack)(Control, int), int color, int button(const char *label, void (*callBack)(Control, int), int color,
String value = ""); // Create Event Button String value = ""); // Create Event Button
void switcher(const char *label, bool startState, int switcher(const char *label, bool startState,
void (*callBack)(Control, int), void (*callBack)(Control, int),
int color); // Create Toggle Button int color); // Create Toggle Button
void pad(const char *label, bool centerButton, void (*callBack)(Control, int), int pad(const char *label, bool centerButton, void (*callBack)(Control, int),
int color); // Create Pad Control int color); // Create Pad Control
void slider(const char *label, void (*callBack)(Control, int), int color, String value); // Create Slider Control int slider(const char *label, void (*callBack)(Control, int), int color, String value); // Create Slider Control
// Update Elements // Update Elements
void print(int id, String value); void print(int id, String value);
void print(String label, String value); void print(String label, String value);
void updateSwitcher(int id, bool nValue, int clientId = -1); void updateSwitcher(int id, bool nValue, int clientId = -1);
void updateSwitcher(String label, bool nValue, int clientId = -1); void updateSwitcher(String label, bool nValue, int clientId = -1);
void updateSlider(int id, int nValue, int clientId = -1); void updateSlider(int id, int nValue, int clientId = -1);
void updateSlider(String label, int nValue, int clientId = -1); void updateSlider(String label, int nValue, int clientId = -1);
void textThem(String text, int clientId); void textThem(String text, int clientId);
// Variables --- // Variables ---
const char *ui_title = "ESPUI"; // Store UI Title and Header Name const char *ui_title = "ESPUI"; // Store UI Title and Header Name
int cIndex = 0; // Control index int cIndex = 0; // Control index
Control *controls[25]; Control *controls[25];
void jsonDom(AsyncWebSocketClient *client); void jsonDom(AsyncWebSocketClient *client);
int getIdByLabel(String label); int getIdByLabel(String label);
bool labelExists(String label); bool labelExists(String label);
private: private:
AsyncWebServer *server; AsyncWebServer *server;
AsyncWebSocket *ws; AsyncWebSocket *ws;
}; };
extern ESPUIClass ESPUI; extern ESPUIClass ESPUI;