1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-24 12:20:55 +00:00

Refactoring to ESPUI

This commit is contained in:
Lukas Bachschwell 2017-10-19 17:43:39 +02:00
parent dbbaad3f4f
commit a072a162c1
2 changed files with 31 additions and 31 deletions

View File

@ -1,4 +1,4 @@
#include "EasyUI.h" #include "ESPUI.h"
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <functional> #include <functional>
@ -11,7 +11,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
case WS_EVT_CONNECT: case WS_EVT_CONNECT:
{ {
Serial.println("Connected"); Serial.println("Connected");
EasyUI.jsonDom(client); ESPUI.jsonDom(client);
Serial.println("JSON Data Sent to Client!"); Serial.println("JSON Data Sent to Client!");
} }
break; break;
@ -21,41 +21,41 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
msg += (char) data[i]; msg += (char) data[i];
} }
if(msg.startsWith("bdown:")){ if(msg.startsWith("bdown:")){
EasyUI.controls[msg.substring(6).toInt()]->callback(msg.substring(6).toInt(), B_DOWN); ESPUI.controls[msg.substring(6).toInt()]->callback(msg.substring(6).toInt(), B_DOWN);
}else if(msg.startsWith("bup:")){ }else if(msg.startsWith("bup:")){
EasyUI.controls[msg.substring(4).toInt()]->callback(msg.substring(4).toInt(), B_UP); ESPUI.controls[msg.substring(4).toInt()]->callback(msg.substring(4).toInt(), B_UP);
}else if(msg.startsWith("pfdown:")){ }else if(msg.startsWith("pfdown:")){
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_FOR_DOWN); ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_FOR_DOWN);
}else if(msg.startsWith("pfup:")){ }else if(msg.startsWith("pfup:")){
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_FOR_UP); ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_FOR_UP);
}else if(msg.startsWith("pldown:")){ }else if(msg.startsWith("pldown:")){
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_LEFT_DOWN); ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_LEFT_DOWN);
}else if(msg.startsWith("plup:")){ }else if(msg.startsWith("plup:")){
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_LEFT_UP); ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_LEFT_UP);
}else if(msg.startsWith("prdown:")){ }else if(msg.startsWith("prdown:")){
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_RIGHT_DOWN); ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_RIGHT_DOWN);
}else if(msg.startsWith("prup:")){ }else if(msg.startsWith("prup:")){
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_RIGHT_UP); ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_RIGHT_UP);
}else if(msg.startsWith("pbdown:")){ }else if(msg.startsWith("pbdown:")){
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_BACK_DOWN); ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_BACK_DOWN);
}else if(msg.startsWith("pbup:")){ }else if(msg.startsWith("pbup:")){
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_BACK_UP); ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_BACK_UP);
}else if(msg.startsWith("pcdown:")){ }else if(msg.startsWith("pcdown:")){
EasyUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_CENTER_DOWN); ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_CENTER_DOWN);
}else if(msg.startsWith("pcup:")){ }else if(msg.startsWith("pcup:")){
EasyUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_CENTER_UP); ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_CENTER_UP);
}else if(msg.startsWith("sactive:")){ }else if(msg.startsWith("sactive:")){
EasyUI.updateSwitcher(msg.substring(8).toInt(), true); ESPUI.updateSwitcher(msg.substring(8).toInt(), true);
EasyUI.controls[msg.substring(8).toInt()]->callback(msg.substring(8).toInt(), S_ACTIVE); ESPUI.controls[msg.substring(8).toInt()]->callback(msg.substring(8).toInt(), S_ACTIVE);
}else if(msg.startsWith("sinactive:")){ }else if(msg.startsWith("sinactive:")){
EasyUI.updateSwitcher(msg.substring(10).toInt(), false); ESPUI.updateSwitcher(msg.substring(10).toInt(), false);
EasyUI.controls[msg.substring(10).toInt()]->callback(msg.substring(10).toInt(), S_INACTIVE); ESPUI.controls[msg.substring(10).toInt()]->callback(msg.substring(10).toInt(), S_INACTIVE);
} }
break; break;
} }
} }
void EasyUIClass::label(const char* label, String value){ void ESPUIClass::label(const char* label, String value){
Control* newL = new Control(); Control* newL = new Control();
newL->type = UI_LABEL; newL->type = UI_LABEL;
newL->label = label; newL->label = label;
@ -66,7 +66,7 @@ void EasyUIClass::label(const char* label, String value){
cIndex++; cIndex++;
} }
void EasyUIClass::button(const char* label, void(* callBack)(int, int)){ void ESPUIClass::button(const char* label, void(* callBack)(int, int)){
Control* newB = new Control(); Control* newB = new Control();
newB->type = UI_BUTTON; newB->type = UI_BUTTON;
newB->label = label; newB->label = label;
@ -75,7 +75,7 @@ void EasyUIClass::button(const char* label, void(* callBack)(int, int)){
cIndex++; cIndex++;
} }
void EasyUIClass::switcher(const char* label, bool startState, void(* callBack)(int, int)){ void ESPUIClass::switcher(const char* label, bool startState, void(* callBack)(int, int)){
Control* newS = new Control(); Control* newS = new Control();
newS->type = UI_SWITCHER; newS->type = UI_SWITCHER;
newS->label = label; newS->label = label;
@ -85,7 +85,7 @@ void EasyUIClass::switcher(const char* label, bool startState, void(* callBack)(
cIndex++; cIndex++;
} }
void EasyUIClass::pad(const char* label, bool center, void(* callBack)(int, int)){ void ESPUIClass::pad(const char* label, bool center, void(* callBack)(int, int)){
Control* newP = new Control(); Control* newP = new Control();
if(center)newP->type = UI_CPAD; if(center)newP->type = UI_CPAD;
else newP->type = UI_PAD; else newP->type = UI_PAD;
@ -95,7 +95,7 @@ void EasyUIClass::pad(const char* label, bool center, void(* callBack)(int, int)
cIndex++; cIndex++;
} }
void EasyUIClass::print(int id, String value){ void ESPUIClass::print(int id, String value){
if(id<cIndex && controls[id]->type == UI_LABEL){ if(id<cIndex && controls[id]->type == UI_LABEL){
controls[id]->value = value; controls[id]->value = value;
String json; String json;
@ -111,7 +111,7 @@ void EasyUIClass::print(int id, String value){
} }
} }
void EasyUIClass::updateSwitcher(int id, bool nValue){ void ESPUIClass::updateSwitcher(int id, bool nValue){
if(id<cIndex && controls[id]->type == UI_SWITCHER){ if(id<cIndex && controls[id]->type == UI_SWITCHER){
controls[id]->value = nValue?1:0; controls[id]->value = nValue?1:0;
String json; String json;
@ -129,7 +129,7 @@ void EasyUIClass::updateSwitcher(int id, bool nValue){
// Convert & Transfer Arduino elements to JSON elements // Convert & Transfer Arduino elements to JSON elements
void EasyUIClass::jsonDom(AsyncWebSocketClient * client){ void ESPUIClass::jsonDom(AsyncWebSocketClient * client){
for(int i=-1; i<cIndex; i++){ for(int i=-1; i<cIndex; i++){
String json; String json;
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;
@ -149,7 +149,7 @@ void EasyUIClass::jsonDom(AsyncWebSocketClient * client){
} }
void EasyUIClass::begin(const char * _title){ void ESPUIClass::begin(const char * _title){
ui_title = _title; ui_title = _title;
server = new AsyncWebServer(80); server = new AsyncWebServer(80);
ws = new AsyncWebSocket("/ws"); ws = new AsyncWebSocket("/ws");
@ -171,4 +171,4 @@ void EasyUIClass::begin(const char * _title){
Serial.println("UI Initialized"); Serial.println("UI Initialized");
} }
EasyUIClass EasyUI; ESPUIClass ESPUI;

View File

@ -1,5 +1,5 @@
#ifndef EasyUI_h #ifndef ESPUI_h
#define EasyUI_h #define ESPUI_h
#define HARDWARE "esp32" #define HARDWARE "esp32"
@ -53,7 +53,7 @@ typedef struct Control
class EasyUIClass{ class ESPUIClass{
public: public:
void begin(const char* _title); // Setup servers and page void begin(const char* _title); // Setup servers and page
@ -79,5 +79,5 @@ private:
AsyncWebSocket* ws; AsyncWebSocket* ws;
}; };
extern EasyUIClass EasyUI; extern ESPUIClass ESPUI;
#endif #endif