forked from aetios50/PincabLedStrip
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PincabLedStrip.ino
545 lines (478 loc) · 13 KB
/
PincabLedStrip.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
/********************************************************************************************************
** Pincab Ledstrip Controller
********************************************************************************************************/
/*
* This code is fully compatible with WEMOS D1 PRO
*/
#define SERIAL_BUFFER_SIZE 2048
#include <EEPROM.h>
#include <elapsedMillis.h>
/***/
#if defined(ESP8266)
//#define DEBUG_ON_WIFI
#ifdef DEBUG_ON_WIFI
#include "WifiDebug.h"
static WifiDebug wifidebug;
#endif
#endif
/***/
#include "LedStrip.h"
#define FirmwareVersionMajor 2
#define FirmwareVersionMinor 2
//Defines the Pinnumber to which the built in led
// Defines the Pinnumber for the test button which is low when pressed
#if defined(ESP8266)
#define TestPin D0
#define LedPin LED_BUILTIN
#elif defined (ESP32)
#define TestPin 5
#define LedPin 15
#elif TEENSY
#define TestPin 17
#define LedPin 13
#else
#error "Unsupported Card Model"
#endif
#if TEENSY
#define READ_EEPROM_SETTINGS 0
#else
#define READ_EEPROM_SETTINGS 1
#endif
enum SettingsEnum {
TEST_ON_RESET,
TEST_SWITCH,
ACTIVITY_LED,
SETTING4,
SETTING5,
SETTING6,
SETTING7,
SETTING8,
COUNT
};
uint8_t Settings[SettingsEnum::COUNT] = {
0,
1,
1,
0,
0,
0,
0,
0
};
void ReadSettings() {
#if READ_EEPROM_SETTINGS
EEPROM.begin(SettingsEnum::COUNT);
for (int i = 0; i < SettingsEnum::COUNT; ++i) {
Settings[i] = EEPROM.read(i);
}
EEPROM.end();
#endif
Serial.print("Test on Reset :");
Serial.println(Settings[SettingsEnum::TEST_ON_RESET], DEC);
Serial.print("Test Switch :");
Serial.println(Settings[SettingsEnum::TEST_SWITCH], DEC);
Serial.print("Activity Led :");
Serial.println(Settings[SettingsEnum::ACTIVITY_LED], DEC);
}
bool HasSetting(int setting) {
return Settings[setting] != 0;
}
// TEST double click settings
long buttonTimer = 0;
long longPressTime = 2000;
boolean buttonActive = false;
boolean longPressActive = false;
//Variable used to control the blinking and flickering of the led of the Wemos
elapsedMillis BlinkTimer;
int BlinkMode;
elapsedMillis BlinkModeTimeoutTimer;
LedStrip ledstrip(MaxLedsPerStrip);
uint32_t configuredStripLength = MaxLedsPerStrip;
//Setup of the system. Is called once on startup.
void setup() {
Serial.begin(2000000); //921600);
while (Serial.available()) {
Serial.read();
};
delay(100);
Serial.println("");
ReadSettings();
/**/
#if DEBUG_ON_WIFI
wifidebug.begin();
#endif
/**/
//Initialize the lib for the ledstrip
ledstrip.setStripLength(configuredStripLength);
ledstrip.begin();
ledstrip.show();
//Initialize the led pin
pinMode(LedPin, OUTPUT);
digitalWrite(LedPin, LOW);
#if DEBUG_ON_WIFI
wifidebug.debug_send_msg("Setup done");
#endif
if (HasSetting(SettingsEnum::TEST_SWITCH)) {
//Initialize and find value of the test pin
pinMode(TestPin, INPUT_PULLUP);
digitalWrite(TestPin, HIGH);
}
if (HasSetting(SettingsEnum::TEST_ON_RESET)) {
Test();
}
ClearAllLedData();
ledstrip.show();
SetBlinkMode(0);
Blink();
}
void TestLedstripColor(byte r, byte g, byte b) {
ledstrip.clearAll();
ledstrip.show();
ActivityLed(-1);
delay(200);
for (int i = 0; i < configuredStripLength * NUMBER_LEDSTRIP; i++) {
ledstrip.setPixel(i, r, g, b);
ActivityLed(-1);
}
ledstrip.show();
ActivityLed(0);
delay(200);
ledstrip.clearAll();
ledstrip.show();
}
void Test() {
TestLedstripColor(HALF_BRIGHTNESS, 0, 0); //RED
TestLedstripColor(0, HALF_BRIGHTNESS, 0); //GREEN
TestLedstripColor(0, 0, HALF_BRIGHTNESS); //BLUE
TestLedstripColor(HALF_BRIGHTNESS, HALF_BRIGHTNESS, 0); //YELLOW
TestLedstripColor(HALF_BRIGHTNESS, 0, HALF_BRIGHTNESS); //MAGENTA
TestLedstripColor(0, HALF_BRIGHTNESS, HALF_BRIGHTNESS); //CYAN
TestLedstripColor(HALF_BRIGHTNESS, HALF_BRIGHTNESS, HALF_BRIGHTNESS); //WHITE
}
void ChaserLedstripColor(byte r, byte g, byte b) {
ledstrip.clearAll();
ledstrip.show();
ActivityLed(-1);
for (int i = 0; i < configuredStripLength * NUMBER_LEDSTRIP; i++) {
ledstrip.setPixel(i, r, g, b);
ActivityLed(-1);
ledstrip.show();
}
ActivityLed(0);
delay(1000);
ledstrip.clearAll();
ledstrip.show();
}
void Chaser() {
ChaserLedstripColor(HALF_BRIGHTNESS, 0, HALF_BRIGHTNESS);
}
static byte receivedByte;
//Main loop of the programm gets called again and again.
void loop() {
// TEST ROUTINES BUTTON DETECTION
if (HasSetting(SettingsEnum::TEST_SWITCH) && digitalRead(TestPin) == LOW) {
//Button pressed
if (buttonActive == false) {
buttonActive = true;
buttonTimer = millis();
}
if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
longPressActive = true;
Chaser();
}
} else {
if (buttonActive == true) {
if (longPressActive == true) {
longPressActive = false;
} else {
Test();
}
buttonActive = false;
}
}
/*
// run test if button is grounded
if (HasSetting(SettingsEnum::TEST_SWITCH) && digitalRead(TestPin) == LOW) {
Test();
}*/
//Check if data is available
if (Serial.available() > 0) {
receivedByte = Serial.read();
switch (receivedByte) {
case 'Z':
//Set length of a strip
SetALedStripLength();
break;
case 'B':
//Set brightness of a strip
SetALedStripBrightness();
break;
case 'L':
//Set length of strips
SetLedStripLength();
break;
case 'F':
//Fill strip area with color
Fill();
break;
case 'Q':
//receive compressed data for strips
ReceiveCompressedData();
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 ledstrip per strip
SendMaxNumberOfLeds();
break;
case 'T':
//Launch a Test sequence
Test();
Ack();
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;
}
void ActivityLed(int activity) {
if (!HasSetting(SettingsEnum::ACTIVITY_LED)) {
return;
}
if (activity < 0) {
digitalWrite(LedPin, !digitalRead(LedPin));
} else {
digitalWrite(LedPin, activity);
}
}
//Controls the blinking of the led
void Blink() {
switch (BlinkMode) {
case 0:
//Blinkmode 0 is only active after the start of the Wemos until the first command is received.
if (BlinkTimer < 1500) {
ActivityLed(0);
} else if (BlinkTimer < 1600) {
ActivityLed(1);
} else {
BlinkTimer = 0;
ActivityLed(0);
}
break;
case 1:
//Blinkmode 1 is activated when the Wemos receives a command
//Mode expires 500ms after the last command has been received resp. mode has been set
if (BlinkTimer > 30) {
BlinkTimer = 0;
ActivityLed(-1);
}
if (BlinkModeTimeoutTimer > 500) {
SetBlinkMode(2);
}
break;
case 2:
//Blinkmode 2 is active while the Wemos is waiting for more commands
if (BlinkTimer < 1500) {
ActivityLed(0);
} else if (BlinkTimer < 1600) {
ActivityLed(1);
} else if (BlinkTimer < 1700) {
ActivityLed(0);
} else if (BlinkTimer < 1800) {
ActivityLed(1);
} else {
BlinkTimer = 0;
ActivityLed(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;
ActivityLed(-1);
}
break;
}
}
//Outputs the data in the ram to the ledstriptrips
void OutputData() {
ledstrip.show();
Ack();
}
//Fills the given area of a ledstriptrip with a color
void Fill() {
word firstLed = ReceiveWord();
word numberOfLeds = ReceiveWord();
int ColorData = ReceiveColorData();
if (firstLed <= configuredStripLength * NUMBER_LEDSTRIP && numberOfLeds > 0 && firstLed + numberOfLeds - 1 <= configuredStripLength * NUMBER_LEDSTRIP) {
ledstrip.setPixels(firstLed, numberOfLeds, ColorData);
/*word endLedNr = firstLed + numberOfLeds;
for (word ledNr = firstLed; ledNr < endLedNr; ledNr++) {
ledstrip.setPixel(ledNr, ColorData);
}*/
Ack();
} else {
//Number of the first led or the number of ledstrip to receive is outside the allowed range
Nack();
}
}
//Receives data for the ledstriptrips
void ReceiveCompressedData() {
word firstLed = ReceiveWord();
word numberOfCompressedData = ReceiveWord();
word numberOfLeds = ReceiveWord();
if (firstLed <= configuredStripLength * NUMBER_LEDSTRIP && numberOfLeds > 0 && firstLed + numberOfLeds - 1 <= configuredStripLength * NUMBER_LEDSTRIP) {
word startLed = firstLed;
word endLedNr = firstLed + numberOfLeds;
for (word numPack = 0; numPack < numberOfCompressedData; numPack++) {
while (!Serial.available()) {};
uint8_t nbLeds = (uint8_t)Serial.read();
int color = ReceiveColorData();
for (word numLed = 0; numLed < nbLeds; numLed++) {
ledstrip.setPixel(startLed + numLed, color);
}
startLed += nbLeds;
if (startLed >= endLedNr) {
break;
}
}
Ack();
} else {
//Number of the first led or the number of ledstrip to receive is outside the allowed range
Nack();
}
}
//Receives data for the ledstriptrips
void ReceiveData() {
word firstLed = ReceiveWord();
word numberOfLeds = ReceiveWord();
if (firstLed <= configuredStripLength * NUMBER_LEDSTRIP && numberOfLeds > 0 && firstLed + numberOfLeds - 1 <= configuredStripLength * NUMBER_LEDSTRIP) {
//FirstLedNr and numberOfLeds are valid.
//Receive and set color data
word endLedNr = firstLed + numberOfLeds;
int i = 0;
for (word ledNr = firstLed; ledNr < endLedNr; ledNr++) {
i++;
ledstrip.setPixel(ledNr, ReceiveColorData());
}
Ack();
} else {
//Number of the first led or the number of ledstrip to receive is outside the allowed range
Nack();
}
}
//Sets the length of the longest connected ledstriptrip. Length is restricted to the max number of allowed ledstrip
void SetLedStripLength() {
word stripLength = ReceiveWord();
if (stripLength < 1 || stripLength > MaxLedsPerStrip) {
//stripLength is either to small or above the max number of ledstrip allowed
Nack();
} else {
//stripLength is in the valid range
configuredStripLength = stripLength;
ledstrip.setStripLength(stripLength);
Ack();
}
}
//Sets the length of a led strip
void SetALedStripLength() {
while (!Serial.available()) {};
byte indexStrip = Serial.read();
while (!Serial.available()) {};
byte lastStrip = Serial.read();
while (!Serial.available()) {};
word stripLength = ReceiveWord();
/*if (stripLength < 1 || stripLength > MaxLedsPerStrip ) {
//stripLength is either to small or above the max number of ledstrip allowed or strip index is not within existing range
Nack();
} else*/
{
//stripLength is in the valid range
ledstrip.addNewStrip(indexStrip, stripLength);
Ack();
}
}
//Sets the brightness of a led strip
void SetALedStripBrightness() {
while (!Serial.available()) {};
byte indexStrip = Serial.read();
while (!Serial.available()) {};
byte lastStrip = Serial.read();
while (!Serial.available()) {};
uint8_t brightness = Serial.read();
ledstrip.setStripBrightness(indexStrip, brightness);
Ack();
}
//Clears the data for all configured ledstrip
void ClearAllLedData() {
/*for (word ledNr = 0; ledNr < configuredStripLength * NUMBER_LEDSTRIP; ledNr++) {
ledstrip.setPixel(ledNr, 0);
}*/
ledstrip.clearAll();
Ack();
}
//Sends the firmware version
void SendVersion() {
Serial.write(FirmwareVersionMajor);
Serial.write(FirmwareVersionMinor);
Ack();
}
//Sends the max number of ledstrip per strip
void SendMaxNumberOfLeds() {
byte B = MaxLedsPerStrip >> 8;
Serial.write(B);
B = MaxLedsPerStrip & 255;
Serial.write(B);
//Reset all ledstrips to 0 size until ledstrip size are sent per index
ledstrip.reset();
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;
}