mirror of
https://github.com/s00500/ESPUI.git
synced 2024-12-23 00:40:49 +00:00
Merge pull request #13 from s00500/development
Merging loads of changes
This commit is contained in:
commit
9717f6c239
38
README.md
38
README.md
@ -29,7 +29,7 @@ This library is dependent on the following libraries to function properly.
|
||||
|
||||
Make sure all the dependencies are installed, then install like so:
|
||||
|
||||
#### Directly Through Arduino IDE
|
||||
#### Directly Through Arduino IDE (*recommended*)
|
||||
|
||||
You can find this Library in the Arduino IDE library manager
|
||||
Go to Sketch > Include Library > Library Manager > Search for "ESPUI" > Install
|
||||
@ -46,14 +46,19 @@ For macOs: Download the [Repository](https://github.com/s00500/ESPUI/archive/mas
|
||||
|
||||
Download the [Repository](https://github.com/s00500/ESPUI/archive/master.zip), Go to Sketch>Include Library>Add .zip Library> Select the Downloaded .zip File.
|
||||
|
||||
## Getting started (Filesystem upload)
|
||||
## Getting started
|
||||
|
||||
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()`
|
||||
ESPUI serves several Files to the browser to build up its webinterface. This can be achieved in 2 wasy:
|
||||
*PROGMEM* or *SPIFFS*
|
||||
|
||||
#### Simple filesystem preparation (recomended)
|
||||
*When ESPUI.begin() is called the default is serving files from Memory and ESPUI should work out of the box!*
|
||||
|
||||
Just open the example sketch **prepareFileSystem** and run it on the ESP, (give it 5 - 10 seconds),
|
||||
But if this causes your program to *use too much memory* you can burn the files into the SPIFFS filesystem on the ESP.
|
||||
There are now two ways to do this: you can either use the ESP file upload tool or you use the library function `ESPUI.prepareFileSystem()`
|
||||
|
||||
#### Simple filesystem preparation (*recommended*)
|
||||
|
||||
Just open the example sketch **prepareFileSystem** and run it on the ESP, (give it up to 30 seconds, you can see the status on the Serial Monitor),
|
||||
The library will create all needed files.
|
||||
Congratulations, you are done, from now on you just need to to this again when there is a library update, or when you want to use another chip :-)
|
||||
Now you can upload your normal sketch, when you do not call the `ESPUI.prepareFileSystem()` function the compiler will strip out all the unnecessary that is already saved in the chip's filesystem and you have more programm memory to work with.
|
||||
@ -75,22 +80,34 @@ Now you are set to go and use any code you want to with this library
|
||||
- Slider
|
||||
|
||||
Checkout the example for the usage
|
||||
|
||||
## Available colors:
|
||||
|
||||
- COLOR_TURQUOISE
|
||||
- COLOR_EMERALD
|
||||
- COLOR_PETERRIVER
|
||||
- COLOR_WETASPHALT
|
||||
- COLOR_SUNFLOWER
|
||||
- COLOR_CARROT
|
||||
- COLOR_ALIZARIN
|
||||
- COLOR_NONE
|
||||
|
||||
## Roadmap :
|
||||
|
||||
- ~~Setup SPIFFS using values in program memory~~
|
||||
- ~~ESP8266 support~~
|
||||
- ~~PlattformIO Integration~~
|
||||
- ~~Multiline Labels~~
|
||||
- ~~GZip Files and serve from memory~~
|
||||
- Datagraph output -> *WIP*
|
||||
- Number input -> *WIP*
|
||||
- GZip Files to improve loadspeed and reduce server load
|
||||
- Text input -> *WIP*
|
||||
- Document slider
|
||||
- New images in docu
|
||||
- proper return value (as int and not as string) for slider
|
||||
- Maybe a slider range setting, meanwhile please use map()
|
||||
- Improve slider stability
|
||||
- Improve general stability
|
||||
- Multiline Labels
|
||||
|
||||
## Documentation
|
||||
|
||||
@ -130,6 +147,8 @@ Button pads come in two flavours: with or without a center button. They are very
|
||||
|
||||
Labels are a nice tool to get information from the robot to the user interface. This can be done to show states, values of sensors and configuration parameters. To send data from the code use ESP.print(labelId, “Text”); . Labels get a name on creation and a initial value. The name is not changeable once the UI initialised.
|
||||
|
||||
Labels automatically wrap your text. If you want them to have multiple lines use the normal `<br>` tag in the string you print to the label
|
||||
|
||||
#### Slider
|
||||
|
||||
There is also an slider component now, needs to be documented though
|
||||
@ -140,5 +159,8 @@ After all the elements are configured you can use ESPUI.begin(“Some Title”);
|
||||
The library is designed to be easy to use and can still be extended with a lot of more functionality.
|
||||
|
||||
|
||||
# Notes for Development
|
||||
All changes to the client side files can be made in the examples/gui/data directory. Using the file uploader thin can be used for development. After this you have to compress them and then you can gzip them. I wrote a little useful jsfiddle for this, [CHECK IT OUT](https://jsfiddle.net/s00500/yvLbhuuv/)
|
||||
|
||||
# Contribute
|
||||
Liked this Library? You can **support** me by sending me a :coffee: [Coffee](https://paypal.me/lukasbachschwell/3).
|
||||
|
@ -107,9 +107,9 @@ void setup(void) {
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
#if defined(ESP32)
|
||||
WiFi.setHostname(ssid);
|
||||
WiFi.setHostname(ssid);
|
||||
#else
|
||||
WiFi.hostname(ssid);
|
||||
WiFi.hostname(ssid);
|
||||
#endif
|
||||
|
||||
WiFi.softAP(ssid);
|
||||
@ -120,18 +120,18 @@ void setup(void) {
|
||||
|
||||
// change the beginning to this if you want to join an existing network
|
||||
/*
|
||||
Serial.begin(115200);
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("");
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
*/
|
||||
Serial.begin(115200);
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("");
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
*/
|
||||
|
||||
ESPUI.label("Status:", COLOR_TURQUOISE, "Stop");
|
||||
ESPUI.label("Millis:", COLOR_EMERALD, "0");
|
||||
@ -144,7 +144,7 @@ void setup(void) {
|
||||
ESPUI.slider("Slider one", &slider, COLOR_ALIZARIN, "30");
|
||||
ESPUI.slider("Slider two", &slider, COLOR_NONE, "100");
|
||||
|
||||
ESPUI.begin("ESP32 Control");
|
||||
ESPUI.begin("ESPUI Control");
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
|
114
src/ESPUI.cpp
114
src/ESPUI.cpp
@ -251,10 +251,13 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
||||
ESPUI.updateSwitcher(c->id, false);
|
||||
c->callback(*c, S_INACTIVE);
|
||||
} else if (msg.startsWith("slvalue:")) {
|
||||
int value =
|
||||
msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
|
||||
int value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
|
||||
ESPUI.updateSlider(c->id, value, client->id());
|
||||
c->callback(*c, SL_VALUE);
|
||||
} else if (msg.startsWith("nvalue:")) {
|
||||
int value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
|
||||
ESPUI.updateNumber(c->id, value, client->id());
|
||||
c->callback(*c, N_VALUE);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -265,9 +268,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
||||
|
||||
int ESPUIClass::label(const char *label, int color, String value) {
|
||||
if (labelExists(label)) {
|
||||
if (debug)
|
||||
Serial.println("UI ERROR: Element " + String(label) +
|
||||
" exists, skipping creating element!");
|
||||
if (debug) Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -286,9 +287,24 @@ int ESPUIClass::label(const char *label, int color, String value) {
|
||||
return cIndex - 1;
|
||||
}
|
||||
|
||||
int ESPUIClass::graph(const char *label, int color) {
|
||||
if (labelExists(label)) {
|
||||
if (debug) Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
Control *newG = new Control();
|
||||
newG->type = UI_GRAPH;
|
||||
newG->label = label;
|
||||
newG->color = color;
|
||||
newG->id = cIndex;
|
||||
controls[cIndex] = newG;
|
||||
cIndex++;
|
||||
return cIndex - 1;
|
||||
}
|
||||
|
||||
// TODO: this still needs a range setting
|
||||
int ESPUIClass::slider(const char *label, void (*callBack)(Control, int),
|
||||
int color, String value) {
|
||||
int 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) +
|
||||
@ -337,8 +353,7 @@ int ESPUIClass::button(const char *label, void (*callBack)(Control, int),
|
||||
return cIndex - 1;
|
||||
}
|
||||
|
||||
int ESPUIClass::switcher(const char *label, bool startState,
|
||||
void (*callBack)(Control, int), int color) {
|
||||
int ESPUIClass::switcher(const char *label, bool startState, void (*callBack)(Control, int), int color) {
|
||||
if (labelExists(label)) {
|
||||
if (debug)
|
||||
Serial.println("UI ERROR: Element " + String(label) +
|
||||
@ -381,6 +396,26 @@ int ESPUIClass::pad(const char *label, bool center,
|
||||
return cIndex - 1;
|
||||
}
|
||||
|
||||
// TODO: min and max need to be saved, they also need to be sent to the frontend
|
||||
int ESPUIClass::number(const char *label, void (*callBack)(Control, int), int color, int number, int min, int max) {
|
||||
if (labelExists(label)) {
|
||||
if (debug)
|
||||
Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
Control *newN = new Control();
|
||||
newN->type = UI_NUMBER;
|
||||
newN->label = label;
|
||||
newN->color = color;
|
||||
newN->value = String(number);
|
||||
newN->callback = callBack;
|
||||
newN->id = cIndex;
|
||||
controls[cIndex] = newN;
|
||||
cIndex++;
|
||||
return cIndex - 1;
|
||||
}
|
||||
|
||||
void ESPUIClass::print(int id, String value) {
|
||||
if (id < cIndex && controls[id]->type == UI_LABEL) {
|
||||
controls[id]->value = value;
|
||||
@ -408,24 +443,6 @@ void ESPUIClass::print(String label, String value) {
|
||||
print(getIdByLabel(label), value);
|
||||
}
|
||||
|
||||
void ESPUIClass::updateSwitcher(int id, bool nValue, int clientId) {
|
||||
if (id < cIndex && controls[id]->type == UI_SWITCHER) {
|
||||
controls[id]->value = nValue ? 1 : 0;
|
||||
String json;
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.createObject();
|
||||
root["type"] = UPDATE_SWITCHER;
|
||||
root["value"] = nValue ? 1 : 0;
|
||||
root["id"] = String(id);
|
||||
root.printTo(json);
|
||||
textThem(json, clientId);
|
||||
} else {
|
||||
if (debug)
|
||||
Serial.println(String("Error: ") + String(id) +
|
||||
String(" is no switcher"));
|
||||
}
|
||||
}
|
||||
|
||||
void ESPUIClass::updateSlider(int id, int nValue, int clientId) {
|
||||
if (id < cIndex && controls[id]->type == UI_SLIDER) {
|
||||
controls[id]->value = nValue;
|
||||
@ -443,16 +460,55 @@ void ESPUIClass::updateSlider(int id, int nValue, int clientId) {
|
||||
}
|
||||
}
|
||||
|
||||
void ESPUIClass::updateSwitcher(int id, bool nValue, int clientId) {
|
||||
if (id < cIndex && controls[id]->type == UI_SWITCHER) {
|
||||
controls[id]->value = nValue ? 1 : 0;
|
||||
String json;
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.createObject();
|
||||
root["type"] = UPDATE_SWITCHER;
|
||||
root["value"] = nValue ? 1 : 0;
|
||||
root["id"] = String(id);
|
||||
root.printTo(json);
|
||||
textThem(json, clientId);
|
||||
} else {
|
||||
if (debug) Serial.println(String("Error: ") + String(id) + String(" is no switcher"));
|
||||
}
|
||||
}
|
||||
|
||||
void ESPUIClass::updateSwitcher(String label, bool nValue, int clientId) {
|
||||
if (!labelExists(label)) {
|
||||
if (debug)
|
||||
Serial.println("UI ERROR: Element does not " + String(label) +
|
||||
" exist, cannot update!");
|
||||
Serial.println("UI ERROR: Element does not " + String(label) + " exist, cannot update!");
|
||||
return;
|
||||
}
|
||||
updateSwitcher(getIdByLabel(label), nValue, clientId);
|
||||
}
|
||||
|
||||
void ESPUIClass::updateNumber(int id, int number, int clientId) {
|
||||
if (id < cIndex && controls[id]->type == UI_NUMBER) {
|
||||
controls[id]->value = number;
|
||||
String json;
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
JsonObject &root = jsonBuffer.createObject();
|
||||
root["type"] = UPDATE_NUMBER;
|
||||
root["value"] = String(number);
|
||||
root["id"] = String(id);
|
||||
root.printTo(json);
|
||||
textThem(json, clientId);
|
||||
} else {
|
||||
if (debug) Serial.println(String("Error: ") + String(id) + String(" is no number"));
|
||||
}
|
||||
}
|
||||
|
||||
void ESPUIClass::updateNumber(String label, int number, int clientId) {
|
||||
if (!labelExists(label)) {
|
||||
if (debug) Serial.println("UI ERROR: Element does not " + String(label) + " exist, cannot update!");
|
||||
return;
|
||||
}
|
||||
updateNumber(getIdByLabel(label), number, clientId);
|
||||
}
|
||||
|
||||
// This is a hacky workaround because ESPAsyncWebServer does not have a function
|
||||
// like this and it's clients array is private
|
||||
void ESPUIClass::textThem(String text, int clientId) {
|
||||
|
39
src/ESPUI.h
39
src/ESPUI.h
@ -39,7 +39,7 @@ typedef struct Control {
|
||||
unsigned int color;
|
||||
} Control;
|
||||
|
||||
// Types
|
||||
// Message Types (and control types)
|
||||
#define UI_TITEL 0
|
||||
#define UI_LABEL 1
|
||||
#define UI_BUTTON 2
|
||||
@ -50,6 +50,12 @@ typedef struct Control {
|
||||
#define UPDATE_SWITCHER 7
|
||||
#define UI_SLIDER 8
|
||||
#define UPDATE_SLIDER 9
|
||||
#define UI_NUMBER 10
|
||||
#define UPDATE_NUMBER 11
|
||||
#define UI_GRAPH 12
|
||||
#define CLEAR_GRAPH 13
|
||||
#define ADD_GRAPH_POINT 14
|
||||
|
||||
|
||||
// Values
|
||||
#define B_DOWN -1
|
||||
@ -70,7 +76,7 @@ typedef struct Control {
|
||||
#define S_INACTIVE 7
|
||||
|
||||
#define SL_VALUE 8
|
||||
|
||||
#define N_VALUE 9
|
||||
|
||||
// Colors
|
||||
#define COLOR_TURQUOISE 0
|
||||
@ -92,15 +98,16 @@ void beginSPIFFS(const char *_title); // Setup servers and page in SPIFFSmode
|
||||
void prepareFileSystem(); // Initially preps the filesystem and loads a lot of stuff into SPIFFS
|
||||
void list();
|
||||
// Creating Elements
|
||||
int label(const char *label, int color, String value = ""); // Create Label
|
||||
int button(const char *label, void (*callBack)(Control, int), int color,
|
||||
String value = ""); // Create Event Button
|
||||
int switcher(const char *label, bool startState,
|
||||
void (*callBack)(Control, int),
|
||||
int color); // Create Toggle Button
|
||||
int pad(const char *label, bool centerButton, void (*callBack)(Control, int),
|
||||
int color); // Create Pad Control
|
||||
int slider(const char *label, void (*callBack)(Control, int), int color, String value); // Create Slider Control
|
||||
|
||||
int button(const char *label, void (*callBack)(Control, int), int color, String value = ""); // Create Event Button
|
||||
int switcher(const char *label, bool startState, void (*callBack)(Control, int), int color); // Create Toggle Button
|
||||
int pad(const char *label, bool centerButton, void (*callBack)(Control, int), int color); // Create Pad Control
|
||||
int slider(const char *label, void (*callBack)(Control, int), int color, String value); // Create Slider Control
|
||||
int number(const char *label, void (*callBack)(Control, int), int color, int number, int min, int max); // Create a Number Input Control
|
||||
|
||||
// Output only
|
||||
int label(const char *label, int color, String value = ""); // Create Label
|
||||
int graph(const char *label, int color); // Create Graph display
|
||||
|
||||
// Update Elements
|
||||
void print(int id, String value);
|
||||
@ -112,6 +119,16 @@ void updateSwitcher(String label, bool nValue, int clientId = -1);
|
||||
void updateSlider(int id, int nValue, int clientId = -1);
|
||||
void updateSlider(String label, int nValue, int clientId = -1);
|
||||
|
||||
void updateNumber(int id, int nValue, int clientId = -1);
|
||||
void updateNumber(String label, int nValue, int clientId = -1);
|
||||
|
||||
void clearGraph(int id, int clientId = -1);
|
||||
void clearGraph(String label, int clientId = -1);
|
||||
|
||||
void addGraphPoint(int id, int nValue, int clientId = -1);
|
||||
void addGraphPoint(String label, int nValue, int clientId = -1);
|
||||
|
||||
|
||||
void textThem(String text, int clientId);
|
||||
|
||||
// Variables ---
|
||||
|
Loading…
Reference in New Issue
Block a user