forked from jankeymeulen/gbl-trafficlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgbltrafficlight-multithread.ino
579 lines (491 loc) · 13.5 KB
/
gbltrafficlight-multithread.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <FastLED.h>
#include <SPI.h>
#define NUM_LEDS 60
#define DATA_PIN 23
#define CLOCK_PIN 18
/* general LED stuff */
CRGB leds[NUM_LEDS];
CRGB colour = CRGB::White;
char effect[50] = "";
unsigned long previousFrame = 0;
/* blink stuff */
#define BLINK_DELAY 1000
bool blinkState = false;/* cylon stuff */
#define CYLON_WIDTH 20
#define CYLON_DELAY 50
int cylonStart = 0;
bool cylonDirection = true;
/* pong stuff */
#define PONG_WIDTH 5
#define PONG_MIN_DELAY 1
#define PONG_MAX_DELAY 20
#define PONG_CYCLE_LENGTH 2000
#define PONG_BLUR 172
int pongDelay = 20;
int pongCycleStart = 0;
int pongStart = 0;
bool pongDirection = true;
bool pongCycleDirection = true;
/* Run stuff */
#define RUN_WIDTH 10
#define RUN_DELAY 5
int runStart = 0;
bool runDirection = true;
/* Sparkle stuff */
#define SPARKLE_COUNT 1
#define SPARKLE_DIMM 244
#define SPARKLE_DELAY 20
#define SPARKLE_CYCLE 500
int previousSparkle = 0;
/* Rainbow stuff */
#define RAINBOW_DELAY 100
uint8_t rainbowHue = 0;
/* Theatre stuff */
#define THEATRE_DELAY 300
#define THEATRE_LENGTH 3
int theatreToggle = 0;
/* Breathe stuff */
#define BREATHE_DELAY 50
#define BREATHE_CYCLE 3000.0
/* Fadeblack stuff */
#define FADEBLACK_DELAY 50
#define FADEBLACK_CYCLE 3000
/* Fadewhite stuff */
#define FADEWHITE_DELAY 50
#define FADEWHITE_CYCLE 3000
/* Fire stuff */
#define FIRE_COOLING 55
#define FIRE_SPARKING 120
#define FIRE_DELAY 30
static byte heat[NUM_LEDS];
int cooldown;
/* Comet stuff */
#define COMET_WIDTH 10
#define COMET_DELAY 30
#define COMET_COOLING 55
#define COMET_INTERVAL 5000
int cometPosition = 0;
int nextComet = 0;
int cometToggle = false;
/* Strobo stuff */
#define STROBO_INTERVAL 100
#define STROBO_LENGTH 5
int nextStrobo = 0;
bool stroboToggle = false;
/* Lightning stuff */
/* Crash stuff */
#define CRASH_WIDTH 2
/* Belgium stuff */
#define BELGIUM_COUNT 3
#define BELGIUM_DELAY 40
int belgiumStart = 0;
/* France stuff */
#define FRANCE_COUNT 3
#define FRANCE_DELAY 40
int franceStart = 0;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("[SETUP] Connecting to Wifi");
WiFi.begin("GoogleGuest-Legacy");
long startConnect = millis();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (startConnect + 60000 < millis())
{
Serial.println("\nError connecting, rebooting");
ESP.restart();
}
}
Serial.println(" connected!");
FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR, DATA_RATE_MHZ(1)>(leds, NUM_LEDS);
FastLED.setBrightness(255);
Serial.println("[SETUP] LEDs configured!");
xTaskCreate(
ledTask, /* Task function. */
"LedTask", /* String with name of task. */
10000, /* Stack size in words. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
xTaskCreate(
httpClientTask, /* Task function. */
"HttpClientTask", /* String with name of task. */
10000, /* Stack size in words. */
NULL, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
Serial.println("[SETUP] Done!");
}
void loop() {
delay(360000);
ESP.restart();
}
void httpClientTask(void* parameter) {
while (1) {
HTTPClient http;
if ((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
http.begin("http://gbl-trafficlight.appspot.com/effect"); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET done, code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
char payloadchar[50];
payload.toCharArray(payloadchar, 50);
Serial.printf("[HTTP] Payload [%s]\n", payloadchar);
int r, g, b;
sscanf(payloadchar, "%s %d %d %d", effect, &r, &g, &b);
colour.r = r;
colour.g = g;
colour.b = b;
Serial.printf("[HTTP] Parsed Effect:[%s],R:[%d],G:[%d],B:[%d]\n", effect, r, g, b);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
ESP.restart();
}
http.end();
}
vTaskDelay(2500);
}
}
void ledTask(void* parameter)
{
Serial.println("[LED] Entered task");
previousFrame = millis();
while (1) {
if ( strcmp(effect, "SOLID") == 0 ) {
fillSolid();
} else if ( strcmp(effect, "BLINK" ) == 0 ) {
fillBlink();
} else if ( strcmp(effect, "CYLON" ) == 0 ) {
fillCylon();
} else if ( strcmp(effect, "RUN" ) == 0 ) {
fillRun();
} else if ( strcmp(effect, "PONG" ) == 0 ) {
fillPong();
} else if ( strcmp(effect, "SPARKLE" ) == 0 ) {
fillSparkle();
} else if ( strcmp(effect, "RAINBOW" ) == 0 ) {
fillRainbow();
} else if ( strcmp(effect, "THEATRE" ) == 0 ) {
fillTheatre();
} else if ( strcmp(effect, "FIRE" ) == 0 ) {
fillFire();
} else if ( strcmp(effect, "COMET" ) == 0 ) {
fillComet();
} else if ( strcmp(effect, "BREATHE" ) == 0 ) {
fillBreathe();
} else if ( strcmp(effect, "STROBO" ) == 0 ) {
fillStrobo();
} else if ( strcmp(effect, "BELGIUM" ) == 0 ) {
fillBelgium();
} else if ( strcmp(effect, "FRANCE" ) == 0 ) {
fillFrance();
}
FastLED.show();
vTaskDelay(1);
}
}
void fillBlink() {
if (previousFrame + BLINK_DELAY < millis()) {
if (blinkState) {
fill_solid (leds, NUM_LEDS, colour);
} else {
fill_solid (leds, NUM_LEDS, CRGB::Black);
}
blinkState = !blinkState;
previousFrame = millis();
}
}
void fillStrobo() {
if (nextStrobo < millis()) {
if (stroboToggle) {
fill_solid (leds, NUM_LEDS, colour);
nextStrobo = millis() + STROBO_LENGTH;
} else {
fill_solid (leds, NUM_LEDS, CRGB::Black);
nextStrobo = millis() + STROBO_INTERVAL;
}
stroboToggle = !stroboToggle;
}
}
void fillBelgium() {
if (previousFrame + BELGIUM_DELAY < millis()) {
int flagWidth = ceil(NUM_LEDS/BELGIUM_COUNT);
for (int i = 0; i < NUM_LEDS; i++)
{
int segment = floor(((i+belgiumStart)%flagWidth)/ceil(flagWidth/3));
if (segment == 0) {
leds[i] = CRGB::Red;
} else if (segment == 1) {
leds[i] = CRGB::Yellow;
} else {
leds[i] = CRGB::Black;
}
}
belgiumStart++;
if(belgiumStart >= NUM_LEDS)
{
belgiumStart = 0;
}
previousFrame = millis();
}
}
void fillFrance() {
if (previousFrame + FRANCE_DELAY < millis()) {
int flagWidth = ceil(NUM_LEDS/FRANCE_COUNT);
for (int i = 0; i < NUM_LEDS; i++)
{
int segment = floor(((i+franceStart)%flagWidth)/ceil(flagWidth/3));
if (segment == 0) {
leds[i] = CRGB::Red;
} else if (segment == 1) {
leds[i] = CRGB::White;
} else {
leds[i] = CRGB::Blue;
}
}
franceStart++;
if(franceStart >= NUM_LEDS)
{
franceStart = 0;
}
previousFrame = millis();
}
}
void fillCylon() {
if (previousFrame + CYLON_DELAY < millis()) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
for (int i = cylonStart; i < NUM_LEDS && i < cylonStart + CYLON_WIDTH ; i++) {
leds[i] = colour;
}
if ( cylonDirection ) {
cylonStart++;
} else {
cylonStart--;
}
if (cylonStart >= NUM_LEDS - CYLON_WIDTH) {
cylonDirection = false;
}
if (cylonStart == 0) {
cylonDirection = true;
}
previousFrame = millis();
}
}
void fillPong() {
if (previousFrame + pongDelay < millis()) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
for (int i = pongStart; i < NUM_LEDS && i < pongStart + PONG_WIDTH ; i++) {
leds[i] = colour;
}
blur1d(leds, NUM_LEDS, PONG_BLUR);
if ( pongDirection ) {
pongStart++;
} else {
pongStart--;
}
if (pongStart >= NUM_LEDS - PONG_WIDTH) {
pongDirection = false;
}
if (pongStart == 0) {
pongDirection = true;
}
if (pongCycleStart + PONG_CYCLE_LENGTH < millis() )
{
if ( pongCycleDirection ) {
pongDelay--;
} else {
pongDelay++;
}
Serial.printf("[DEBUG] Pong Cycled, delay [%d]\n", pongDelay);
pongCycleStart = millis();
}
if (pongDelay >= PONG_MAX_DELAY) {
pongCycleDirection = true;
}
if (pongDelay <= PONG_MIN_DELAY) {
pongCycleDirection = false;
}
previousFrame = millis();
}
}
void fillRun() {
if (previousFrame + RUN_DELAY < millis()) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
for (int i = runStart; i < NUM_LEDS && i < runStart + RUN_WIDTH ; i++) {
leds[i] = colour;
}
if (runStart >= NUM_LEDS - RUN_WIDTH)
runStart = 0;
runStart++;
previousFrame = millis();
}
}
//void fillComet2() {
// if (previousFrame + COMET_DELAY < millis()) {
// fill_solid(leds, NUM_LEDS, CRGB::Black);
// for (int i = cometStart; i >= 0 && i > cometStart - COMET_WIDTH ; i--) {
// leds[i] = colour;
// leds[i].fadeLightBy(((cometStart-i) * 256) / COMET_WIDTH);
// }
// if (cometStart >= NUM_LEDS)
// cometStart = 0;
// cometStart++;
// previousFrame = millis();
// }
//}
void fillSparkle() {
if (previousFrame + SPARKLE_DELAY < millis()) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8( SPARKLE_DIMM );
}
if ( previousSparkle + SPARKLE_CYCLE < millis() ) {
for (int i = 0; i < SPARKLE_COUNT; i++) {
leds[random(NUM_LEDS)] = colour;
}
}
previousFrame = millis();
}
}
void fillRainbow() {
if (previousFrame + RAINBOW_DELAY < millis()) {
fill_rainbow(leds, NUM_LEDS, rainbowHue);
rainbowHue--;
previousFrame = millis();
}
}
void fillSolid() {
fill_solid (leds, NUM_LEDS, colour);
}
void fillBreathe() {
if ( previousFrame + BREATHE_DELAY < millis() ) {
previousFrame = millis();
CRGB dimmedColour = colour;
dimmedColour %= (exp(sin(previousFrame / BREATHE_CYCLE * PI)) - 0.36787944) * 108.0;
dimmedColour += 10;
fill_solid (leds, NUM_LEDS, dimmedColour);
}
}
void fillFadeBlack() {
if ( previousFrame + FADEBLACK_DELAY < millis() ) {
previousFrame = millis();
CRGB dimmedColour = colour;
dimmedColour %= (exp(sin(previousFrame / BREATHE_CYCLE * PI)) - 0.36787944) * 108.0;
dimmedColour += 10;
fill_solid (leds, NUM_LEDS, dimmedColour);
}
}
void fillTheatre() {
if ( previousFrame + THEATRE_DELAY < millis() ) {
for ( int i = 0; i < NUM_LEDS; i++ )
{
if (i % THEATRE_LENGTH == theatreToggle)
{
leds[i] = colour;
} else {
leds[i] = CRGB::Black;
}
}
theatreToggle++;
if (theatreToggle == THEATRE_LENGTH)
{
theatreToggle = 0;
}
previousFrame = millis();
}
}
void fillComet() {
if ( previousFrame + COMET_DELAY < millis() ) {
// Cooldown all cells
for ( int i = 0; i < NUM_LEDS; i++) {
cooldown = random(0, ((COMET_COOLING * 10) / NUM_LEDS) + 2);
if (cooldown > heat[i]) {
heat[i] = 0;
} else {
heat[i] = heat[i] - cooldown;
}
}
// Move the comet up
if (cometPosition < NUM_LEDS - 1 && cometToggle)
{
heat[cometPosition] = 255;
heat[cometPosition + 1] = 255;
cometPosition += 2;
}
// Convert heat to colours
for ( int j = 0; j < NUM_LEDS; j++) {
setPixelHeatColor(j, heat[j] );
}
// Colour the comet to the custom colour
if (cometPosition < NUM_LEDS - 1 && cometToggle)
{
leds[cometPosition + 1] = colour;
}
if (cometPosition >= NUM_LEDS)
{
cometToggle = false;
cometPosition = 0;
nextComet = millis() + random(COMET_INTERVAL, COMET_INTERVAL * 3);
}
if (nextComet < millis())
{
cometToggle = true;
}
previousFrame = millis();
}
}
void fillFire() {
if ( previousFrame + FIRE_DELAY < millis() ) {
// Step 1. Cool down every cell a little
for ( int i = 0; i < NUM_LEDS; i++) {
cooldown = random(0, ((FIRE_COOLING * 10) / NUM_LEDS) + 2);
if (cooldown > heat[i]) {
heat[i] = 0;
} else {
heat[i] = heat[i] - cooldown;
}
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for ( int k = NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Step 3. Randomly ignite new 'sparks' near the bottom
if ( random(255) < FIRE_SPARKING ) {
int y = random(7);
heat[y] = heat[y] + random(160, 255);
}
// Step 4. Convert heat to LED colors
for ( int j = 0; j < NUM_LEDS; j++) {
setPixelHeatColor(j, heat[j] );
}
previousFrame = millis();
}
}
void setPixelHeatColor (int pixel, byte temperature) {
// Scale 'heat' down from 0-255 to 0-191
byte t192 = round((temperature / 255.0) * 191);
// calculate ramp up from
byte heatramp = t192 & 0x3F; // 0..63
heatramp <<= 2; // scale up to 0..252
// figure out which third of the spectrum we're in:
if ( t192 > 0x80) { // hottest
leds[pixel].setRGB(255, 255, heatramp);
} else if ( t192 > 0x40 ) { // middle
leds[pixel].setRGB(255, heatramp, 0);
} else { // coolest
leds[pixel].setRGB(heatramp, 0, 0);
}
}