forked from bracci/Qlockthree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LedDriverNeoPixel.cpp
422 lines (377 loc) · 12.9 KB
/
LedDriverNeoPixel.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
/**
LedDriverNeoPixel
Implementierung auf der Basis von WS2812B-Streifen wie sie die Adafruit-Neo-Pixel verwenden.
Es lohnt sich in jedem Fall, den UeberGuide von Adafruit zu lesen:
https://learn.adafruit.com/adafruit-neopixel-uberguide/overview
@mc Arduino/RBBB
@autor Christian Aschoff / caschoff _AT_ mac _DOT_ com
@version 1.2
@created 5.1.2015
@updated 16.2.2015
Versionshistorie:
V 1.0: - Erstellt.
V 1.1: - Getter fuer Helligkeit nachgezogen.
V 1.2: - 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.
*/
#include "LedDriverNeoPixel.h"
// #define DEBUG
#include "Debug.h"
#ifdef MATRIX_XXL
#define NUM_PIXEL 225
#else
#define NUM_PIXEL 115
#endif
/**
Initialisierung.
@param data Pin, an dem die Data-Line haengt.
*/
LedDriverNeoPixel::LedDriverNeoPixel(byte dataPin) {
_strip = new Adafruit_NeoPixel(NUM_PIXEL, dataPin, NEO_GRB + NEO_KHZ800);
_strip->begin();
_wheelPos = 0;
_transitionCounter = 0;
_transitionCompleted = true;
_lastColorUpdate = millis();
_dirty = false;
_demoTransition = false;
}
/**
init() wird im Hauptprogramm in init() aufgerufen.
Hier sollten die LED-Treiber in eine definierten
Ausgangszustand gebracht werden.
*/
void LedDriverNeoPixel::init() {
setBrightness(50);
clearData();
wakeUp();
}
void LedDriverNeoPixel::printSignature() {
DEBUG_PRINTLN(F("NeoPixel - WS2812B"));
}
/**
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 LedDriverNeoPixel::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 = MATRIXCOUNTERLOAD;
_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].green)), _brightnessScaleColor(_brightnessInPercent, pgm_read_byte_near(&defaultColors[a_color].blue)));
colorNew = _strip->Color(_brightnessScaleColor(brightnessNew, pgm_read_byte_near(&defaultColors[a_color].red)), _brightnessScaleColor(brightnessNew, pgm_read_byte_near(&defaultColors[a_color].green)), _brightnessScaleColor(brightnessNew, pgm_read_byte_near(&defaultColors[a_color].blue)));
colorOld = _strip->Color(_brightnessScaleColor(brightnessOld, pgm_read_byte_near(&defaultColors[a_color].red)), _brightnessScaleColor(brightnessOld, pgm_read_byte_near(&defaultColors[a_color].green)), _brightnessScaleColor(brightnessOld, pgm_read_byte_near(&defaultColors[a_color].blue)));
}
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, 255), _brightnessScaleColor(_brightnessInPercent, 0));
colorOverlay2 = _strip->Color(_brightnessScaleColor(_brightnessInPercent, 0), _brightnessScaleColor(_brightnessInPercent, 255 * 0.5), _brightnessScaleColor(_brightnessInPercent, 0));
colorOld = _strip->Color(_brightnessScaleColor(_brightnessInPercent, 0), _brightnessScaleColor(_brightnessInPercent, 255 * 0.1), _brightnessScaleColor(_brightnessInPercent, 0));
}
/*************
WRITE OUT
**************/
_strip->clear();
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);
}
else if ((_matrixOverlay[y + 1] & t) == t) {
_setPixel(15 - x, y, colorOverlay2);
}
else if ((_matrixOld[y] & t) == t) {
_setPixel(15 - x, y, colorOld);
}
else if ((_matrixNew[y] & t) == t) {
_setPixel(15 - x, y, colorNew);
}
}
}
}
// 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 LedDriverNeoPixel::setBrightness(byte brightnessInPercent) {
if ((brightnessInPercent != _brightnessInPercent) && _transitionCompleted) {
_brightnessInPercent = brightnessInPercent;
_dirty = true;
}
}
/**
Die aktuelle Helligkeit bekommen.
*/
byte LedDriverNeoPixel::getBrightness() {
return _brightnessInPercent;
}
/**
Anpassung der Groesse des Bildspeichers.
@param linesToWrite Wieviel Zeilen aus dem Bildspeicher sollen
geschrieben werden?
*/
void LedDriverNeoPixel::setLinesToWrite(byte linesToWrite) {
}
/**
Das Display einschalten.
*/
void LedDriverNeoPixel::wakeUp() {
}
/**
Das Display ausschalten.
*/
void LedDriverNeoPixel::shutDown() {
_strip->clear();
_strip->show();
_transitionCompleted = true;
}
/**
Den Dateninhalt des LED-Treibers loeschen.
*/
void LedDriverNeoPixel::clearData() {
_strip->clear();
_strip->show();
}
/**
Einen X/Y-koordinierten Pixel in der Matrix setzen.
*/
void LedDriverNeoPixel::_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 LedDriverNeoPixel::_setPixel(byte num, uint32_t c) {
#ifdef MATRIX_XXL
if (num < 110) {
if ((num / 11) % 2 == 0) {
_strip->setPixelColor(num*2, c);
_strip->setPixelColor(num*2+1, c);
} else {
_strip->setPixelColor(((num / 11) * 22) + 21 - (num % 11)*2, c);
_strip->setPixelColor(((num / 11) * 22) + 21 - ((num % 11)*2 + 1), c);
}
} else {
switch (num) {
case 110:
_strip->setPixelColor(221, c);
break;
case 111:
_strip->setPixelColor(222, c);
break;
case 112:
_strip->setPixelColor(223, c);
break;
case 113:
_strip->setPixelColor(220, c);
break;
case 114: // die Alarm-LED
_strip->setPixelColor(224, c);
break;
default:
;
}
}
#else
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:
;
}
}
#endif
}
/**
Funktion fuer saubere 'Regenbogen'-Farben.
Kopiert aus den Adafruit-Beispielen (strand).
*/
uint32_t LedDriverNeoPixel::_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 LedDriverNeoPixel::_brightnessScaleColor(byte brightness, byte colorPart) {
return map(brightness, 0, 100, 0, colorPart);
}