mirror of
https://github.com/s00500/ESPUI.git
synced 2024-11-24 07:20:53 +00:00
Adding Button Type and callbacks
This commit is contained in:
parent
ccc1286c56
commit
332e9afbfa
@ -10,31 +10,34 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
|
|||||||
break;
|
break;
|
||||||
case WS_EVT_CONNECT:
|
case WS_EVT_CONNECT:
|
||||||
{
|
{
|
||||||
//Serial.printf("[WSc] Connected to url: %s\n", payload);
|
|
||||||
Serial.println("Connected");
|
Serial.println("Connected");
|
||||||
EasyUI.handleWebpage(client);
|
EasyUI.jsonDom(client);
|
||||||
Serial.println("JSON Data Sent to Client!");
|
Serial.println("JSON Data Sent to Client!");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case WS_EVT_DATA:
|
case WS_EVT_DATA:
|
||||||
Serial.print("WS Event");
|
Serial.print("WS Event");
|
||||||
String msg = "";
|
String msg = "";
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
msg += (char) data[i];
|
msg += (char) data[i];
|
||||||
}
|
}
|
||||||
Serial.println(msg);
|
Serial.println(msg);
|
||||||
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
StaticJsonBuffer<200> jsonBuffer;
|
JsonObject& root = jsonBuffer.parseObject(msg);
|
||||||
JsonObject& root = jsonBuffer.parseObject(data);
|
String type = root["type"];
|
||||||
String mode = root["mode"];
|
if(type == "t"){
|
||||||
if(mode == "check_tb_status"){
|
//Button Action
|
||||||
|
//TODO: get Button here
|
||||||
|
EasyUI.buttons[0]->callback();
|
||||||
//EasyUI.tbuttonStatus();
|
//EasyUI.tbuttonStatus();
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
else if(mode == "tb_click"){
|
else if(mode == "tb_click"){
|
||||||
String status = root["status"];
|
String status = root["status"];
|
||||||
String index = root["index"];
|
String index = root["index"];
|
||||||
//EasyUI.tbClick(index, status);
|
//EasyUI.tbClick(index, status);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,14 +64,15 @@ void EasyUIClass::toggleButton(uint8_t pin, const char* tbutton_label, int star
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
// Create a generic Button
|
// Create a generic Button
|
||||||
void EasyUIClass::button(uint8_t pin, const char* tbutton_label, int start_state, bool swap_state){
|
void EasyUIClass::button(const char* bLabel, void(* callBack)()){
|
||||||
//TODO: Implement
|
|
||||||
digitalWrite(pin, start_state);
|
|
||||||
tbutton_swap[tb_index] = swap_state;
|
|
||||||
tbutton_pinout[tb_index] = pin;
|
|
||||||
tbuttontitle[tb_index] = tbutton_label;
|
|
||||||
tb_index++;
|
|
||||||
|
|
||||||
|
//TODO: Implement
|
||||||
|
Button* newB = new Button();
|
||||||
|
|
||||||
|
newB->label = bLabel;
|
||||||
|
newB->callback = callBack;
|
||||||
|
buttons[bIndex] = newB;
|
||||||
|
bIndex++;
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
// Check Toggle Buttons States and Transfer to Webpage
|
// 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
|
// Convert & Transfer Arduino elements to JSON elements
|
||||||
void EasyUIClass::handleWebpage(AsyncWebSocketClient * client){
|
void EasyUIClass::jsonDom(AsyncWebSocketClient * client){
|
||||||
|
//SiteTitle
|
||||||
|
//TODO: emit here
|
||||||
// Labels
|
// Labels
|
||||||
for(int i=0; i<l_index; i++){
|
for(int i=0; i<l_index; i++){
|
||||||
String json;
|
String json;
|
||||||
@ -150,13 +156,13 @@ void EasyUIClass::handleWebpage(AsyncWebSocketClient * client){
|
|||||||
client->text(json);
|
client->text(json);
|
||||||
}
|
}
|
||||||
// Buttons
|
// Buttons
|
||||||
for(int i=0; i<tb_index; i++){
|
for(int i=0; i<bIndex; i++){
|
||||||
String json;
|
String json;
|
||||||
StaticJsonBuffer<200> jsonBuffer2;
|
StaticJsonBuffer<200> jsonBuffer2;
|
||||||
JsonObject& root2 = jsonBuffer2.createObject();
|
JsonObject& root2 = jsonBuffer2.createObject();
|
||||||
root2["type"] = "domButton";
|
root2["type"] = "domButton";
|
||||||
root2["index"] = String(i);
|
root2["index"] = String(i);
|
||||||
root2["label"] = tbuttontitle[i];
|
root2["label"] = buttons[i]->label;
|
||||||
root2.printTo(json);
|
root2.printTo(json);
|
||||||
client->text(json);
|
client->text(json);
|
||||||
}
|
}
|
||||||
|
25
src/EasyUI.h
25
src/EasyUI.h
@ -16,38 +16,39 @@
|
|||||||
#include <AsyncTCP.h>
|
#include <AsyncTCP.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
|
|
||||||
|
typedef struct Button
|
||||||
|
{
|
||||||
|
const char *label;
|
||||||
|
void (*callback)();
|
||||||
|
} Button;
|
||||||
|
|
||||||
class EasyUIClass{
|
class EasyUIClass{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void begin(); // Begin HTTP Server + WebSocketsServer & Initalize All Elements
|
void begin(); // Begin HTTP Server + WebSocketsServer & Initalize All Elements
|
||||||
void title(const char* _title); // Define Webpage Header Name and title
|
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 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 button(const char* tbutton_label, void(* callBack)()); // Create Event Button
|
||||||
void label(const char* label_name, const char* label_val); // Create Label
|
void label(const char* label_name, const char* label_val); // Create Label
|
||||||
// Variables ---
|
// Variables ---
|
||||||
const char* ui_title = "EasyUI"; // Store UI Title and Header Name
|
const char* ui_title = "EasyUI"; // Store UI Title and Header Name
|
||||||
int tb_index; // Calculate How Many Toggle Buttons
|
int bIndex; // How Many Buttons
|
||||||
int l_index; // Calculate How Many Labels
|
int l_index; // How Many Labels
|
||||||
|
|
||||||
bool tbutton_swap[10];
|
bool tbutton_swap[10];
|
||||||
uint8_t tbutton_pinout[10]; // Stores GPIO Values - MAX 10
|
Button* buttons[10];
|
||||||
const char* label_value[10]; // Stores Label Values - MAX 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* 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 webpage; // Coverts Arduino elements to JSON elements
|
||||||
String wsString = ""; // Stores Websockets Script
|
|
||||||
|
|
||||||
void tbClick(String _index, String _status);
|
void tbClick(String _index, String _status);
|
||||||
void tbuttonStatus();
|
void tbuttonStatus();
|
||||||
void handleWebpage(AsyncWebSocketClient * client);
|
void jsonDom(AsyncWebSocketClient * client);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
AsyncWebServer* server; // Create Unique Instance for Webserver
|
AsyncWebServer* server;
|
||||||
AsyncWebSocket* ws;
|
AsyncWebSocket* ws;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user