-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiotsaDisplay.cpp
More file actions
223 lines (205 loc) · 6.04 KB
/
Copy pathiotsaDisplay.cpp
File metadata and controls
223 lines (205 loc) · 6.04 KB
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
#include "iotsa.h"
#include "iotsaDisplay.h"
#include <Wire.h>
// The LiquidCrystal library needed is from
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/LiquidCrystal_V1.2.1.zip
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
unsigned long clearTime; // time at which to turn off backlight
// LCD handlers
void IotsaDisplayMod::setup() {
IFDEBUG IotsaSerial.print("lcdSetup");
Wire.begin(pin_sda, pin_scl);
lcd.begin(lcd_width, lcd_height);
lcd.backlight();
lcd.print(iotsaConfig.hostName);
delay(200);
lcd.noBacklight();
lcd.clear();
IFDEBUG IotsaSerial.println(" done");
}
void IotsaDisplayMod::webHandler() {
IotsaWebServer *server = api.webService->server;
String msg;
bool any = false;
bool didBacklight = false;
bool didPos = false;
if( server->hasArg("msg")) {
msg = server->arg("msg");
any = true;
}
if( server->hasArg("clear")) {
if (atoi(server->arg("clear").c_str()) > 0) {
lcd.clear();
if (!didPos) x = y = 0;
}
any = true;
}
if( server->hasArg("x")) {
const char *arg = server->arg("x").c_str();
if (arg && *arg) {
didPos = true;
x = atoi(arg);
}
}
if( server->hasArg("y")) {
const char *arg = server->arg("y").c_str();
if (arg && *arg) {
didPos = true;
y = atoi(arg);
}
}
if (server->hasArg("backlight")) {
const char *arg = server->arg("backlight").c_str();
if (arg && *arg) {
int dur = atoi(arg);
any = true;
didBacklight = true;
if (dur) {
clearTime = millis() + dur*1000;
} else {
clearTime = 0;
}
any = true;
}
}
if (buzzer) {
if (server->hasArg("alarm")) {
const char *arg = server->arg("alarm").c_str();
if (arg && *arg) {
int dur = atoi(arg);
buzzer->set(dur*100);
}
}
}
if (any) {
lcd.setCursor(x, y);
// IFDEBUG IotsaSerial.print("Show message ");
// IFDEBUG IotsaSerial.println(msg);
printPercentEscape(msg);
if (!didBacklight)
clearTime = millis() + 5000; // 5 seconds is the default display time
if (clearTime) {
IFDEBUG IotsaSerial.println("backlight");
lcd.backlight();
} else {
IFDEBUG IotsaSerial.println("nobacklight");
lcd.noBacklight();
}
}
String message = "<html><head><title>LCD Server</title></head><body><h1>LCD Server</h1>";
message += "<form method='get'>Message: <input name='msg' value=''><br>\n";
message += "Position X: <input name='x' value=''> Y: <input name='y' value=''><br>\n";
message += "<input name='clear' type='checkbox' value='1'>Clear<br>\n";
message += "Backlight: <input name='backlight' value=''> seconds<br>\n";
if (buzzer) {
message += "Alarm: <input name='alarm' value=''> (times 0.1 second)<br>\n";
}
message += "<input type='submit'></form></body></html>";
server->send(200, "text/html", message);
}
String IotsaDisplayMod::info() {
return "<p>See <a href='/display'>/display</a> to display messages or <a href='/api/display'>/api/display</a> for REST interface.</p>";
}
bool IotsaDisplayMod::postHandler(const char *path, const JsonVariant& request, JsonObject& reply) {
return putHandler(path, request, reply);
}
bool IotsaDisplayMod::putHandler(const char *path, const JsonVariant& request, JsonObject& reply) {
bool any = false;
if (!request.is<JsonObject>()) return false;
JsonObject reqObj = request.as<JsonObject>();
bool clear = false;
if (getFromRequest<bool, bool>(reqObj, "clear", clear) && clear) {
any = true;
lcd.clear();
}
bool changedX = getFromRequest<int>(reqObj, "x", x);
bool changedY = getFromRequest<int>(reqObj, "y", y);
if (changedX || changedY) {
lcd.setCursor(x, y);
any = true;
}
if (buzzer) {
int alarm = 0;
if (getFromRequest<int>(reqObj, "alarm", alarm)) {
any = true;
buzzer->set(alarm*100);
}
}
float backlight = 5;
if (getFromRequest<float>(reqObj, "backlight", backlight)) {
any = true;
}
if (backlight) {
clearTime = millis() + (int)(backlight*1000);
IFDEBUG IotsaSerial.println("backlight");
lcd.backlight();
} else {
IFDEBUG IotsaSerial.println("nobacklight");
lcd.noBacklight();
}
String msg;
if (getFromRequest<const char *>(reqObj, "msg", msg)) {
printString(msg);
any = true;
}
return any;
}
void IotsaDisplayMod::loop() {
if (clearTime && millis() > clearTime) {
clearTime = 0;
IFDEBUG IotsaSerial.println("nobacklight");
lcd.noBacklight();
}
}
void IotsaDisplayMod::lateSetup() {
// get=false: GET /api/display is not meaningful (the display is write-only), so
// api.setup() registers no web page -- wire /display up on the shared server.
api.setup("display", false, true, true);
name = "display";
app.server->on("/display", std::bind(&IotsaDisplayMod::webHandler, this));
}
void IotsaDisplayMod::printPercentEscape(String &src) {
const char *arg = src.c_str();
while (*arg) {
char newch = 0;
if (*arg == '+') newch = ' ';
else if (*arg == '%') {
arg++;
if (*arg >= '0' && *arg <= '9') newch = (*arg-'0') << 4;
if (*arg >= 'a' && *arg <= 'f') newch = (*arg-'a'+10) << 4;
if (*arg >= 'A' && *arg <= 'F') newch = (*arg-'A'+10) << 4;
arg++;
if (*arg == 0) break;
if (*arg >= '0' && *arg <= '9') newch |= (*arg-'0');
if (*arg >= 'a' && *arg <= 'f') newch |= (*arg-'a'+10);
if (*arg >= 'A' && *arg <= 'F') newch |= (*arg-'A'+10);
} else {
newch = *arg;
}
lcd.print(newch);
x++;
if (x >= lcd_width) {
x = 0;
y++;
if (y >= lcd_height) y = 0;
lcd.setCursor(x, y);
}
arg++;
}
}
void IotsaDisplayMod::printString(String &src) {
if (src != "") {
for (size_t i=0; i<src.length(); i++) {
char newch = src.charAt(i);
lcd.print(newch);
x++;
if (x >= lcd_width) {
x = 0;
y++;
if (y >= lcd_height) y = 0;
lcd.setCursor(x, y);
}
}
}
}