-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDisplayBacklight.ino
136 lines (119 loc) · 3.09 KB
/
DisplayBacklight.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
/*
* RGB LED UART controller
*
* This simple sketch controls an RGB LED strand via data received over UART.
* The protocol works as follows:
*
* A single 'frame' that can be displayed, on our N LED strand, is defined as:
* uint8_t data[N * 3] = { r0, g0, b0, r1, g1, b1, r2, g2, b2, ... };
* With consecutive 24-bit RGB pixels.
* This is simply transferred, as is, from the PC to the Arduino. To distinguish
* between start and end of different frames, a magic string is used, defined below.
* Hopefully it won't appear in the data stream... :)
*/
#include <FastLED.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define DEBUG
#ifdef DEBUG
#define debugPrint(x) Serial.print(x)
#define debugPrintln(x) Serial.println(x)
#else
#define debugPrint(x)
#define debugPrintln(x)
#endif
#define LED_PIN 13
#define LED_COUNT 20
#define BAUDRATE 115200
#define MAGIC "xythobuzRGBled"
// Optional auto-turn-off after no data received
//#define TURN_OFF_TIMEOUT 60000l // in ms
CRGB leds[LED_COUNT];
void setup() {
Serial.begin(BAUDRATE);
FastLED.addLeds<WS2812B, LED_PIN, RBG>(leds, LED_COUNT);
FastLED.show(); // Initialize all pixels to 'off'
debugPrintln("RGB LED controller ready!");
debugPrint("Config: ");
debugPrint(LED_COUNT);
debugPrintln(" LEDs...");
debugPrint("Magic-Word: \"");
debugPrint(MAGIC);
debugPrintln("\"");
// Blink LED as init message
leds[0] = CRGB(32, 32, 32);
FastLED.show();
delay(100);
leds[0] = CRGB(0, 0, 0);
FastLED.show();
}
#define WAIT_FOR_START 0
#define RECEIVING_START 1
#define RECEIVING_DATA 2
uint8_t state = WAIT_FOR_START;
uint8_t startPos = 0;
uint16_t dataPos = 0;
uint8_t frame[LED_COUNT * 3];
#ifdef TURN_OFF_TIMEOUT
unsigned long lastTime = millis();
#endif
static void setNewFrame() {
for (uint16_t i = 0; i < LED_COUNT; i++) {
leds[i] = CRGB(frame[i * 3], frame[(i * 3) + 1], frame[(i * 3) + 2]);
}
FastLED.show();
}
void loop() {
if (!Serial.available()) {
#ifdef TURN_OFF_TIMEOUT
if ((millis() - lastTime) >= TURN_OFF_TIMEOUT) {
for (int i = 0; i < (LED_COUNT * 3); i++) {
frame[i] = 0;
}
setNewFrame();
}
#endif
return;
}
#ifdef TURN_OFF_TIMEOUT
lastTime = millis();
#endif
uint8_t c;
Serial.readBytes(&c, 1);
if (state == WAIT_FOR_START) {
if (c == MAGIC[0]) {
state = RECEIVING_START;
startPos = 1;
debugPrintln("f");
} else {
debugPrintln("");
}
} else if (state == RECEIVING_START) {
if (startPos < strlen(MAGIC)) {
if (c == MAGIC[startPos]) {
startPos++;
debugPrintln("g");
if (startPos >= strlen(MAGIC)) {
state = RECEIVING_DATA;
dataPos = 0;
debugPrintln("w");
}
} else {
state = WAIT_FOR_START;
debugPrintln("x");
}
} else {
// Should not happen
state = RECEIVING_START;
debugPrintln("s");
}
} else if (state == RECEIVING_DATA) {
frame[dataPos++] = c;
if (dataPos >= (LED_COUNT * 3)) {
debugPrintln("d");
setNewFrame();
state = WAIT_FOR_START;
}
}
}