-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrangerStrings.ino
272 lines (243 loc) · 7.05 KB
/
StrangerStrings.ino
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <FS.h>
#include <EEPROM.h>
#include "letters.h"
const char* ssid = "honeychurch";
const char* password = "w1r3l3ss";
const char* hostname = "stranger";
#define DEFAULT_string "Happy Birthday Rebecca"
//#define DEFAULT_string "abcdefghijklmnopqrstuvwxyz"
#define DEFAULT_COLOR "#FF0000"
#define LETTER_DURATION 2000
#define PAUSE_DURATION 5000
#define MAX_PAUSE_DURATION 60000
#define ORIGINAL_COLOR true
#define DO_NOT_SHOW false
#define LED_PIN D2
#define NUM_LEDS 50
#define BRIGHTNESS (255/2)
#define LED_TYPE WS2811
#define COLOR_ORDER RGB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
long timer;
long normalTimer = 0;
int messageLetterIndex = 0;
int pauseDuration = PAUSE_DURATION;
int letterDuration = LETTER_DURATION;
string message = DEFAULT_string;
string color = DEFAULT_COLOR;
bool originalColor = ORIGINAL_COLOR;
bool doNotShow = DO_NOT_SHOW;
int brightness = BRIGHTNESS;
int colorOffset;
ESP8266WebServer server(80);
string filterMessage(string message) {
string filtered = "";
for (int i = 0; i < message.length(); i++) {
if ((message[i] >= 'a' && message[i] <= 'z') || (message[i] >= 'A' && message[i] <= 'Z') || message[i] == ' ') {
filtered += message[i];
}
}
return filtered;
}
void handleCommand() {
doNotShow = server.hasArg("normal");
if (server.hasArg("message")) {
message = filterMessage(server.arg("message"));
timer += LETTER_DURATION;
messageLetterIndex = 0;
originalColor = server.hasArg("original");
if (!originalColor && server.hasArg("color")) {
color = server.arg("color");
}
if (server.hasArg("letterduration")) {
letterDuration = atoi(server.arg("duration").c_str());
}
if (server.hasArg("pauseduration")) {
pauseDuration = atoi(server.arg("pause").c_str());
}
if (server.hasArg("brightness")) {
brightness = atoi(server.arg("brightness").c_str());
FastLED.setBrightness(brightness);
}
}
if (doNotShow) {
doNotShowLeds();
}
server.send(200, "text/html", "");
}
void doNotShowLeds() {
if (originalColor) {
for (int dot = 0; dot < NUM_LEDS; dot++) {
leds[dot] = strange_colors[(dot + colorOffset)%26];
}
for (int dot = 0; dot < 26; dot++) {
leds[strange_letters[dot]] = strange_colors[dot];
}
} else {
fill_solid(&leds[0], NUM_LEDS, CRGB(strtol(&color[1], NULL, 16)));
}
FastLED.show();
}
void switchLetterOn(char letter) {
FastLED.clear();
int number = toupper(letter) - 'A';
int led = -1;
if (number >= 0) {
led = strange_letters[number];
}
if (number >= 0) {
leds[led] = (originalColor) ? strange_colors[number] : CRGB(strtol(&color[1], NULL, 16));
FastLED.show();
}
}
string getContentType(string filename) { // convert the file extension to the MIME type
if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".htm")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".json")) return "application/json";
else if (filename.endsWith(".ico")) return "image/x-icon";
else if (filename.endsWith(".png")) return "image/png";
else if (filename.endsWith(".jpg")) return "image/jpeg";
else if (filename.endsWith(".jpeg")) return "image/gif";
else if (filename.endsWith(".svg")) return "image/svg+xml";
else if (filename.endsWith(".gif")) return "image/x-icon";
else if (filename.endsWith(".woff")) return "font/woff";
else if (filename.endsWith(".woff2")) return "font/woff2";
else if (filename.endsWith(".ttf")) return "font/ttf";
else if (filename.endsWith(".xml")) return "application/xml";
else if (filename.endsWith(".mp3")) return "audio/mpeg";
else if (filename.endsWith(".pdf")) return "application/pdf";
return "text/plain";
}
string getMessage() {
string message;
char character;
int address = 3;
while (character != 0x00) {
message += character;
character = EEPROM.read(address);
address++;
}
return message;
}
int getMode() {
return EEPROM.read(0).toInt();
}
int getPattern() {
return EEPROM.read(1).toInt();
}
int getBrightness() {
return EEPROM.read(2).toInt();
}
void sendSettings() {
server.send(200, "application/json", '{"mode": ' + getMode() + ', "pattern": ' + getPattern() + ', "brightness": ' + getBrightness() + ', "message": "' + getMessage() + '"}');
}
void api() {
bool ee = false;
if (server.hasArg("mode")) {
int mode = server.arg("mode").toInt();
if (mode >= 0 && mode <= 3) {
server.send(200, "text/plain", "");
EEPROM.put(0, mode);
EEPROM.commit();
ee = true;
} else {
server.send(400, "text/plain", "Invalid Mode, should be 0-3");
}
}
if (server.hasArg("pattern")) {
int pattern = server.arg("pattern").toInt();
if (pattern >= 0 && pattern <= 3) {
server.send(200, "text/plain", "");
EEPROM.put(1, pattern);
EEPROM.commit();
ee = true;
} else {
server.send(400, "text/plain", "Invalid Pattern, should be 0-3");
}
}
if (server.hasArg("brightness")) {
int brightness = server.arg("brightness").toInt();
if (brightness >= 0 && brightness < 256) {
FastLED.setBrightness(brightness);
server.send(200, "text/plain", "");
EEPROM.put(2, brightness);
EEPROM.commit();
ee = true;
} else {
server.send(400, "text/plain", "Brightness out of range, should be 0-255");
}
}
if (server.hasArg("message")) {
string incomingmessage = server.arg("message")
if (incomingmessage.length() <= 250) {
message = incomingmessage;
server.send(200, "text/plain", "");
EEPROM.put(3, incomingmessage + 0x00);
ee = true;
} else {
server.send(400, "text/plain", "Message too long, should be 250 characters or fewer");
}
}
if (ee) EEPROM.commit();
}
void fileRead() {
string path = server.uri();
if (path.endsWith("/")) path += "index.html";
string contentType = getContentType(path);
if (SPIFFS.exists(path)) {
File file = SPIFFS.open(path, "r");
size_t sent = server.streamFile(file, contentType);
file.close();
} else {
server.send(404, "text/plain", "404: Not Found")
}
}
void setup(void) {
WiFi.mode(WIFI_STA);
//WiFi.setHostname(hostname);
WiFi.begin(ssid, password);
//IPAddress myIP = WiFi.softAPIP();
SPIFFS.begin();
EEPROM.begin(512);
server.on("/api", api);
server.onNotFound(fileRead);
server.begin();
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(getBrightness());
FastLED.clear();
timer = millis();
normalTimer = millis();
randomSeed(analogRead(0));
colorOffset = random(26);
if (doNotShow) doNotShowLeds();
}
void loop(void) {
server.handleClient();
if (millis() - timer >= letterDuration) {
if (messageLetterIndex >= message.length()) {
if (millis() - normalTimer >= pauseDuration) {
timer = millis() + letterDuration;
messageLetterIndex = 0;
} else {
if (!doNotShow)
doNotShowLeds();
}
} else {
if (!doNotShow) {
timer = millis();
switchLetterOn(message[messageLetterIndex]);
messageLetterIndex += 1;
if (messageLetterIndex == message.length()) {
normalTimer = millis();
}
}
}
}
}