-
Notifications
You must be signed in to change notification settings - Fork 2
/
ESP32StripController.ino
323 lines (269 loc) · 9.25 KB
/
ESP32StripController.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
/********************************************************************************************************
** ESP32Strip Controller
** ----------------------
*********************************************************************************************************/
/*
License:
--------
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <elapsedMillis.h>
#include <FastLED.h>
#define SERIAL_BUFFER_SIZE 4096
//Definiton of Major and Minor part of the firmware version. This value can be received using the V command.
//If something is changed in the code the number should be increased.
#define FirmwareVersionMajor 1
#define FirmwareVersionMinor 3
//Defines the max number of leds which is allowed per ledstrip.
#define MaxLedsPerStrip 448
#define configuredStrips 6
word configuredStripLength = 448;
CRGB leds[configuredStrips*MaxLedsPerStrip];
//Defines the Pinnumber to which the built in led is connected.
#define LedPin 2
//Variable used to control the blinking and flickering of the led
elapsedMillis BlinkTimer;
int BlinkMode;
elapsedMillis BlinkModeTimeoutTimer;
//Setup of the system. Is called once on startup.
void setup() {
Serial.begin(921600);
//Initialize the lib for the leds
SetFastLedStripes(configuredStripLength);
//Initialize the led pin
pinMode(LedPin, OUTPUT);
SetBlinkMode(0);
}
//Main loop of the programm gets called again and again.
void loop() {
// put your main code here, to run repeatedly:
//Check if data is available
if (Serial.available()) {
byte receivedByte = Serial.read();
switch (receivedByte) {
case 'L':
//Set length of strips
SetLedStripLength();
break;
case 'F':
//Fill strip area with color
Fill();
break;
case 'R':
//receive data for strips
ReceiveData();
break;
case 'O':
//output data on strip
OutputData();
break;
case 'C':
//Clears all previously received led data
ClearAllLedData();
break;
case 'V':
//Send the firmware version
SendVersion();
break;
case 'M':
//Get max number of leds per strip
SendMaxNumberOfLeds();
break;
case 'T':
fill_solid( leds, MaxLedsPerStrip*configuredStrips, CRGB(255,0,0));
FastLED.show();
FastLED.delay(1000);
// FastLED.showColor(CRGB(0, 255, 0));
fill_solid( leds, MaxLedsPerStrip*configuredStrips, CRGB(0,255,0));
FastLED.show();
FastLED.delay(1000);
// FastLED.showColor(CRGB(0, 0, 255));
fill_solid( leds, MaxLedsPerStrip*configuredStrips, CRGB(0,0,255));
FastLED.show();
FastLED.delay(1000);
ClearAllLedData();
FastLED.show();
break;
default:
// no unknown commands allowed. Send NACK (N)
Nack();
break;
}
SetBlinkMode(1);
}
Blink();
}
//Sets the mode for the blinking of the led
void SetBlinkMode(int Mode) {
BlinkMode = Mode;
BlinkModeTimeoutTimer = 0;
}
//Controls the blinking of the led
void Blink() {
switch (BlinkMode) {
case 0:
//Blinkmode 0 is only active after the start of the Teensy until the first command is received.
if (BlinkTimer < 1500) {
digitalWrite(LedPin, 0);
} else if (BlinkTimer < 1600) {
digitalWrite(LedPin, 1);
} else {
BlinkTimer = 0;
digitalWrite(LedPin, 0);
}
break;
case 1:
//Blinkmode 1 is activated when the Teensy receives a command
//Mode expires 500ms after the last command has been received resp. mode has been set
if (BlinkTimer > 30) {
BlinkTimer = 0;
digitalWrite(LedPin, !digitalRead(LedPin));
}
if (BlinkModeTimeoutTimer > 500) {
SetBlinkMode(2);
}
break;
case 2:
//Blinkmode 2 is active while the Teensy is waiting for more commands
if (BlinkTimer < 1500) {
digitalWrite(LedPin, 0);
} else if (BlinkTimer < 1600) {
digitalWrite(LedPin, 1);
} else if (BlinkTimer < 1700) {
digitalWrite(LedPin, 0);
} else if (BlinkTimer < 1800) {
digitalWrite(LedPin, 1);
} else {
BlinkTimer = 0;
digitalWrite(LedPin, 0);
}
default:
//This should never be active
//The code is only here to make it easier to determine if a wrong Blinkcode has been set
if (BlinkTimer > 2000) {
BlinkTimer = 0;
digitalWrite(LedPin, !digitalRead(LedPin));
}
break;
}
}
//Outputs the data in the ram to the ledstrips
void OutputData() {
FastLED.show();
Ack();
}
//Fills the given area of a ledstrip with a color
void Fill() {
word firstLed = ReceiveWord();
word numberOfLeds = ReceiveWord();
int ColorData = ReceiveColorData();
if ( firstLed <= configuredStripLength * configuredStrips && numberOfLeds > 0 && firstLed + numberOfLeds - 1 <= configuredStripLength * configuredStrips ) {
word endLedNr = firstLed + numberOfLeds;
for (word ledNr = firstLed; ledNr < endLedNr; ledNr++) {
leds[ledNr] = CRGB(ColorData);
}
Ack();
} else {
//Number of the first led or the number of leds to receive is outside the allowed range
Nack();
}
}
//Receives data for the ledstrips
void ReceiveData() {
word firstLed = ReceiveWord();
word numberOfLeds = ReceiveWord();
if ( firstLed <= configuredStripLength * configuredStrips && numberOfLeds > 0 && firstLed + numberOfLeds - 1 <= configuredStripLength * configuredStrips ) {
//FirstLedNr and numberOfLeds are valid.
//Receive and set color data
word endLedNr = firstLed + numberOfLeds;
for (word ledNr = firstLed; ledNr < endLedNr; ledNr++) {
leds[ledNr] = CRGB(ReceiveColorData());
}
Ack();
} else {
//Number of the first led or the number of leds to receive is outside the allowed range
Nack();
}
}
//Sets the length of the longest connected ledstrip. Length is restricted to the max number of allowed leds
void SetLedStripLength() {
word stripLength = ReceiveWord();
if (stripLength < 1 || stripLength > MaxLedsPerStrip) {
//stripLength is either to small or above the max number of leds allowed
Nack();
} else {
//stripLength is in the valid range
configuredStripLength = stripLength;
// Removed until i figure out to change the length dynamicly
// SetFastLedStripes(stripLength);
Ack();
}
}
//Clears the data for all configured leds
void ClearAllLedData() {
FastLED.clear();
// fill_solid( leds, MaxLedsPerStrip*configuredStrips, CRGB(0,0,0));
Ack();
}
//Sends the firmware version
void SendVersion() {
Serial.write(FirmwareVersionMajor);
Serial.write(FirmwareVersionMinor);
Ack();
}
//Sends the max number of leds per strip
void SendMaxNumberOfLeds() {
byte B = MaxLedsPerStrip >> configuredStrips;
Serial.write(B);
B = MaxLedsPerStrip & 255;
Serial.write(B);
Ack();
}
//Sends a ack (A)
void Ack() {
Serial.write('A');
}
//Sends a NACK (N)
void Nack() {
Serial.write('N');
}
//Receives 3 bytes of color data.
int ReceiveColorData() {
while (!Serial.available()) {};
int colorValue = Serial.read();
while (!Serial.available()) {};
colorValue = (colorValue << 8) | Serial.read();
while (!Serial.available()) {};
colorValue = (colorValue << 8) | Serial.read();
return colorValue;
}
//Receives a word value. High byte first, low byte second
word ReceiveWord() {
while (!Serial.available()) {};
word wordValue = Serial.read() << 8;
while (!Serial.available()) {};
wordValue = wordValue | Serial.read();
return wordValue;
}
void SetFastLedStripes(int NumOfLeds) {
FastLED.addLeds<NEOPIXEL, 14>(leds, 0, NumOfLeds);
FastLED.addLeds<NEOPIXEL, 15>(leds, NumOfLeds, NumOfLeds);
FastLED.addLeds<NEOPIXEL, 26>(leds, 2 * NumOfLeds, NumOfLeds);
FastLED.addLeds<NEOPIXEL, 27>(leds, 3 * NumOfLeds, NumOfLeds);
FastLED.addLeds<NEOPIXEL, 19>(leds, 4 * NumOfLeds, NumOfLeds);
FastLED.addLeds<NEOPIXEL, 16>(leds, 5 * NumOfLeds, NumOfLeds);
}