-
Notifications
You must be signed in to change notification settings - Fork 14
/
LedDriverLPD8806.cpp
445 lines (401 loc) · 13.4 KB
/
LedDriverLPD8806.cpp
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
/**
LedDriverLPD8806
Implementierung auf der Basis von LPD8806-Streifen.
@mc Arduino/RBBB
@autor Christian Aschoff / caschoff _AT_ mac _DOT_ com
@version 1.1
@created 9.2.2015
@updated 16.2.2015
Versionshistorie:
V 1.0: - Erstellt.
V 1.1: - Unterstuetzung fuer die alte Arduino-IDE (bis 1.0.6) entfernt.
Verkabelung: Einspeisung oben links, dann schlangenfoermig runter,
dann Ecke unten links, oben links, oben rechts, unten rechts.
Achtung! LPD8806-Streifen koennen nur in Vielfachen von 2 getrennt werden! Daher bleiben am Rand LEDs uebrig (und dunkel)!
*/
#include "LedDriverLPD8806.h"
// #define DEBUG
#include "Debug.h"
// eigentlich haben wir ja 115 LEDs, aber LPD8806 sind ja immer zweier...
#if defined(MATRIX_XXL) || defined(RGBW_LEDS)
#define NUM_PIXEL 115
#else
#define NUM_PIXEL 130
#endif
/**
Initialisierung.
@param data Pin, an dem die Data-Line haengt.
*/
LedDriverLPD8806::LedDriverLPD8806(byte dataPin, byte clockPin) {
#ifdef MATRIX_XXL
#ifdef RGBW_LEDS
_strip = new LPD8806RGBW_DBL(NUM_PIXEL, dataPin, clockPin);
#else
_strip = new LPD8806DBL(NUM_PIXEL, dataPin, clockPin);
#endif
#else
#ifdef RGBW_LEDS
_strip = new LPD8806RGBW(NUM_PIXEL, dataPin, clockPin);
#else
_strip = new LPD8806(NUM_PIXEL, dataPin, clockPin);
#endif
#endif
_strip->begin();
_wheelPos = 0;
_transitionCounter = 0;
_transitionCompleted = true;
_lastColorUpdate = millis();
_dirty = false;
_demoTransition = false;
_lastLEDsOn = 0;
}
/**
init() wird im Hauptprogramm in init() aufgerufen.
Hier sollten die LED-Treiber in eine definierten
Ausgangszustand gebracht werden.
*/
void LedDriverLPD8806::init() {
setBrightness(50);
clearData();
wakeUp();
}
void LedDriverLPD8806::printSignature() {
DEBUG_PRINTLN(F("LPD8806"));
}
/**
Den Bildschirm-Puffer auf die LED-Matrix schreiben.
@param onChange: TRUE, wenn es Aenderungen in dem Bildschirm-Puffer gab,
FALSE, wenn es ein Refresh-Aufruf war.
*/
void LedDriverLPD8806::writeScreenBufferToMatrix(word matrix[16], boolean onChange, eColors a_color) {
boolean updateWheelColor = false;
byte wheelPosIncrement = 0;
if ((a_color == color_rgb_continuous) && _transitionCompleted) {
if ((millis() - _lastColorUpdate) > ((1 + (10-settings.getColorChangeRate())) * 100)) {
updateWheelColor = true;
_lastColorUpdate = millis();
wheelPosIncrement = 2;
}
}
if (a_color == color_rgb_step) {
if (!(rtc.getMinutes() % 5) && (helperSeconds == 0) && onChange) {
updateWheelColor = true;
_lastColorUpdate = millis();
wheelPosIncrement = 200;
}
}
if (!_transitionCompleted && (_transitionCounter > 0)) {
_transitionCounter--;
}
else
{
_transitionCounter = 0;
}
if (onChange || _dirty || _demoTransition || updateWheelColor || (((_transitionCounter == 0) || (Settings::TRANSITION_MODE_FADE == settings.getTransitionMode())) && !_transitionCompleted)) {
uint32_t color = 0;
uint32_t colorNew = 0;
uint32_t colorOld = 0;
uint32_t colorOverlay1 = 0;
uint32_t colorOverlay2 = 0;
byte brightnessOld = 0;
byte brightnessNew = 0;
_dirty = false;
if (mode != STD_MODE_NORMAL) {
_transitionCompleted = true;
_demoTransition = false;
}
/*************
MATRIX
**************/
if (onChange || _demoTransition) {
if (((helperSeconds == 0) || _demoTransition) && (mode == STD_MODE_NORMAL) && _transitionCompleted && !evtActive) {
switch (settings.getTransitionMode()) {
case Settings::TRANSITION_MODE_FADE:
for (byte i = 0; i < 11; i++) {
_matrixOld[i] = _matrixNew[i];
if (_demoTransition) {
_matrixNew[i] = 0;
}
else {
_matrixNew[i] = matrix[i];
}
_matrixOverlay[i] = 0;
}
_transitionCompleted = false;
_transitionCounter = FADINGCOUNTERLOAD;
break;
case Settings::TRANSITION_MODE_MATRIX:
case Settings::TRANSITION_MODE_SLIDE:
if (((rtc.getMinutes() % 5) == 0) || _demoTransition) {
Transitions::resetTransition();
for (byte i = 0; i < 11; i++) {
_matrixOld[i] = 0;
_matrixOverlay[i] = 0;
}
_transitionCompleted = false;
}
break;
case Settings::TRANSITION_MODE_NORMAL:
if (_demoTransition) {
for (byte i = 0; i < 11; i++) {
_matrixNew[i] = 0;
}
_transitionCompleted = false;
_transitionCounter = NORMALCOUNTERLOAD;
}
break;
default:
;
}
}
}
if (_transitionCompleted) {
for (byte i = 0; i < 11; i++) {
_matrixOld[i] = 0;
_matrixNew[i] = matrix[i];
_matrixOverlay[i] = 0;
}
}
_demoTransition = false;
if ((_transitionCounter == 0) && !_transitionCompleted) {
switch (settings.getTransitionMode()) {
case Settings::TRANSITION_MODE_MATRIX:
_transitionCounter = map(_lastLEDsOn, 0, 110, MATRIXCOUNTERLOAD, MATRIXCOUNTERLOAD * 0.4);
_transitionCompleted = Transitions::nextMatrixStep(_matrixOld, _matrixNew, _matrixOverlay, matrix);
break;
case Settings::TRANSITION_MODE_SLIDE:
_transitionCounter = SLIDINGCOUNTERLOAD;
_transitionCompleted = Transitions::nextSlideStep(_matrixNew, matrix);
break;
case Settings::TRANSITION_MODE_NORMAL:
_transitionCompleted = true;
break;
default:
;
}
}
/*************
BRIGHTNESS
**************/
if ((Settings::TRANSITION_MODE_FADE == settings.getTransitionMode()) && !_transitionCompleted) {
brightnessOld = map(_transitionCounter, 0, FADINGCOUNTERLOAD, 0, _brightnessInPercent);
brightnessNew = map(_transitionCounter, FADINGCOUNTERLOAD, 0 , 0 , _brightnessInPercent);
if (_transitionCounter == 0) {
_transitionCompleted = true;
}
}
else {
brightnessNew = _brightnessInPercent;
}
/*************
COLOR
**************/
if (a_color <= color_single_max)
{
color = _strip->Color(_brightnessScaleColor(_brightnessInPercent, pgm_read_byte_near(&defaultColors[a_color].red)), _brightnessScaleColor(_brightnessInPercent, pgm_read_byte_near(&defaultColors[a_color].blue)), _brightnessScaleColor(_brightnessInPercent, pgm_read_byte_near(&defaultColors[a_color].green)));
colorNew = _strip->Color(_brightnessScaleColor(brightnessNew, pgm_read_byte_near(&defaultColors[a_color].red)), _brightnessScaleColor(brightnessNew, pgm_read_byte_near(&defaultColors[a_color].blue)), _brightnessScaleColor(brightnessNew, pgm_read_byte_near(&defaultColors[a_color].green)));
colorOld = _strip->Color(_brightnessScaleColor(brightnessOld, pgm_read_byte_near(&defaultColors[a_color].red)), _brightnessScaleColor(brightnessOld, pgm_read_byte_near(&defaultColors[a_color].blue)), _brightnessScaleColor(brightnessOld, pgm_read_byte_near(&defaultColors[a_color].green)));
}
else if ((a_color == color_rgb_continuous || a_color == color_rgb_step)) {
if (updateWheelColor) {
_wheelPos += wheelPosIncrement;
}
color = _wheel(_brightnessInPercent, _wheelPos);
colorNew = _wheel(brightnessNew, _wheelPos);
colorOld = _wheel(brightnessOld, _wheelPos);
}
if ( (settings.getTransitionMode() == Settings::TRANSITION_MODE_MATRIX) && !_transitionCompleted ) {
colorOverlay1 = _strip->Color(_brightnessScaleColor(_brightnessInPercent, 0), _brightnessScaleColor(_brightnessInPercent, 0), _brightnessScaleColor(_brightnessInPercent, 255));
colorOverlay2 = _strip->Color(_brightnessScaleColor(_brightnessInPercent, 0), _brightnessScaleColor(_brightnessInPercent, 0), _brightnessScaleColor(_brightnessInPercent, 255 * 0.5));
colorOld = _strip->Color(_brightnessScaleColor(_brightnessInPercent, 0), _brightnessScaleColor(_brightnessInPercent, 0), _brightnessScaleColor(_brightnessInPercent, 255 * 0.1));
}
/*************
WRITE OUT
**************/
_clear();
_lastLEDsOn = 0;
for (byte y = 0; y < 10; y++) {
for (byte x = 5; x < 16; x++) {
word t = 1 << x;
if ((settings.getTransitionMode() == Settings::TRANSITION_MODE_FADE) && ((_matrixOld[y] & t) == t) && ((_matrixNew[y] & t) == t) ) {
_setPixel(15 - x, y, color);
}
else {
if ((_matrixOverlay[y] & t) == t) {
_setPixel(15 - x, y, colorOverlay1);
_lastLEDsOn++;
}
else if ((_matrixOverlay[y + 1] & t) == t) {
_setPixel(15 - x, y, colorOverlay2);
_lastLEDsOn++;
}
else if ((_matrixOld[y] & t) == t) {
_setPixel(15 - x, y, colorOld);
_lastLEDsOn++;
}
else if ((_matrixNew[y] & t) == t) {
_setPixel(15 - x, y, colorNew);
_lastLEDsOn++;
}
}
}
}
// wir muessen die Eck-LEDs und die Alarm-LED umsetzen...
byte cornerLedCount[] = {1, 0, 3, 2, 4};
for ( byte i = 0; i < 5; i++) {
if ((settings.getTransitionMode() == Settings::TRANSITION_MODE_FADE) && ((_matrixOld[cornerLedCount[i]] & _matrixNew[cornerLedCount[i]] & 0b0000000000011111) > 0) ) {
_setPixel(110 + i, color);
}
else {
if (((_matrixOld[cornerLedCount[i]] & 0b0000000000010000) > 0) ) {
_setPixel(110 + i, colorOld);
}
else if (((_matrixNew[cornerLedCount[i]] & 0b0000000000010000) > 0) ) {
_setPixel(110 + i, colorNew);
}
}
}
_strip->show();
}
}
/**
Die Helligkeit des Displays anpassen.
@param brightnessInPercent Die Helligkeit.
*/
void LedDriverLPD8806::setBrightness(byte brightnessInPercent) {
if ((brightnessInPercent != _brightnessInPercent) && _transitionCompleted) {
_brightnessInPercent = brightnessInPercent;
_dirty = true;
}
}
/**
Die aktuelle Helligkeit bekommen.
*/
byte LedDriverLPD8806::getBrightness() {
return _brightnessInPercent;
}
/**
Anpassung der Groesse des Bildspeichers.
@param linesToWrite Wieviel Zeilen aus dem Bildspeicher sollen
geschrieben werden?
*/
void LedDriverLPD8806::setLinesToWrite(byte linesToWrite) {
}
/**
Das Display ausschalten.
*/
void LedDriverLPD8806::shutDown() {
_clear();
_strip->show();
_transitionCompleted = true;
}
/**
Das Display einschalten.
*/
void LedDriverLPD8806::wakeUp() {
}
/**
Den Dateninhalt des LED-Treibers loeschen.
*/
void LedDriverLPD8806::clearData() {
_clear();
_strip->show();
}
/**
Einen X/Y-koordinierten Pixel in der Matrix setzen.
*/
void LedDriverLPD8806::_setPixel(byte x, byte y, uint32_t c) {
_setPixel(x + (y * 11), c);
}
/**
Einen Pixel im Streifen setzten (die Eck-LEDs sind am Ende).
*/
void LedDriverLPD8806::_setPixel(byte num, uint32_t c) {
#if defined(MATRIX_XXL) || defined(RGBW_LEDS)
if (num < 110) {
if ((num / 11) % 2 == 0) {
_strip->setPixelColor(num, c);
} else {
_strip->setPixelColor(((num / 11) * 11) + 10 - (num % 11), c);
}
} else {
switch (num) {
case 110:
_strip->setPixelColor(111, c);
break;
case 111:
_strip->setPixelColor(112, c);
break;
case 112:
_strip->setPixelColor(113, c);
break;
case 113:
_strip->setPixelColor(110, c);
break;
case 114: // die Alarm-LED
_strip->setPixelColor(114, c);
break;
default:
;
}
}
#else
if (num < 110) {
if ((num / 11) % 2 == 0) {
_strip->setPixelColor(num + (num / 11), c);
} else {
_strip->setPixelColor(((num / 11) * 12) + 11 - (num % 11), c);
}
} else {
switch (num) {
case 110:
_strip->setPixelColor(111 + 11, c);
break;
case 111:
_strip->setPixelColor(112 + 12, c);
break;
case 112:
_strip->setPixelColor(113 + 13, c);
break;
case 113:
_strip->setPixelColor(110 + 10, c);
break;
case 114:
_strip->setPixelColor(114 + 14, c);
break;
default:
;
}
}
#endif
delay(1);
}
/**
Funktion fuer saubere 'Regenbogen'-Farben.
Kopiert aus den Adafruit-Beispielen (strand).
*/
uint32_t LedDriverLPD8806::_wheel(byte brightness, byte wheelPos) {
if (wheelPos < 85) {
return _strip->Color(_brightnessScaleColor(brightness, wheelPos * 3), _brightnessScaleColor(brightness, 255 - wheelPos * 3), _brightnessScaleColor(brightness, 0));
} else if (wheelPos < 170) {
wheelPos -= 85;
return _strip->Color(_brightnessScaleColor(brightness, 255 - wheelPos * 3), _brightnessScaleColor(brightness, 0), _brightnessScaleColor(brightness, wheelPos * 3));
} else {
wheelPos -= 170;
return _strip->Color(_brightnessScaleColor(brightness, 0), _brightnessScaleColor(brightness, wheelPos * 3), _brightnessScaleColor(brightness, 255 - wheelPos * 3));
}
}
/**
Hilfsfunktion fuer das Skalieren der Farben.
*/
byte LedDriverLPD8806::_brightnessScaleColor(byte brightness, byte colorPart) {
return map(brightness, 0, 100, 0, colorPart / 2); // LPD8806 kann nur 7 bit Farben! (also 0..127, nicht 0..255)
}
/**
Streifen loeschen.
*/
void LedDriverLPD8806::_clear() {
for (byte i = 0; i < NUM_PIXEL; i++) {
_strip->setPixelColor(i, 0);
}
}