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

Adding Labels and Pads

This commit is contained in:
Lukas Bachschwell 2017-10-19 13:46:47 +02:00
parent 139de283a9
commit a258eab72b
6 changed files with 388 additions and 197 deletions

View File

@ -295,6 +295,7 @@
/* Lists
*/
/*
ul {
list-style: circle inside; }
ol {
@ -310,7 +311,7 @@
font-size: 90%; }
li {
margin-bottom: 1rem; }
*/
/* Code
*/
@ -398,3 +399,85 @@
content: "";
display: table;
clear: both; }
/* ButtonPad
*/
.control {
background-color: #ddd;
background-image: linear-gradient(hsla(0,0%,0%,.1), hsla(0,0%,100%,.1));
border-radius: 50%;
box-shadow: inset 0 1px 1px 1px hsla(0,0%,100%,.5),
0 0 1px 1px hsla(0,0%,100%,.75),
0 0 1px 2px hsla(0,0%,100%,.25),
0 0 1px 3px hsla(0,0%,100%,.25),
0 0 1px 4px hsla(0,0%,100%,.25),
0 0 1px 6px hsla(0,0%,0%,.75);
height: 9em;
margin: 3em auto;
position: relative;
width: 9em;
}
.control ul {
height: 100%;
padding: 0;
transform: rotate(45deg);
}
.control li {
border-radius: 100% 0 0 0;
box-shadow: inset -1px -1px 1px hsla(0,0%,100%,.5),
0 0 1px hsla(0,0%,0%,.75);
display: inline-block;
height: 50%;
overflow: hidden;
width: 50%;
}
.control ul li:nth-child(2) {
transform: rotate(90deg);
}
.control ul li:nth-child(3) {
transform: rotate(-90deg);
}
.control ul li:nth-child(4) {
transform: rotate(180deg);
}
.control ul a {
height: 200%;
position: relative;
transform: rotate(-45deg);
width: 200%;
}
.control a:hover,
.control a:focus {
background-color: hsla(0,0%,100%,.25);
}
.control a {
border-radius: 50%;
color: #333;
display: block;
font: bold 1em/3 sans-serif;
text-align: center;
text-decoration: none;
text-shadow: 0 1px 1px hsla(0,0%,100%,.4);
transition: .15s;
}
.control .confirm {
background-color: #ddd;
background-image: linear-gradient(hsla(0,0%,0%,.15), hsla(0,0%,100%,.25));
box-shadow: inset 0 1px 1px 1px hsla(0,0%,100%,.5),
0 0 1px 1px hsla(0,0%,100%,.25),
0 0 1px 2px hsla(0,0%,100%,.25),
0 0 1px 3px hsla(0,0%,100%,.25),
0 0 1px 4px hsla(0,0%,100%,.25),
0 0 1px 6px hsla(0,0%,0%,.85);
left: 50%;
line-height: 3;
margin: -1.5em;
position: absolute;
top: 50%;
width: 3em;
}
.control .confirm:hover,
.control .confirm:focus {
background-color: #eee;
}

View File

@ -5,17 +5,18 @@
<title>Control</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAPFBMVEUAAACA1VWR21qQ2liR3FqR3FqS3VuR3VqR3VuR3VqO21mS21uS3FqS3FqS21uJ2GKQ21qR3FuR3FoAAAB/3Gu7AAAAEnRSTlMABoA3kPBwz8i5Kzioxg4NVcU3uEJHAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAAN1wAADdcBQiibeAAAAAd0SU1FB+EFEhcEM+HpYwQAAABYSURBVBjThY/JDsAgCESt4lpX/v9jLQZJ6qF9t3khAyj1xXUKbQ4BVowDwqOYgExkkW4iY6lPaF06RqM8YItOuRbMaz6xjbsusDAW/drplBg47jP696cXE8bPA1eUDeK2AAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE3LTA1LTE4VDIzOjA0OjUxKzAyOjAwxE59ewAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNy0wNS0xOFQyMzowNDo1MSswMjowMLUTxccAAAAZdEVYdFNvZnR3YXJlAHd3dy5pbmtzY2FwZS5vcmeb7jwaAAAAAElFTkSuQmCC"/>
<link rel="stylesheet" href="/css/normalize.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body onload="javascript:start();">
<div><h4 id="mainHeader">Control<span id="conStatus" class="label">Offline</span></h4></div>
<div><h4><div id="mainHeader">Control</div> <span id="conStatus" class="label">Offline</span></h4></div>
<hr />
<div class="container">
<div id="row" class="row u-full-width">
</div>
</div>
<script src="/js/zepto.js"></script>
<script src="/js/controls.js"></script>
</body>

View File

