From 332e9afbfa162b401203262ed232d00bfdcc1af3 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Tue, 17 Oct 2017 01:10:29 +0200 Subject: [PATCH] Adding Button Type and callbacks --- src/EasyUI.cpp | 42 ++++++++++++++++++++++++------------------ src/EasyUI.h | 25 +++++++++++++------------ 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/EasyUI.cpp b/src/EasyUI.cpp index 543d0cd..92d05b1 100644 --- a/src/EasyUI.cpp +++ b/src/EasyUI.cpp @@ -10,31 +10,34 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT break; case WS_EVT_CONNECT: { - //Serial.printf("[WSc] Connected to url: %s\n", payload); Serial.println("Connected"); - EasyUI.handleWebpage(client); + EasyUI.jsonDom(client); Serial.println("JSON Data Sent to Client!"); } break; case WS_EVT_DATA: - Serial.print("WS Event"); + Serial.print("WS Event"); String msg = ""; for (size_t i = 0; i < len; i++) { msg += (char) data[i]; } Serial.println(msg); - - StaticJsonBuffer<200> jsonBuffer; - JsonObject& root = jsonBuffer.parseObject(data); - String mode = root["mode"]; - if(mode == "check_tb_status"){ + StaticJsonBuffer<200> jsonBuffer; + JsonObject& root = jsonBuffer.parseObject(msg); + String type = root["type"]; + if(type == "t"){ + //Button Action + //TODO: get Button here + EasyUI.buttons[0]->callback(); //EasyUI.tbuttonStatus(); } + /* else if(mode == "tb_click"){ String status = root["status"]; String index = root["index"]; //EasyUI.tbClick(index, status); } + */ break; } } @@ -61,14 +64,15 @@ void EasyUIClass::toggleButton(uint8_t pin, const char* tbutton_label, int star } */ // 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++; +void EasyUIClass::button(const char* bLabel, void(* callBack)()){ +//TODO: Implement + Button* newB = new Button(); + + newB->label = bLabel; + newB->callback = callBack; + buttons[bIndex] = newB; + bIndex++; } /* // Check Toggle Buttons States and Transfer to Webpage @@ -137,7 +141,9 @@ void EasyUIClass::tbClick(String _index, String _status){ */ // Convert & Transfer Arduino elements to JSON elements -void EasyUIClass::handleWebpage(AsyncWebSocketClient * client){ +void EasyUIClass::jsonDom(AsyncWebSocketClient * client){ + //SiteTitle + //TODO: emit here // Labels for(int i=0; itext(json); } // Buttons - for(int i=0; i jsonBuffer2; JsonObject& root2 = jsonBuffer2.createObject(); root2["type"] = "domButton"; root2["index"] = String(i); - root2["label"] = tbuttontitle[i]; + root2["label"] = buttons[i]->label; root2.printTo(json); client->text(json); } diff --git a/src/EasyUI.h b/src/EasyUI.h index 2e6bf45..16b02dd 100644 --- a/src/EasyUI.h +++ b/src/EasyUI.h @@ -16,38 +16,39 @@ #include #include +typedef struct Button +{ + const char *label; + void (*callback)(); +} Button; class EasyUIClass{ public: void begin(); // Begin HTTP Server + WebSocketsServer & Initalize All Elements void title(const char* _title); // Define Webpage Header Name and title - void toggleButton(uint8_t pin, const char* tbutton_label, int start_state = 0, bool swap_state = false); // Create Toggle Button - void button(uint8_t pin, const char* tbutton_label, int start_state = 0, bool swap_state = false); // Create Toggle Button + //void toggleButton(uint8_t pin, const char* tbutton_label, int start_state = 0, bool swap_state = false); // Create Toggle Button + void button(const char* tbutton_label, void(* callBack)()); // Create Event Button void label(const char* label_name, const char* label_val); // Create Label // Variables --- const char* ui_title = "EasyUI"; // Store UI Title and Header Name - int tb_index; // Calculate How Many Toggle Buttons - int l_index; // Calculate How Many Labels + int bIndex; // How Many Buttons + int l_index; // How Many Labels bool tbutton_swap[10]; - uint8_t tbutton_pinout[10]; // Stores GPIO Values - MAX 10 - const char* label_value[10]; // Stores Label Values - MAX 10 + Button* buttons[10]; - const char* tbuttontitle[10]; // Stores Toggle Button Titles - MAX 10 + const char* label_value[10]; // Stores Label Values - MAX 10 const char* label_title[10]; // Stores Label Titles - MAX 10 - // const char* variable_type[10]; // un-used feature for now // Stores Label Types, Like 'C' , 'F' or '%' - MAX 10 - String webpage; // Coverts Arduino elements to JSON elements - String wsString = ""; // Stores Websockets Script void tbClick(String _index, String _status); void tbuttonStatus(); - void handleWebpage(AsyncWebSocketClient * client); + void jsonDom(AsyncWebSocketClient * client); private: - AsyncWebServer* server; // Create Unique Instance for Webserver + AsyncWebServer* server; AsyncWebSocket* ws; };