-
Notifications
You must be signed in to change notification settings - Fork 34
/
aREST_UI.h
248 lines (196 loc) · 6.37 KB
/
aREST_UI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
aREST UI for Arduino & the ESP8266
See the README file for more details.
Written in 2015 by Marco Schwartz under a GPL license.
Version 1.1.1
Changelog:
Version 1.1.1: Fixed bug with NodeMCU boards
Version 1.1.0: Added support for functions
Version 1.0.1: Initial release with buttons only
*/
#ifndef aRest_ui_h
#define aRest_ui_h
// Include Arduino header
#include "Arduino.h"
// Using ESP8266 ?
#if defined(ESP8266)
#include "stdlib_noniso.h"
#endif
class aREST_UI: public aREST {
public:
aREST_UI() {
}
// Get title
void title(String the_title) {
ui_title = the_title;
}
// Create button
void button(int pin){
// Set pin as output
#if defined(ARDUINO_ESP8266_NODEMCU) || defined(ARDUINO_ESP8266_WEMOS_D1MINI)
pinMode(esp_12_pin_map(pin), OUTPUT);
#else
pinMode(pin, OUTPUT);
#endif
// Set in button array
buttons[buttons_index] = pin;
buttons_index++;
}
// Create function control
void callFunction(char * functionName, char * type){
// Set in callFunction array
callFunctionNames[functions_index] = functionName;
callFunctionTypes[functions_index] = type;
functions_index++;
}
// Create slider
void slider(int pin) {
// Set pin as output
#if defined(ARDUINO_ESP8266_NODEMCU) || defined(ARDUINO_ESP8266_WEMOS_D1MINI)
pinMode(esp_12_pin_map(pin), OUTPUT);
#else
pinMode(pin, OUTPUT);
#endif
// Set in button array
sliders[sliders_index] = pin;
sliders_index++;
}
// Create label
void label(char * label_name){
int_labels_names[int_labels_index] = label_name;
int_labels_index++;
}
// Handle connection
virtual void root_answer() {
// Answer
addToBuffer("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
addToBuffer("<html><head>");
addToBuffer("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
addToBuffer("<script ");
addToBuffer("src=\"http://code.jquery.com/jquery-2.1.3.min.js\">");
addToBuffer("</script>");
addToBuffer("<script type='text/javascript' src='http://cdn.rawgit.com/Foliotek/AjaxQ/master/ajaxq.js'></script>");
addToBuffer("<style>.row {margin-top: 30px;} .indicator {font-size: 30px; vertical-align: middle;}</style>");
addToBuffer("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");
addToBuffer("</head><body>");
addToBuffer("<div class=\"container\">");
// Title
if (ui_title.length() != 0) {
addToBuffer("<h1>");
addToBuffer(ui_title);
addToBuffer("</h1>");
}
else {
addToBuffer("<h1>Interface</h1>");
}
// Buttons UI
for (int i = 0; i < buttons_index; i++) {
addToBuffer("<div class=\"row\">");
addToBuffer("<div class=\"col-md-2\"><button class=\"btn btn-block btn-lg btn-primary\" id='btn_on");
addToBuffer(buttons[i]);
addToBuffer("'>On</button></div>");
addToBuffer("<div class=\"col-md-2\"><button class=\"btn btn-block btn-lg btn-danger\" id='btn_off");
addToBuffer(buttons[i]);
addToBuffer("'>Off</button></div>");
addToBuffer("</div>");
}
// Function calls UI
for (int i = 0; i < functions_index; i++) {
if (callFunctionTypes[i] == "push") {
addToBuffer("<div class=\"row\">");
addToBuffer("<div class=\"col-md-3\"><button class=\"btn btn-block btn-lg btn-primary\" id='fn_push_");
addToBuffer(callFunctionNames[i]);
addToBuffer("'>");
addToBuffer(callFunctionNames[i]);
addToBuffer("</button></div>");
addToBuffer("</div>");
}
}
// Sliders UI
for (int i = 0; i < sliders_index; i++) {
addToBuffer("<div class=\"row\">");
#if defined(ESP8266)
addToBuffer("<div class=\"col-md-2\"><input type='range' value='0' max='1023' min='0' step='5' id='slider");
#else
addToBuffer("<div class=\"col-md-2\"><input type='range' value='0' max='255' min='0' step='5' id='slider");
#endif
addToBuffer(sliders[i]);
addToBuffer("'></div>");
addToBuffer("</div>");
}
// Labels UI
for (int j = 0; j < int_labels_index; j++) {
addToBuffer("<div class=\"row\">");
addToBuffer("<div class='col-md-3 indicator'>");
addToBuffer(int_labels_names[j]);
addToBuffer(": </div>");
addToBuffer("<div class='col-md-3 indicator' id='");
addToBuffer(int_labels_names[j]);
addToBuffer("'></div>");
addToBuffer("</div>");
}
addToBuffer("</div>");
addToBuffer("<script>$( document ).ready(function() {");
// Buttons JavaScript
for (int i = 0; i < buttons_index; i++) {
addToBuffer("$('#btn_on");
addToBuffer(buttons[i]);
addToBuffer("').click(function() {$.getq('queue','/digital/");
addToBuffer(buttons[i]);
addToBuffer("/1');});");
addToBuffer("$('#btn_off");
addToBuffer(buttons[i]);
addToBuffer("').click(function() {$.getq('queue','/digital/");
addToBuffer(buttons[i]);
addToBuffer("/0');});");
}
// Functions JavaScript
for (int i = 0; i < functions_index; i++) {
addToBuffer("$('#fn_push_");
addToBuffer(callFunctionNames[i]);
addToBuffer("').click(function() {$.getq('queue','/");
addToBuffer(callFunctionNames[i]);
addToBuffer("');});");
}
// Sliders JavaScript
for (int i = 0; i < sliders_index; i++) {
addToBuffer("$('#slider");
addToBuffer(sliders[i]);
addToBuffer("').mouseup(function() {var val = $('#slider");
addToBuffer(sliders[i]);
addToBuffer("').val(); $.getq('queue','/analog/");
addToBuffer(sliders[i]);
addToBuffer("/' + val); });");
}
// Labels JavaScript
for (int j = 0; j < int_labels_index; j++) {
addToBuffer("$.getq('queue','/");
addToBuffer(int_labels_names[j]);
addToBuffer("', function(data) { $('#");
addToBuffer(int_labels_names[j]);
addToBuffer("').html(data.");
addToBuffer(int_labels_names[j]);
addToBuffer("); });");
}
addToBuffer("});</script>");
addToBuffer("</body></html>\r\n");
}
private:
// UI title
String ui_title;
// Buttons array
int buttons[10];
int buttons_index;
// Functions array
char * callFunctionNames[10];
char * callFunctionTypes[10];
int functions_index;
// Buttons array
int sliders[10];
int sliders_index;
// Indicators array
uint8_t int_labels_index;
int * int_labels_variables[10];
char * int_labels_names[10];
};
#endif