-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws2811wrapper.cpp
More file actions
583 lines (431 loc) · 14.3 KB
/
ws2811wrapper.cpp
File metadata and controls
583 lines (431 loc) · 14.3 KB
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "ws2811wrapper.h"
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
Ws2811Wrapper::Ws2811Wrapper()
{
std::memset(&_ledstring, '\0', sizeof(_ledstring));
_ledstring.freq = WS2811_TARGET_FREQ;
_ledstring.dmanum = 0;
_ledstring.channel[Channel1].strip_type = WS2811_STRIP_RGB;
_ledstring.channel[Channel1].gpionum = 0;
_ledstring.channel[Channel1].count = 0;
_ledstring.channel[Channel1].invert = 0;
_ledstring.channel[Channel1].brightness = 127;
_ledstring.channel[Channel1].leds = nullptr;
_ledstring.channel[Channel1].gamma = nullptr;
_ledstring.channel[Channel2].strip_type = WS2811_STRIP_RGB;
_ledstring.channel[Channel2].gpionum = 0;
_ledstring.channel[Channel2].count = 0;
_ledstring.channel[Channel2].invert = 0;
_ledstring.channel[Channel2].brightness = 127;
_ledstring.channel[Channel2].leds = nullptr;
_ledstring.channel[Channel2].gamma = nullptr;
_stripTypes[Channel1] = NONE;
_stripTypes[Channel2] = NONE;
_curChannel = Channel1;
_clearOnExit = true;
_matrix[Channel1] = nullptr;
_matrix[Channel2] = nullptr;
_useGammaCorrection[Channel1] = false;
_useGammaCorrection[Channel2] = false;
_rgbMatrix = nullptr;
}
Ws2811Wrapper::~Ws2811Wrapper()
{
if(true == _clearOnExit)
clearLeds();
cleanUpWs2811();
cleanUpRpiMatrix();
}
ws2811_return_t Ws2811Wrapper::show()
{
ws2811_return_t result = WS2811_SUCCESS;
if(_stripTypes[_curChannel] != MATRIX_2121)
{
for (int led = 0; led < _ledstring.channel[_curChannel].count; led++)
{
_ledstring.channel[_curChannel].leds[led] = _matrix[_curChannel][led];
}
result = ws2811_render(&_ledstring);
}
else if(_stripTypes[_curChannel] == MATRIX_2121)
{
int led = 0;
for (uint32_t row = 0; row < _rows[_curChannel] ; row++)
{
for (uint32_t col = 0; col < _columns[_curChannel]; col++)
{
led = (row * _columns[_curChannel]) + col;
_frame->SetPixel(col, row, red(_matrix[_curChannel][led]), green(_matrix[_curChannel][led]), blue(_matrix[_curChannel][led]) );
}
}
_frame = _rgbMatrix->SwapOnVSync(_frame);
}
return result;
}
void Ws2811Wrapper::setCustomGammaCorrection(double gammaFactor)
{
_useGammaCorrection[_curChannel] = (gammaFactor > 0) ? true : false;
if( (_stripTypes[Channel1] != NONE && _stripTypes[Channel2] != NONE) && (_stripTypes[Channel1] != MATRIX_2121 || _stripTypes[Channel2] != MATRIX_2121))
ws2811_set_custom_gamma_factor(&_ledstring, gammaFactor);
else if(_stripTypes[Channel1] == MATRIX_2121)
_frame->set_luminance_correct((gammaFactor <= 0) ? false : true);
}
void Ws2811Wrapper::cleanUpWs2811()
{
if(_matrix[Channel1] != nullptr && _stripTypes[Channel1] != MATRIX_2121 )
{
delete [] _matrix[Channel1];
_matrix[Channel1] = nullptr;
}
if(_matrix[Channel2] != nullptr && _stripTypes[Channel1] != MATRIX_2121 )
{
delete [] _matrix[Channel2];
_matrix[Channel2] = nullptr;
}
if( (_stripTypes[Channel1] != NONE && _stripTypes[Channel2] != NONE) && (_stripTypes[Channel1] != MATRIX_2121 || _stripTypes[Channel2] != MATRIX_2121))
ws2811_fini(&_ledstring);
}
void Ws2811Wrapper::cleanUpRpiMatrix()
{
if(_matrix[Channel1] != nullptr && _stripTypes[Channel1] == MATRIX_2121 )
{
delete [] _matrix[Channel1];
_matrix[Channel1] = nullptr;
}
if(_matrix[Channel2] != nullptr && _stripTypes[Channel2] == MATRIX_2121 )
{
delete [] _matrix[Channel2];
_matrix[Channel2] = nullptr;
}
if(_rgbMatrix != nullptr)
{
_rgbMatrix->Clear();
delete _rgbMatrix;
_rgbMatrix = nullptr;
}
}
void Ws2811Wrapper::setCurChannel(ws2811Channel curChannel)
{
_curChannel = curChannel;
}
ws2811Channel Ws2811Wrapper::getCurChannel() const
{
return _curChannel;
}
ws2811_return_t Ws2811Wrapper::initStrip(ws2811Channel channel, u_int32_t rows, u_int32_t columns, LedStripType stripType, int dma, int gpio, matrixDirection matrixDir)
{
ws2811_return_t retval = WS2811_SUCCESS;
_curChannel = channel;
_columns[_curChannel] = columns;
_rows[_curChannel] = rows;
_matrixDirection[_curChannel] = matrixDir;
if(_ledstring.channel[_curChannel].count > 0)
cleanUpWs2811();
_ledstring.channel[_curChannel].count = _rows[_curChannel] * _columns[_curChannel];
setStripType(stripType);
switch (gpio)
{
case 12:
case 18:
case 40:
case 52:
_ledstring.channel[_curChannel].gpionum = gpio;
break;
default:
retval = WS2811_ERROR_GPIO_INIT;
break;
}
if (dma < 14)
_ledstring.dmanum = dma;
else
retval = WS2811_ERROR_DMA;
if (retval == WS2811_SUCCESS)
{
_matrix[_curChannel] = new ws2811_led_t[sizeof(ws2811_led_t) * (_rows[_curChannel] * _columns[_curChannel])];
retval = ws2811_init(&_ledstring);
}
return retval;
}
void Ws2811Wrapper::PrintOptions(const RGBMatrix::Options &o)
{
#define P_INT(val) fprintf(stderr, "Ws2811Wrapper::initStrip %s : %d\n", #val, o.val)
#define P_STR(val) fprintf(stderr, "Ws2811Wrapper::initStrip %s : %s\n", #val, o.val)
#define P_BOOL(val) fprintf(stderr, "Ws2811Wrapper::initStrip %s : %s\n", #val, o.val ? "true":"false")
P_STR(hardware_mapping);
P_INT(rows);
P_INT(cols);
P_INT(chain_length);
P_INT(parallel);
P_INT(pwm_bits);
P_INT(pwm_lsb_nanoseconds);
P_INT(pwm_dither_bits);
P_INT(brightness);
P_INT(scan_mode);
P_INT(row_address_type);
P_INT(multiplexing);
P_BOOL(disable_hardware_pulsing);
P_BOOL(show_refresh_rate);
P_BOOL(inverse_colors);
P_STR(led_rgb_sequence);
P_STR(pixel_mapper_config);
P_STR(panel_type);
P_INT(limit_refresh_rate_hz);
#undef P_INT
#undef P_STR
#undef P_BOOL
}
ws2811_return_t Ws2811Wrapper::initStrip(u_int32_t rows, u_int32_t columns, LedStripType stripType, matrixDirection matrixDir, Wiring2121 wiring)
{
ws2811_return_t retval = WS2811_SUCCESS;
_curChannel = Channel1;
_columns[_curChannel] = columns;
_rows[_curChannel] = rows;
_wiring[_curChannel] = wiring;
setStripType(stripType);
cleanUpRpiMatrix();
switch(wiring)
{
case Regular:
_led_options.hardware_mapping = "regular";
break;
case Adafruithat:
_led_options.hardware_mapping = "adafruit-hat";
break;
case Adafruithatpwm:
_led_options.hardware_mapping = "adafruit-hat-pwm";
break;
default:
_led_options.hardware_mapping = "regular";
break;
}
// _led_options.pwm_bits = 8;
_led_options.rows = rows;
_led_options.cols = columns;
_led_options.chain_length = 1;
_led_options.parallel = 1;
_led_options.multiplexing = 0;
_led_options.show_refresh_rate = false;
_runtime.drop_privileges = -1;
_runtime.gpio_slowdown = 3;
_runtime.do_gpio_init = true;
// PrintOptions(_led_options); used for debugging.
_matrixDirection[_curChannel] = matrixDir;
_rgbMatrix = CreateMatrixFromOptions(_led_options, _runtime);
if(_rgbMatrix == nullptr)
retval = WS2811_ERROR_GPIO_INIT; //not correct error but will work for now.
if (retval == WS2811_SUCCESS)
{
_frame = _rgbMatrix->SwapOnVSync(_frame);
_matrix[_curChannel] = new ws2811_led_t[sizeof(ws2811_led_t) * (_rows[_curChannel] * _columns[_curChannel])];
}
return retval;
}
void Ws2811Wrapper::setStripType(LedStripType stripType)
{
_stripTypes[_curChannel] = stripType;
switch(stripType)
{
case NEO_WS2811_STRIP_RGB:
_ledstring.channel[_curChannel].strip_type = WS2811_STRIP_RGB;
break;
case NEO_WS2811_STRIP_RBG:
_ledstring.channel[_curChannel].strip_type = WS2811_STRIP_RBG;
break;
case NEO_WS2811_STRIP_GRB:
_ledstring.channel[_curChannel].strip_type = WS2811_STRIP_GRB;
break;
case NEO_WS2811_STRIP_GBR:
_ledstring.channel[_curChannel].strip_type = WS2811_STRIP_GBR;
break;
case NEO_WS2811_STRIP_BRG:
_ledstring.channel[_curChannel].strip_type = WS2811_STRIP_BRG;
break;
case NEO_WS2811_STRIP_BGR:
_ledstring.channel[_curChannel].strip_type = WS2811_STRIP_BGR;
break;
case NEO_SK6812_STRIP_RGBW:
_ledstring.channel[_curChannel].strip_type = SK6812_STRIP_RGBW;
break;
case NEO_SK6812_STRIP_GRBW:
_ledstring.channel[_curChannel].strip_type = SK6812_STRIP_GRBW;
break;
case MATRIX_2121:
break;
default:
//todo logerror
break;
}
}
ws2811_return_t Ws2811Wrapper::clearLeds(bool render)
{
ws2811_return_t retVal = WS2811_SUCCESS;
for (u_int32_t row = 0; row < _rows[_curChannel] ; row++)
{
for (u_int32_t col = 0; col < _columns[_curChannel]; col++)
_matrix[_curChannel][(row * _columns[_curChannel]) + col] = 0;
}
if(true == render)
retVal = show();
return retVal;
}
void Ws2811Wrapper::setClearOnExit(bool clear)
{
_clearOnExit = clear;
}
void Ws2811Wrapper::setPixelColor(ws2811_led_t color)
{
for (u_int32_t row = 0; row < _rows[_curChannel]; row++)
{
for (u_int32_t col = 0; col < _columns[_curChannel]; col++)
{
_matrix[_curChannel][ (row * _columns[_curChannel]) + col] = color;
}
}
}
u_int32_t Ws2811Wrapper::getPixelIndex(u_int32_t row, u_int32_t pixel)
{
u_int32_t index = 0;
switch(_matrixDirection[_curChannel])
{
case TopLeftRight:
index = (row % 2 != 0) ? ((row * _columns[_curChannel]) + pixel) : (row * _columns[_curChannel]) + ((_columns[_curChannel] - 1) - pixel);
break;
case TopRightDown:
index = (pixel % 2 == 0) ? (pixel * _rows[_curChannel]) + row : (pixel * _rows[_curChannel]) + (_rows[_curChannel] - 1) - row;
break;
case BottomRightUp:
index = (pixel % 2 != 0) ? (pixel * _rows[_curChannel]) + row : (pixel * _rows[_curChannel]) + (_rows[_curChannel] - 1) - row;
break;
case TopRightLeft:
index = (row % 2 == 0) ? ((row * _columns[_curChannel]) + pixel) : (row * _columns[_curChannel]) + pixel;
break;
case MatrixTopDown:
index = (row * _columns[_curChannel]) + pixel;
break;
case MatrixTopDownLeft: //very speical case. Only saw on one board.
index = (row * _columns[_curChannel]) + ((_columns[_curChannel] - 1) - pixel);
break;
}
return index;
}
void Ws2811Wrapper::setPixel(u_int32_t pixel, ws2811_led_t value)
{
if( pixel > getNumberLeds() - 1) return;
_matrix[_curChannel][pixel] = value;
}
void Ws2811Wrapper::setPixelColor(u_int32_t row, u_int32_t pixel, ws2811_led_t value)
{
setPixel(getPixelIndex(row, pixel), value );
}
void Ws2811Wrapper::setPixelColor(u_int32_t pixel, ws2811_led_t value)
{
setPixel(pixel, value);
}
void Ws2811Wrapper::setPixelColor(u_int32_t row, u_int32_t pixel, u_int8_t red, u_int8_t green, u_int8_t blue)
{
setPixel(getPixelIndex(row, pixel), color(red, green, blue));
}
void Ws2811Wrapper::setPixelColor(u_int32_t row, u_int32_t pixel, u_int8_t red, u_int8_t green, u_int8_t blue, u_int8_t white)
{
setPixel(getPixelIndex(row, pixel), color(red, green, blue, white));
}
ws2811_led_t Ws2811Wrapper::getPixelColor(u_int32_t row, u_int32_t pixel)
{
if( pixel > getNumberLeds() - 1) return 0;
return getPixelColor(getPixelIndex(row, pixel));
}
ws2811_led_t Ws2811Wrapper::getPixelColor(u_int32_t pixel)
{
if( pixel > getNumberLeds() - 1) return 0;
return _matrix[_curChannel][pixel];
}
void Ws2811Wrapper::setBrightness(u_int8_t brightness)
{
_brightness[_curChannel] = brightness;
if(_stripTypes[_curChannel] != MATRIX_2121)
{
_ledstring.channel[_curChannel].brightness = brightness;
}
else
{
_rgbMatrix->SetBrightness((brightness > 100 ) ? 100 : brightness );
}
}
u_int32_t Ws2811Wrapper::getNumberLeds()
{
return (_stripTypes[_curChannel] != MATRIX_2121) ? _ledstring.channel[_curChannel].count : _columns[_curChannel] * _rows[_curChannel];
}
u_int32_t Ws2811Wrapper::getColumns()
{
return _columns[_curChannel];
}
u_int32_t Ws2811Wrapper::getRows()
{
return _rows[_curChannel];
}
ws2811_led_t Ws2811Wrapper::color(u_int8_t red, u_int8_t green, u_int8_t blue)
{
return ((u_int32_t) red << 16) | ((u_int32_t) green << 8) | blue;
}
ws2811_led_t Ws2811Wrapper::color(u_int8_t red, u_int8_t green, u_int8_t blue, u_int8_t white)
{
return ((u_int32_t) white << 24) | ((u_int32_t) red << 16) | ((u_int32_t) green << 8) | blue;
}
ws2811_led_t Ws2811Wrapper::wheel(u_int8_t wheelPos)
{
wheelPos = 255 - wheelPos;
if(wheelPos < 85)
{
return color(255 - wheelPos * 3, 0, wheelPos * 3);
}
if(wheelPos < 170)
{
wheelPos -= 85;
return color(0, wheelPos * 3, 255 - wheelPos * 3);
}
wheelPos -= 170;
return color(wheelPos * 3, 255 - wheelPos * 3, 0);
}
int Ws2811Wrapper::red(ws2811_led_t value)
{
return (value >> 16) & 0xFF;
}
int Ws2811Wrapper::green(ws2811_led_t value)
{
return (value >> 8) & 0xFF;
}
int Ws2811Wrapper::blue(ws2811_led_t value)
{
return value & 0xFF;
}
ws2811_led_t Ws2811Wrapper::dimColor(ws2811_led_t value)
{
ws2811_led_t dimColor = color(red(value) >> 1, green(value) >> 1, blue(value) >> 1);
return dimColor;
}
ws2811_led_t Ws2811Wrapper::brightenColor(ws2811_led_t value)
{
ws2811_led_t dimColor = color(red(value) << 1, green(value) << 1, blue(value) << 1);
return dimColor;
}
void Ws2811Wrapper::waitSec(u_int32_t sec)
{
timespec deadline;
deadline.tv_sec = sec;
deadline.tv_nsec = 0;
clock_nanosleep(CLOCK_REALTIME, 0 , &deadline, NULL);
}
void Ws2811Wrapper::waitMillSec(u_int32_t mill)
{
timespec deadline;
deadline.tv_sec = mill / 1000;
deadline.tv_nsec = (mill * 1000000) % 1000000000;
clock_nanosleep(CLOCK_REALTIME, 0 , &deadline, NULL);
}
const char * Ws2811Wrapper::getws2811ErrorString(const ws2811_return_t state)
{
return ws2811_get_return_t_str(state);
}