@ -1,9 +1,20 @@
// TASKS:
// Roadmap
// Build changeable Labels
// Roadmap:
// Build a serial Console?
const UI_TITEL = 0;
const UI_LABEL = 1;
const UI_BUTTON = 2;
const UI_SWITCHER = 3;
const UI_PAD = 4;
const UI_CPAD = 5;
const UPDATE_LABEL = 6;
const FOR = 0;
const BACK = 1;
const LEFT = 2;
const RIGHT = 3;
const CENTER = 4;
var websock;
function start() {
websock = new WebSocket('ws://' + window.location.hostname + '/ws');
@ -27,29 +38,88 @@ function start() {
websock.onmessage = function(evt) {
console.log(evt);
var data = JSON.parse(evt.data);
console.log(data);
var e = document.body;
if (data.type === 'domButton') {
//initial rendering of the Buttons
$('#row').append("<div class='two columns card'><h5>"+data.label+"</h5><hr/><button onClick='buttonclick(this)'' id="+data.id+"></button></div>");
}
if (data.type === 'domLabel') {
//initial rendering of the Labels
$('#row').append("<div class='two columns card'><h5>"+data.l_title+"</h5><hr /><h3><span class='label'>"+data.label+"</span></h3></div>");
}
else if (data.type === 'value') {
// Display some kind of value
}
else {
console.log('unknown event');
var center = "";
switch(data.type){
case UI_TITEL:
document.title = data.label;
$('#mainHeader').html(data.label);
break;
case UI_LABEL:
$('#row').append("<div class='two columns card'><h5 id='"+data.id+"'>"+data.label+"</h5></div>");
break;
case UI_BUTTON:
$('#row').append("<div class='two columns card'><h5>"+data.label+"</h5><hr/><button onmousedown='buttonclick("+data.id+", true)' onmouseup='buttonclick("+data.id+", false)' id='"+data.id+"'></button></div>");
$('#'+data.id).on({ 'touchstart' : function(e){e.preventDefault(); buttonclick(data.id, true) } });
$('#'+data.id).on({ 'touchend' : function(e){e.preventDefault(); buttonclick(data.id, false) } });
break;
case UI_CPAD:
center = "<a class='confirm' onmousedown='padclick(CENTER, "+data.id+", true)' onmouseup='padclick(CENTER, "+data.id+", false)' href='#' id='c"+data.id+"'>OK</a>";
//NO BREAK
case UI_PAD:
$('#row').append(
"<div class='two columns card'><h5>"+data.label+"</h5><hr/>"+
"<nav class='control'>"+
"<ul>"+
"<li><a onmousedown='padclick(FOR, "+data.id+", true)' onmouseup='padclick(FOR, "+data.id+", false)' href='#' id='f"+data.id+"'>▲</a></li>" +
"<li><a onmousedown='padclick(RIGHT, "+data.id+", true)' onmouseup='padclick(RIGHT, "+data.id+", false)' href='#' id='r"+data.id+"'>▲</a></li>" +
"<li><a onmousedown='padclick(LEFT, "+data.id+", true)' onmouseup='padclick(LEFT, "+data.id+", false)' href='#' id='l"+data.id+"'>▲</a></li>" +
"<li><a onmousedown='padclick(BACK, "+data.id+", true)' onmouseup='padclick(BACK, "+data.id+", false)' href='#' id='b"+data.id+"'>▲</a></li>" +
"</ul>"+
center +
"</nav>"+
"</div>");
$('#f'+data.id).on({ 'touchstart' : function(e){e.preventDefault(); padclick(FOR, data.id, true) } });
$('#f'+data.id).on({ 'touchend' : function(e){e.preventDefault(); padclick(FOR, data.id, false) } });
$('#l'+data.id).on({ 'touchstart' : function(e){e.preventDefault(); padclick(LEFT, data.id, true) } });
$('#l'+data.id).on({ 'touchend' : function(e){e.preventDefault(); padclick(LEFT, data.id, false) } });
$('#r'+data.id).on({ 'touchstart' : function(e){e.preventDefault(); padclick(RIGHT, data.id, true) } });
$('#r'+data.id).on({ 'touchend' : function(e){e.preventDefault(); padclick(RIGHT, data.id, false) } });
$('#b'+data.id).on({ 'touchstart' : function(e){e.preventDefault(); padclick(BACK, data.id, true) } });
$('#b'+data.id).on({ 'touchend' : function(e){e.preventDefault(); padclick(BACK,data.id, false) } });
$('#c'+data.id).on({ 'touchstart' : function(e){e.preventDefault(); padclick(CENTER, data.id, true) } });
$('#c'+data.id).on({ 'touchend' : function(e){e.preventDefault(); padclick(CENTER,data.id, false) } });
break;
case UPDATE_LABEL:
$('#'+data.id).html(data.label);
break;
default:
console.error('Unknown type or event');
break;
}
};
}
function buttonclick(e) {
websock.send("bdown:"+e.id);
/*
if release
websock.send("bup:"+e.id);
*/
function buttonclick(number, isdown) {
if(isdown)websock.send("bdown:"+number);
else websock.send("bup:"+number);
}
function padclick(type, number, isdown) {
switch(type){
case CENTER:
if(isdown)websock.send("pcdown:"+number);
else websock.send("pcup:"+number);
break;
case FOR:
if(isdown)websock.send("pfdown:"+number);
else websock.send("pfup:"+number);
break;
case BACK:
if(isdown)websock.send("pbdown:"+number);
else websock.send("pbup:"+number);
break;
case LEFT:
if(isdown)websock.send("pldown:"+number);
else websock.send("plup:"+number);
break;
case RIGHT:
if(isdown)websock.send("prdown:"+number);
else websock.send("prup:"+number);
break;
}
}

View File

@ -1,36 +1,94 @@
#include <WiFi.h>
#include <EasyUI.h>
const char* ssid = "EasyUI";
const char* ssid = "LARSUI";
const char* password = "";
long oldTime = 0;
void setup(void) {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
WiFi.setHostname(ssid);
WiFi.softAP(ssid);
//WiFi.softAP(ssid, password);
Serial.println("");
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
EasyUI.title("LARSControl");
EasyUI.label("Status: Maxim Stop");
EasyUI.label("0");
EasyUI.button("MaximDance Button", &callback2);
EasyUI.pad("center", true, &callback3);
EasyUI.pad("NoCenter", false, &callback3);
EasyUI.begin("LARS Control");
EasyUI.label("Label","123");
EasyUI.label("Label2","456");
EasyUI.label("Label3","789");
EasyUI.button("LED", &callback1);
EasyUI.begin();
}
void loop(void) {
if(millis()-oldTime> 5000){
EasyUI.print(1, String(millis()));
oldTime = millis();
}
}
void callback1(void) {
Serial.println("CALLBACK UNO");
void callback1(int id, int type) {
switch (type) {
case B_DOWN:
Serial.println("Button DOWN");
break;
case B_UP:
Serial.println("Button UP");
break;
}
void callback2(void) {
Serial.println("CALLBACK DOS");
}
void callback3(void) {
Serial.println("CALLBACK TRES");
void callback2(int id, int type) {
switch (type) {
case B_DOWN:
Serial.println("Maxim Start DAnce######################");
EasyUI.print(0, "Status: Maxim Start");
break;
case B_UP:
Serial.println("Maxim STOP DAnce######################");
EasyUI.print(0, "Status: Maxim Start");
break;
}
}
void callback3(int id, int value) {
switch (value) {
case P_LEFT_DOWN:
Serial.println("left down");
break;
case P_LEFT_UP:
Serial.println("left up");
break;
case P_RIGHT_DOWN:
Serial.println("right down");
break;
case P_RIGHT_UP:
Serial.println("right up");
break;
case P_FOR_DOWN:
Serial.println("for down");
break;
case P_FOR_UP:
Serial.println("for up");
break;
case P_BACK_DOWN:
Serial.println("back down");
break;
case P_BACK_UP:
Serial.println("back up");
break;
case P_CENTER_DOWN:
Serial.println("center down");
break;
case P_CENTER_UP:
Serial.println("center up");
break;
}
Serial.println(id);
}

View File

@ -16,168 +16,124 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
}
break;
case WS_EVT_DATA:
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(msg);
String type = root["type"];
if(type == "t"){
//Button Action
//TODO: get Button here
EasyUI.buttons[0]->callback();
//EasyUI.tbuttonStatus();
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);
}
/*
else if(mode == "tb_click"){
String status = root["status"];
String index = root["index"];
//EasyUI.tbClick(index, status);
}
*/
break;
}
}
void EasyUIClass::title(const char* _title){
ui_title = _title;
void EasyUIClass::label(const char* label){
Control* newL = new Control();
newL->type = UI_LABEL;
newL->label = label;
newL->oldValue = label;
newL->callback = NULL;
controls[cIndex] = newL;
cIndex++;
}
// 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++;
}
/*
// 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++;
}
*/
// Create a generic Button
void EasyUIClass::button(const char* bLabel, void(* callBack)()){
//TODO: Implement
Button* newB = new Button();
newB->label = bLabel;
void EasyUIClass::button(const char* label, void(* callBack)(int, int)){
Control* newB = new Control();
newB->type = UI_BUTTON;
newB->label = label;
newB->callback = callBack;
buttons[bIndex] = newB;
bIndex++;
controls[cIndex] = newB;
cIndex++;
}
/*
// Check Toggle Buttons States and Transfer to Webpage
void EasyUIClass::tbuttonStatus(){
void EasyUIClass::switcher(const char* label, int start_state, void(* callBack)(int, int)){
Control* newS = new Control();
newS->type = UI_SWITCHER;
newS->label = label;
newS->callback = callBack;
controls[cIndex] = newS;
cIndex++;
//TODO: implement switch state buffer
//tbutton_swap[tb_index] = swap_state;
}
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++;
}
void EasyUIClass::print(int labelid, String value){
if(labelid<cIndex && controls[labelid]->type == UI_LABEL){
controls[labelid]->oldValue = value;
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["type"] = UPDATE_LABEL;
root["label"] = value;
root["id"] = String(labelid);
root.printTo(json);
webSocket->broadcastTXT(json);
}
*/
/*
// 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);
this->ws->textAll(json);
}else{
digitalWrite(tbutton_pinout[_index.toInt()], HIGH);
Serial.println(String("Error: ")+ String(labelid) +String(" is no label"));
}
}
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);
}
}
}
*/
// Convert & Transfer Arduino elements to JSON elements
void EasyUIClass::jsonDom(AsyncWebSocketClient * client){
//SiteTitle
//TODO: emit here
// Labels
for(int i=0; i<l_index; i++){
for(int i=-1; i<cIndex; i++){
String json;
StaticJsonBuffer<200> jsonBuffer1;
JsonObject& root1 = jsonBuffer1.createObject();
root1["type"] = "domLabel";
root1["l_title"] = String(label_title[i]);
root1["label"] = String(label_value[i]);
root1.printTo(json);
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);
root["id"] = String(i);
if(controls[i]->type == UI_LABEL)root["label"] = controls[i]->oldValue;
}
root.printTo(json);
client->text(json);
}
// Buttons
for(int i=0; i<bIndex; i++){
String json;
StaticJsonBuffer<200> jsonBuffer2;
JsonObject& root2 = jsonBuffer2.createObject();
root2["type"] = "domButton";
root2["index"] = String(i);
root2["label"] = buttons[i]->label;
root2.printTo(json);
client->text(json);
}
}
void EasyUIClass::begin(){
void EasyUIClass::begin(const char * _title){
ui_title = _title;
server = new AsyncWebServer(80);
ws = new AsyncWebSocket("/ws");
SPIFFS.begin(false);
SPIFFS.begin();
ws->onEvent(onWsEvent);
server->addHandler(ws);
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
//Heap for general Servertest

View File

@ -16,35 +16,58 @@
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
typedef struct Button
typedef struct Control
{
unsigned int type;
const char *label;
void (*callback)();
} Button;
void (*callback)(int, int);
String oldValue;
} Control;
// Types
#define UI_TITEL 0
#define UI_LABEL 1
#define UI_BUTTON 2
#define UI_SWITCHER 3
#define UI_PAD 4
#define UI_CPAD 5
#define UPDATE_LABEL 6
// Values
#define B_DOWN -1
#define B_UP 1
#define P_LEFT_DOWN -2
#define P_LEFT_UP 2
#define P_RIGHT_DOWN -3
#define P_RIGHT_UP 3
#define P_FOR_DOWN -4
#define P_FOR_UP 4
#define P_BACK_DOWN -5
#define P_BACK_UP 5
#define P_CENTER_DOWN -6
#define P_CENTER_UP 6
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(const char* tbutton_label, void(* callBack)()); // Create Event Button
void label(const char* label_name, const char* label_val); // Create Label
void begin(const char* _title); // Setup servers and page
// Creating Elements
void label(const char* label); // Create Label
void button(const char* label, void(* callBack)(int, int)); // Create Event Button
void switcher(const char* label, int start_state, void(* callBack)(int, int)); // Create Toggle Button
void pad(const char* label, bool centerButton, void(* callBack)(int, int)); // Create Pad Control
// Update Elements
void print(int labelid, String value);
// Variables ---
const char* ui_title = "EasyUI"; // Store UI Title and Header Name
int bIndex; // How Many Buttons
int l_index; // How Many Labels
bool tbutton_swap[10];
Button* buttons[10];
const char* label_value[10]; // Stores Label Values - MAX 10
const char* label_title[10]; // Stores Label Titles - MAX 10
String webpage; // Coverts Arduino elements to JSON elements
void tbClick(String _index, String _status);
void tbuttonStatus();
const char* ui_title = "ESPUI"; // Store UI Title and Header Name
int cIndex; // Control index
Control* controls[25];
void jsonDom(AsyncWebSocketClient * client);
private: