-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRGB_Wall.ino
725 lines (406 loc) · 18 KB
/
RGB_Wall.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/* NOTE: This sketch is made for the Arduino Ethernet.
Changes from the last one:
- added a function for outputting the current key colors
- commands that can be sent:
- /c/?8046df,8046df,etc
- /f/?ff9900
- /g/
*/
#include "LPD8806.h"
#include "SPI.h"
#include <Ethernet.h>
// ------------------------------- debugging -------------------------------
boolean isDebugging = true;
// ------------------------------- LED setup -------------------------------
// declare the number of LEDs
// (NOTE: make sure to set the lines below for the new color arrays to this number as well!)
int numLEDs = 160;
// initialize the arrays to hold the new color values that we're going to fade to
byte newR[160];
byte newG[160];
byte newB[160];
// initalize a couple variables to keep track of which colors are key colors in the gradient we're displaying
byte keyColorIndexes[20];
byte numKeyColors = 0;
// create an instance of the LED strip class. use pin 6 for data in (DI) and
// pin 8 for clock in (CI) (we're not using the faster hardware SPI (pins 11
// and 13) because they're used by the Ethernet interface)
LPD8806 strip = LPD8806(numLEDs, 6, 8);
/*
// create an instance of the LED strip class. Use hardware SPI, so connect DI
// to pin 11 and CI to pin 13.
LPD8806 strip = LPD8806(numLEDs);
*/
// ---------------------------- web server setup ---------------------------
// set the MAC address of this specific Arduino Ethernet board
byte macAddress[] = {0x90, 0xA2, 0xDA, 0x0D, 0x27, 0x29};
// Initialize the Ethernet server library (using DHCP for the ip address)
// (on port 80 for HTTP)
EthernetServer server(80);
// -------------------------- core runtime functions ------------------------
void setup() {
// wait a brief moment to give the strips a chance to get powered up
delay(500);
// start up the LED strip
strip.begin();
// update the strip, to start they are all 'off'
strip.show();
// wait a brief moment and tell the strip to show again (this fixes a bug
// where the last couple LED's don't update reliably the first time)
delay(10);
strip.show();
// enable Serial output for debugging
if (isDebugging) Serial.begin(9600);
// start the Ethernet connection and the server
Ethernet.begin(macAddress);
server.begin();
// output what IP the server is listening on
if (isDebugging) {
Serial.print(F("server is at "));
Serial.println(Ethernet.localIP());
}
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
// when we have a connected client...
if (client) {
// output for debugging
if (isDebugging) Serial.println(F("new client"));
// read the command that we received
char command = readCommand(client);
// output for debugging
if (isDebugging) {
Serial.print(F("received command: "));
Serial.println(command);
}
// set variables that we'll use to output a response
int processedColors = 0;
boolean successfulSolidColor = false;
// if we received a command to set the colors, read and process them now
if (command == 'c') {
processedColors = readAndProcessColors(client);
// else, if we received a command to flash a solid color, read and process it now
} else if (command == 'f') {
successfulSolidColor = readAndFlashSolidColor(client);
}
// read the rest of the headers
readAllHeaders(client);
// write the response heaers
boolean wasStatusOK = ((command == 'f' && successfulSolidColor) || (command == 'c' && processedColors > 0) || command == 'g');
writeResponseHeaders(client, wasStatusOK);
// if we processed and displayed colors, output a short message
if (command == 'c') {
client.print(F("processed "));
client.print(processedColors);
client.print(F(" colors"));
// else, if we flashed a solid color, output an acknowledgement
} else if (command == 'f') {
if (successfulSolidColor) {
client.print(F("flashed solid color"));
} else {
client.print(F("no solid color"));
}
// else, if we received a command to output our key colors, output them now
} else if (command == 'g') {
outputKeyColors(client);
// else, return a message saying that we received an invalid command
} else {
client.print(F("invalid command"));
}
// delay for a milisecond to give the web browser time to receive the data
delay(1);
// close the connection
client.stop();
// output for debugging
if (isDebugging) Serial.println(F("client disconnected"));
}
}
// ----------------------- handle web request/response ---------------------
char readCommand(EthernetClient client) {
// declare variables for reading in the data
char incomingBuffer[5];
int currentBufferIndex = 0;
boolean isReceivingCommandName = false;
// while the client stays connected, keep looping...
while (client.connected()) {
// if there is any available incoming data...
if (client.available()) {
// read the next character and add it to the buffer
char nextChar = client.read();
incomingBuffer[currentBufferIndex++] = nextChar;
// if we're receiving the command name...
if (isReceivingCommandName) {
// if this character is a question mark or a space, then we've received the command
if (nextChar == '?' || nextChar == ' ') {
// if the command was blank, treat it as "c" for backwards compatibility with
// the older rgb wall code
if (incomingBuffer[0] == '?') incomingBuffer[0] = 'c';
// if the command was "c", "f" or "g", then it's a valid command, so return it
if (incomingBuffer[0] == 'c' || incomingBuffer[0] == 'f' || incomingBuffer[0] == 'g') {
return incomingBuffer[0];
// else, return "i" for invalid command
} else {
return 'i';
}
// else, if we've read three characters and we still haven't received the command,
// return "i" for invalid command
} else if (currentBufferIndex == 3) {
return 'i';
}
// else, we're looking for the string "GET /"
} else {
// if we've read the first five bytes, check to see if the first three are the value "GET"
if (currentBufferIndex == 5) {
// if they are "GET", then set the flag so we'll start receiving the command name
if (incomingBuffer[0] == 'G' && incomingBuffer[1] == 'E' && incomingBuffer[2] == 'T') {
// set the flag
isReceivingCommandName = true;
// reset the buffer that we're reading into
incomingBuffer[0] = incomingBuffer[1] = incomingBuffer[2] = incomingBuffer[3] = incomingBuffer[4] = ' ';
currentBufferIndex = 0;
// else, return "i" for invalid command
} else {
return 'i';
}
}
}
}
}
// if for some reason the client gets disconnected, quit with "i" for invalid command
return 'i';
}
int readAndProcessColors(EthernetClient client) {
// keep track of which LED we're setting
int currentLED = 0;
int numLEDsProcessed = 0;
// declare variables for reading in the data
char incomingBuffer[6];
int currentBufferIndex = 0;
// declare a variable for a timeout
unsigned long timeoutTime = millis() + 10000;
// reset our count of key colors
numKeyColors = 0;
// while the client stays connected, keep looping...
while (client.connected()) {
// if there is any available incoming data...
if (client.available()) {
// read the next character and add it to the buffer
char nextChar = client.read();
// if the current character is an asterisk, the current color is a "key" color, so
// add it to our array of key colors
if (nextChar == '*') {
// add the index of this color as a "key" color
keyColorIndexes[numKeyColors++] = currentLED;
// reset the number of key colors if it gets too big (to protect against buffer overflow)
if (numKeyColors > 19) numKeyColors = 0;
// else, if the current character is a comma or a period, consider the buffer complete
// and send the color value to the LED function
} else if (nextChar == ',' || nextChar == '.') {
// store the value for this new color
storeNewColor(currentLED, String(incomingBuffer));
// increment our LED counters
currentLED++;
numLEDsProcessed++;
// if we've set all the LEDs, start over at the beginning of the strip (this shouldn't
// happen, but it's here as a safeguard)
if (currentLED >= numLEDs) currentLED = 0;
// start our buffer over
currentBufferIndex = 0;
// if we reached a period, stop receiving the command and tell the strip to fade in
// its new colors
if (nextChar == '.') {
// output for debugging
if (isDebugging) Serial.println(F("\nfinished reading colors\n"));
// fade in the new colors (at normal speed)
fadeInNewColors(12);
// stop here
return numLEDsProcessed;
}
// else, we have a single character that we just read, so add the character to
// the buffer
} else {
incomingBuffer[currentBufferIndex++] = nextChar;
if (currentBufferIndex >= 6) currentBufferIndex = 0;
}
}
// if we've been reading or waiting for more than 10 seconds, time out
if (millis() > timeoutTime) return 0;
}
}
boolean readAndFlashSolidColor(EthernetClient client) {
// declare variables for reading in the data
char incomingBuffer[6];
int currentBufferIndex = 0;
unsigned long timeoutTime = millis() + 2000;
// while the client stays connected, keep looping...
while (client.connected()) {
// if there is any available incoming data...
if (client.available()) {
// read the next character and add it to the buffer
char nextChar = client.read();
incomingBuffer[currentBufferIndex++] = nextChar;
// when we get to the sixth character, flash the color and stop here
if (currentBufferIndex == 6) {
flashSolidColor(String(incomingBuffer));
return true;
}
}
// if we've been reading or waiting for more than 2 seconds, time out
if (millis() > timeoutTime) return false;
}
// if for some reason the client gets disconnected, quit and return false
return false;
}
void readAllHeaders(EthernetClient client) {
// declare a variable so we can know when we've received two blank lines in
// a row (which signifies the end of the message)
boolean isAtEndOfLine = false;
unsigned long timeoutTime = millis() + 5000;
// while the client stays connected, keep looping...
while (client.connected()) {
// if there is any available incoming data...
if (client.available()) {
// read the next character
char nextChar = client.read();
// if the next character is a new line...
if (nextChar == '\n') {
// if we were already at the end of a line, stop here
if (isAtEndOfLine) return;
// otherwise, set the flag and continue
isAtEndOfLine = true;
// else if the character is some other content (besides a line feed), set the flag
} else if (nextChar != '\r') {
isAtEndOfLine = false;
}
}
// if we've been reading or waiting for more than 5 seconds, time out
if (millis() > timeoutTime) return;
}
}
void writeResponseHeaders(EthernetClient client, boolean wasRequestOK) {
// if the client isn't connected, just quit
if (!client.connected()) return;
// if this request was processed successfully, output a 200 header
if (wasRequestOK) {
client.println(F("HTTP/1.1 200 OK"));
// else, output a 404 header
} else {
client.println(F("HTTP/1.1 404 Not Found"));
}
// for the rest of the header lines, send a standard http response header (including an
// access control header that will allow ajax requests to be called from any host)
client.println(F("Content-Type: text/html"));
client.println(F("Access-Control-Allow-Origin: *"));
client.println(F("Connnection: close"));
client.println();
}
// -------------------------- process gradient color commands ------------------------
// store the value for the new color that we'll set a specific LED (using a hex color
// as the input)
void storeNewColor(int ledNum, String hexColor) {
// if the hex isn't 6 character, just quit here
if (hexColor.length() != 6) return;
// separate the hex string into rgb and convert it from 8 bit (0-255) to 7 bit (0-127)
// then store them in the array {r, g, b}
String hexPart;
hexPart = hexColor.substring(0, 2);
newR[ledNum] = (byte) hexToDec(hexPart) / 2;
hexPart = hexColor.substring(2, 4);
newG[ledNum] = (byte) hexToDec(hexPart) / 2;
hexPart = hexColor.substring(4, 6);
newB[ledNum] = (byte) hexToDec(hexPart) / 2;
}
void fadeInNewColors(int fadeSteps) {
// for each fade step...
for (int remainingFadeSteps = fadeSteps; remainingFadeSteps >= 1; remainingFadeSteps--) {
// for each LED...
for (int ledNum = 0; ledNum < numLEDs; ledNum++) {
// get the current color value for this LED and extract its RGB components
uint32_t currentColor = strip.getPixelColor(ledNum);
byte currentG = (currentColor >> 16) & 255;
byte currentR = (currentColor >> 8) & 255;
byte currentB = currentColor & 255;
// calculate the next color for each LED
int r = ((newR[ledNum] - currentR) / remainingFadeSteps) + currentR;
int g = ((newG[ledNum] - currentG) / remainingFadeSteps) + currentG;
int b = ((newB[ledNum] - currentB) / remainingFadeSteps) + currentB;
// and then set it in the strip
strip.setPixelColor(ledNum, r, g, b);
}
// show the colors (at their current fade step)
strip.show();
}
// show the strip again, just to reliably lock in the last colors
delay(10);
strip.show();
}
// -------------------------- flash a solid color --------------------------
void flashSolidColor(String hexColor) {
// get the rgb decimal value from the hex string:
// separate the hex string into rgb and convert it from 8 bit (0-255) to 7 bit (0-127)
// then store them in the array {r, g, b}
String hexPart;
hexPart = hexColor.substring(0, 2);
byte r = (byte) hexToDec(hexPart) / 2;
hexPart = hexColor.substring(2, 4);
byte g = (byte) hexToDec(hexPart) / 2;
hexPart = hexColor.substring(4, 6);
byte b = (byte) hexToDec(hexPart) / 2;
// set the entire strip to the new color
for (int ledNum = 0; ledNum < numLEDs; ledNum++) {
strip.setPixelColor(ledNum, r, g, b);
}
// show the colors
strip.show();
// show the strip again, just to reliably lock in the color
delay(10);
strip.show();
// wait a moment
delay(400);
// fade the original colors back in slowly
fadeInNewColors(60);
}
// ------------------------- output our key colors -------------------------
void outputKeyColors(EthernetClient client) {
// if we don't have any key colors, output #000000
if (numKeyColors == 0) {
client.print(F("#000000"));
// else, loop through the key colors and output them
} else {
for (int i = 0; i < numKeyColors; i++) {
client.print(F("#"));
client.print(decToHex(newR[keyColorIndexes[i]] * 2, 2));
client.print(decToHex(newG[keyColorIndexes[i]] * 2, 2));
client.print(decToHex(newB[keyColorIndexes[i]] * 2, 2));
client.print(F(" "));
client.print(round(((float) keyColorIndexes[i] / (numLEDs - 1)) * 100));
client.print(F("%"));
if (i + 1 < numKeyColors) client.print(F(","));
}
}
}
// ---------------------- hex/dec color conversion utilities -----------------------
unsigned int hexToDec(String hexString) {
// NOTE: This function can handle a positive hex value from 0 - 65,535 (a four digit hex string).
// For larger/longer values, change "unsigned int" to "long" in both places.
unsigned int decValue = 0;
int nextInt;
for (int i = 0; i < hexString.length(); i++) {
nextInt = int(hexString.charAt(i));
if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
nextInt = constrain(nextInt, 0, 15);
decValue = (decValue * 16) + nextInt;
}
return decValue;
}
String decToHex(byte decValue, byte desiredStringLength) {
// NOTE: This function can handle a positive decimal value from 0 - 255, and it will pad it
// with 0's (on the left) if it is less than the desired string length.
// For larger/longer values, change "byte" to "unsigned int" or "long" for the decValue parameter.
String hexString = String(decValue, HEX);
while (hexString.length() < desiredStringLength) hexString = "0" + hexString;
return hexString;
}