1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-06-16 07:53:32 +00:00
ESPUI/src/EasyUI.cpp

191 lines
5.0 KiB
C++
Raw Normal View History

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
{
2017-10-16 13:00:53 +00:00
//Serial.printf("[WSc] Connected to url: %s\n", payload);
2017-05-18 22:05:32 +00:00
Serial.println("Connected");
2017-10-16 22:10:48 +00:00
EasyUI.handleWebpage(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
Serial.print("WS Event");
String msg = "";
for (size_t i = 0; i < len; i++) {
msg += (char) data[i];
}
Serial.println(msg);
2017-05-18 22:05:32 +00:00
StaticJsonBuffer<200> jsonBuffer;
2017-10-16 13:00:53 +00:00
JsonObject& root = jsonBuffer.parseObject(data);
2017-05-18 22:05:32 +00:00
String mode = root["mode"];
if(mode == "check_tb_status"){
2017-10-16 22:10:48 +00:00
//EasyUI.tbuttonStatus();
2017-05-18 22:05:32 +00:00
}
else if(mode == "tb_click"){
String status = root["status"];
String index = root["index"];
2017-10-16 22:10:48 +00:00
//EasyUI.tbClick(index, status);
2017-05-18 22:05:32 +00:00
}
break;
}
}
2017-10-16 22:10:48 +00:00
2017-05-18 22:05:32 +00:00
void EasyUIClass::title(const char* _title){
ui_title = _title;
}
// Create Labels
void EasyUIClass::label(const char* label_name,const char* label_val){
label_value[l_index] = label_val;
label_title[l_index] = label_name;
l_index++;
}
2017-10-16 13:00:53 +00:00
/*
2017-05-18 22:05:32 +00:00
// Create Toggle Buttons
void EasyUIClass::toggleButton(uint8_t pin, const char* tbutton_label, int start_state, bool swap_state){
pinMode(pin, OUTPUT);
digitalWrite(pin, start_state);
tbutton_swap[tb_index] = swap_state;
tbutton_pinout[tb_index] = pin;
tbuttontitle[tb_index] = tbutton_label;
tb_index++;
}
2017-10-16 13:00:53 +00:00
*/
// Create a generic Button
void EasyUIClass::button(uint8_t pin, const char* tbutton_label, int start_state, bool swap_state){
//TODO: Implement
digitalWrite(pin, start_state);
tbutton_swap[tb_index] = swap_state;
tbutton_pinout[tb_index] = pin;
tbuttontitle[tb_index] = tbutton_label;
tb_index++;
2017-05-18 22:05:32 +00:00
2017-10-16 13:00:53 +00:00
}
/*
2017-05-18 22:05:32 +00:00
// Check Toggle Buttons States and Transfer to Webpage
void EasyUIClass::tbuttonStatus(){
String json;
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["mode"] = "t_button_startup";
root["index"] = tb_index;
for(int i=0; i<tb_index; i++){
String name = "tb"+String(i);
int stat = digitalRead(tbutton_pinout[i]);
if(tbutton_swap[i]){
if(stat == HIGH)
root[name] = "0";
else if(stat == LOW)
root[name] = "1";
else
root[name] = "unknown";
}
else{
if(stat == HIGH)
root[name] = "1";
else if(stat == LOW)
root[name] = "0";
else
root[name] = "unknown";
}
}
root.printTo(json);
webSocket->broadcastTXT(json);
}
2017-10-16 13:00:53 +00:00
*/
2017-05-18 22:05:32 +00:00
2017-10-16 13:00:53 +00:00
/*
2017-05-18 22:05:32 +00:00
// Handle Toggle Button Click Response
void EasyUIClass::tbClick(String _index, String _status){
String json;
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
String name = "tb"+_index;
root["mode"] = "t_button_click";
root["index"] = _index;
if(_status == "on"){
root[name] = "1";
root.printTo(json);
webSocket->broadcastTXT(json);
if(tbutton_swap[_index.toInt()]){
digitalWrite(tbutton_pinout[_index.toInt()], LOW);
}else{
digitalWrite(tbutton_pinout[_index.toInt()], HIGH);
}
}
else if(_status == "off"){
root[name] = "0";
root.printTo(json);
webSocket->broadcastTXT(json);
if(tbutton_swap[_index.toInt()]){
digitalWrite(tbutton_pinout[_index.toInt()], HIGH);
}else{
digitalWrite(tbutton_pinout[_index.toInt()], LOW);
}
}
}
2017-10-16 13:00:53 +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
void EasyUIClass::handleWebpage(AsyncWebSocketClient * client){
// Labels
2017-05-18 22:05:32 +00:00
for(int i=0; i<l_index; i++){
String json;
StaticJsonBuffer<200> jsonBuffer1;
JsonObject& root1 = jsonBuffer1.createObject();
2017-10-16 22:10:48 +00:00
root1["type"] = "domLabel";
2017-05-18 22:05:32 +00:00
root1["l_title"] = String(label_title[i]);
2017-10-16 22:10:48 +00:00
root1["label"] = String(label_value[i]);
2017-05-18 22:05:32 +00:00
root1.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
// Buttons
2017-05-18 22:05:32 +00:00
for(int i=0; i<tb_index; i++){
String json;
StaticJsonBuffer<200> jsonBuffer2;
JsonObject& root2 = jsonBuffer2.createObject();
2017-10-16 22:10:48 +00:00
root2["type"] = "domButton";
2017-05-18 22:05:32 +00:00
root2["index"] = String(i);
2017-10-16 22:10:48 +00:00
root2["label"] = tbuttontitle[i];
2017-05-18 22:05:32 +00:00
root2.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
void EasyUIClass::begin(){
2017-10-16 22:10:48 +00:00
server = new AsyncWebServer(80);
ws = new AsyncWebSocket("/ws");
SPIFFS.begin(false);
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;