2017-05-18 22:05:32 +00:00
|
|
|
#include "EasyUI.h"
|
2017-10-16 13:00:53 +00:00
|
|
|
#include <ESPAsyncWebServer.h>
|
2017-05-18 22:05:32 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2017-10-16 13:00:53 +00:00
|
|
|
// Handle Websockets Communication
|
|
|
|
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
|
2017-05-18 22:05:32 +00:00
|
|
|
switch(type) {
|
2017-10-16 13:00:53 +00:00
|
|
|
case WS_EVT_DISCONNECT:
|
2017-05-18 22:05:32 +00:00
|
|
|
Serial.printf("Disconnected!\n");
|
|
|
|
break;
|
2017-10-16 13:00:53 +00:00
|
|
|
case WS_EVT_CONNECT:
|
2017-05-18 22:05:32 +00:00
|
|
|
{
|
|
|
|
Serial.println("Connected");
|
2017-10-16 23:10:29 +00:00
|
|
|
EasyUI.jsonDom(client);
|
2017-05-18 22:05:32 +00:00
|
|
|
Serial.println("JSON Data Sent to Client!");
|
|
|
|
}
|
|
|
|
break;
|
2017-10-16 13:00:53 +00:00
|
|
|
case WS_EVT_DATA:
|
2017-10-16 22:10:48 +00:00
|
|
|
String msg = "";
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
msg += (char) data[i];
|
|
|
|
}
|
2017-10-19 11:46:47 +00:00
|
|
|
if(msg.startsWith("bdown:")){
|
|
|
|
EasyUI.controls[msg.substring(6).toInt()]->callback(msg.substring(6).toInt(), B_DOWN);
|
|
|
|
}else if(msg.startsWith("bup:")){
|
|
|
|
EasyUI.controls[msg.substring(4).toInt()]->callback(msg.substring(4).toInt(), B_UP);
|
|
|
|
}else if(msg.startsWith("pfdown:")){
|
|
|
|
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_FOR_DOWN);
|
|
|
|
}else if(msg.startsWith("pfup:")){
|
|
|
|
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_FOR_UP);
|
|
|
|
}else if(msg.startsWith("pldown:")){
|
|
|
|
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_LEFT_DOWN);
|
|
|
|
}else if(msg.startsWith("plup:")){
|
|
|
|
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_LEFT_UP);
|
|
|
|
}else if(msg.startsWith("prdown:")){
|
|
|
|
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_RIGHT_DOWN);
|
|
|
|
}else if(msg.startsWith("prup:")){
|
|
|
|
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_RIGHT_UP);
|
|
|
|
}else if(msg.startsWith("pbdown:")){
|
|
|
|
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_BACK_DOWN);
|
|
|
|
}else if(msg.startsWith("pbup:")){
|
|
|
|
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_BACK_UP);
|
|
|
|
}else if(msg.startsWith("pcdown:")){
|
|
|
|
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_CENTER_DOWN);
|
|
|
|
}else if(msg.startsWith("pcup:")){
|
|
|
|
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_CENTER_UP);
|
2017-10-19 15:30:32 +00:00
|
|
|
}else if(msg.startsWith("sactive:")){
|
|
|
|
EasyUI.updateSwitcher(msg.substring(8).toInt(), true);
|
|
|
|
EasyUI.controls[msg.substring(8).toInt()]->callback(msg.substring(8).toInt(), S_ACTIVE);
|
|
|
|
}else if(msg.startsWith("sinactive:")){
|
|
|
|
EasyUI.updateSwitcher(msg.substring(10).toInt(), false);
|
|
|
|
EasyUI.controls[msg.substring(10).toInt()]->callback(msg.substring(10).toInt(), S_INACTIVE);
|
2017-10-19 11:46:47 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-16 22:10:48 +00:00
|
|
|
|
2017-10-19 15:30:32 +00:00
|
|
|
void EasyUIClass::label(const char* label, String value){
|
2017-10-19 11:46:47 +00:00
|
|
|
Control* newL = new Control();
|
|
|
|
newL->type = UI_LABEL;
|
|
|
|
newL->label = label;
|
2017-10-19 15:30:32 +00:00
|
|
|
if(value != "") newL->value = value; // Init with labeltext
|
|
|
|
else newL->value = String(label);
|
2017-10-19 11:46:47 +00:00
|
|
|
newL->callback = NULL;
|
|
|
|
controls[cIndex] = newL;
|
|
|
|
cIndex++;
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 11:46:47 +00:00
|
|
|
void EasyUIClass::button(const char* label, void(* callBack)(int, int)){
|
|
|
|
Control* newB = new Control();
|
|
|
|
newB->type = UI_BUTTON;
|
|
|
|
newB->label = label;
|
2017-10-16 23:10:29 +00:00
|
|
|
newB->callback = callBack;
|
2017-10-19 11:46:47 +00:00
|
|
|
controls[cIndex] = newB;
|
|
|
|
cIndex++;
|
2017-10-16 13:00:53 +00:00
|
|
|
}
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2017-10-19 15:30:32 +00:00
|
|
|
void EasyUIClass::switcher(const char* label, bool startState, void(* callBack)(int, int)){
|
2017-10-19 11:46:47 +00:00
|
|
|
Control* newS = new Control();
|
|
|
|
newS->type = UI_SWITCHER;
|
|
|
|
newS->label = label;
|
2017-10-19 15:30:32 +00:00
|
|
|
newS->value = String(startState);
|
2017-10-19 11:46:47 +00:00
|
|
|
newS->callback = callBack;
|
|
|
|
controls[cIndex] = newS;
|
|
|
|
cIndex++;
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 11:46:47 +00:00
|
|
|
void EasyUIClass::pad(const char* label, bool center, void(* callBack)(int, int)){
|
|
|
|
Control* newP = new Control();
|
|
|
|
if(center)newP->type = UI_CPAD;
|
|
|
|
else newP->type = UI_PAD;
|
|
|
|
newP->label = label;
|
|
|
|
newP->callback = callBack;
|
|
|
|
controls[cIndex] = newP;
|
|
|
|
cIndex++;
|
|
|
|
}
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2017-10-19 15:30:32 +00:00
|
|
|
void EasyUIClass::print(int id, String value){
|
|
|
|
if(id<cIndex && controls[id]->type == UI_LABEL){
|
|
|
|
controls[id]->value = value;
|
2017-10-19 11:46:47 +00:00
|
|
|
String json;
|
|
|
|
StaticJsonBuffer<200> jsonBuffer;
|
|
|
|
JsonObject& root = jsonBuffer.createObject();
|
|
|
|
root["type"] = UPDATE_LABEL;
|
2017-10-19 15:30:32 +00:00
|
|
|
root["value"] = value;
|
|
|
|
root["id"] = String(id);
|
2017-05-18 22:05:32 +00:00
|
|
|
root.printTo(json);
|
2017-10-19 11:46:47 +00:00
|
|
|
this->ws->textAll(json);
|
2017-05-18 22:05:32 +00:00
|
|
|
}else{
|
2017-10-19 15:30:32 +00:00
|
|
|
Serial.println(String("Error: ")+ String(id) +String(" is no label"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EasyUIClass::updateSwitcher(int id, bool nValue){
|
|
|
|
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_SWITCH;
|
|
|
|
root["value"] = nValue?1:0;
|
|
|
|
root["id"] = String(id);
|
|
|
|
root.printTo(json);
|
|
|
|
this->ws->textAll(json);
|
|
|
|
}else{
|
|
|
|
Serial.println(String("Error: ")+ String(id) +String(" is no switcher"));
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2017-10-16 22:10:48 +00:00
|
|
|
// Convert & Transfer Arduino elements to JSON elements
|
2017-10-16 23:10:29 +00:00
|
|
|
void EasyUIClass::jsonDom(AsyncWebSocketClient * client){
|
2017-10-19 11:46:47 +00:00
|
|
|
for(int i=-1; i<cIndex; i++){
|
2017-05-18 22:05:32 +00:00
|
|
|
String json;
|
2017-10-19 11:46:47 +00:00
|
|
|
StaticJsonBuffer<200> jsonBuffer;
|
|
|
|
JsonObject& root = jsonBuffer.createObject();
|
|
|
|
if(i == -1){
|
|
|
|
root["type"] = UI_TITEL;
|
|
|
|
root["label"] = String(ui_title);
|
|
|
|
}else{
|
|
|
|
root["type"] = controls[i]->type;
|
|
|
|
root["label"] = String(controls[i]->label);
|
2017-10-19 15:30:32 +00:00
|
|
|
root["value"] = String(controls[i]->value);
|
2017-10-19 11:46:47 +00:00
|
|
|
root["id"] = String(i);
|
|
|
|
}
|
|
|
|
root.printTo(json);
|
2017-10-16 22:10:48 +00:00
|
|
|
client->text(json);
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-16 22:10:48 +00:00
|
|
|
|
2017-05-18 22:05:32 +00:00
|
|
|
|
2017-10-19 11:46:47 +00:00
|
|
|
void EasyUIClass::begin(const char * _title){
|
|
|
|
ui_title = _title;
|
2017-10-16 22:10:48 +00:00
|
|
|
server = new AsyncWebServer(80);
|
|
|
|
ws = new AsyncWebSocket("/ws");
|
2017-10-19 11:46:47 +00:00
|
|
|
SPIFFS.begin();
|
2017-10-16 22:10:48 +00:00
|
|
|
ws->onEvent(onWsEvent);
|
|
|
|
server->addHandler(ws);
|
|
|
|
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
|
2017-10-16 13:00:53 +00:00
|
|
|
|
|
|
|
//Heap for general Servertest
|
|
|
|
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send(200, "text/plain", String(ESP.getFreeHeap()));
|
|
|
|
});
|
|
|
|
|
|
|
|
server->onNotFound([](AsyncWebServerRequest *request){
|
|
|
|
request->send(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
server->begin();
|
|
|
|
Serial.println("UI Initialized");
|
2017-05-18 22:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EasyUIClass EasyUI;
|