-
Notifications
You must be signed in to change notification settings - Fork 0
/
neopixel.cpp
649 lines (538 loc) · 17.7 KB
/
neopixel.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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "pico/stdlib.h"
#include "pico/time.h"
#include "hardware/clocks.h"
#include "ws2812.pio.h"
#include "neopixel.hpp"
#define IS_RGBW true
#define WS2812_PIN 7
NeopixelGrid grid;
/// @brief Utility function to add 2 RGB values together.
/// @param c1
/// @param c2
/// @return
static uint32_t addRgb(uint32_t c1, uint32_t c2){
uint32_t r = (c1 & 0xFF0000) + (c2 & 0xFF0000);
uint32_t g = (c1 & 0x00FF00) + (c2 & 0x00FF00);
uint32_t b = (c1 & 0x0000FF) + (c2 & 0x0000FF);
if(r > 0xFF0000) r = 0xFF0000;
if(g > 0x00FF00) g = 0x00FF00;
if(b > 0x0000FF) b = 0x0000FF;
return r | g | b;
}
class NullAction: public Action {
public:
virtual void tick(){}
virtual void start(NeopixelGrid* grid, Command* cmd) {}
};
class SetAction: public Action {
public:
virtual void tick(){}
virtual void start(NeopixelGrid* grid, Command* cmd) {grid->set(cmd->params[0], cmd->params[1]);}
};
////////////////////////////////////////////////////////////////////////////////////////////
class ColourChangeAction: public Action {
float hue; // current hue
float value; // how bright the RGB is
float increment; // for hue per tick
uint8_t white; // white LED value.
NeopixelGrid* grid;
public:
virtual void tick();
virtual void start(NeopixelGrid* grid, Command* cmd);
};
void ColourChangeAction::tick(){
hue = hue + increment;
if(hue > 1.0) hue = 0;
uint32_t rgb = grid->hsvToRgb(hue, 1.0, value);
grid->set(rgb, white);
}
void ColourChangeAction::start(NeopixelGrid* grid, Command* cmd){
this->grid = grid;
this->hue = 0;
this->value = (1.0f / SCALE) * (float)cmd->params[0];
this->increment = (1.0f/ SCALE) * (float)cmd->params[1];
this->white = cmd->params[2];
}
////////////////////////////////////////////////////////////////////////////////////////////
class SparkleAction: public Action {
NeopixelGrid* grid;
public:
virtual void tick();
virtual void start(NeopixelGrid* grid, Command* cmd);
};
void SparkleAction::tick(){
for (int i = 0; i < PIXEL_COUNT; ++i){
grid->setPixel(i, rand() % 16 ? 0 : 0xffffffff, 0);
}
grid->send();
}
void SparkleAction::start(NeopixelGrid* grid, Command* cmd){
this->grid = grid;
}
////////////////////////////////////////////////////////////////////////////////////////////
// Utility base class for actions
class ActionBase: public Action {
protected:
const static int SINE_LENGTH = 1000;
static float sinfLookup[SINE_LENGTH];
float hue; // colour to animate.
float hue2; // colour to animate.
float value; // how bright the RGB is 0..1
float count; // number of ripples
uint8_t white; // white LED value.
NeopixelGrid* grid;
int baseIndices[PIXEL_COUNT]; // precalculated from radius & count.
int phaseIndex;
int inc;
void readParameters(NeopixelGrid* grid, Command* cmd);
public:
ActionBase();
virtual void tick();
virtual void start(NeopixelGrid* grid, Command* cmd) =0;
static void show(); // debug
};
float ActionBase::sinfLookup[ActionBase::SINE_LENGTH];
ActionBase::ActionBase()
: hue(0)
, hue2(1)
, value(1)
, count(1)
, white(0)
, grid(0)
, phaseIndex(0)
, inc(0)
{
for(int i=0; i<SINE_LENGTH; ++i){
sinfLookup[i] = (1.0f + sinf(i * 2.0f * (float)M_PI / (float)SINE_LENGTH))/2;
}
}
void ActionBase::readParameters(NeopixelGrid* grid, Command* cmd){
this->grid = grid;
this->hue = (1.0f / SCALE) * (float)cmd->params[0];
this->hue2 = (1.0f / SCALE) * (float)cmd->params[1];
this->value = (1.0f / SCALE) * (float)cmd->params[2];
this->inc = cmd->params[3];
this->count = (1.0f / SCALE) * (float)cmd->params[4];
this->white = cmd->params[5];
if(hue < 0) hue = 0; else if(hue > 1) hue = 1.0f;
if(hue2 < 0) hue2 = -1.0f; else if(hue2 > 1) hue2 = 1.0f;
if(value < 0) value = 0; else if(value > 1) value = 1.0f;
if(inc < -500) inc = 500; else if(inc > 500) inc = 500;
if(count < 0) count = 0; else if(count > 10) count = 10;
// ignore white - will truncate anyway.
}
void ActionBase::tick(){
phaseIndex -= inc;
if(phaseIndex < 0)
phaseIndex += SINE_LENGTH;
else if(phaseIndex >= SINE_LENGTH)
phaseIndex -= SINE_LENGTH;
// So we've got a lookup in baseIndices of the index for each pixel of where they are
// in the sine array before the ripple effect is added. Add phaseIndex (varying) to
// get the actual position and then lookup the sine value. This avoids a lot
// of floating point.
//absolute_time_t t0 = get_absolute_time();
for(int i=0; i<PIXEL_COUNT; ++i){
int idx = baseIndices[i] + phaseIndex;
while(idx >= SINE_LENGTH) idx -= SINE_LENGTH;
float v1 = sinfLookup[idx]; // range 0..1
float v2 = 1.0f - v1;
uint32_t rgb = grid->hvToRgb(hue, v1*value);
if(hue2 >= 0){
rgb = addRgb(rgb, grid->hvToRgb(hue2, v2*value));
}
grid->setPixel(i, rgb, white);
}
//absolute_time_t t1 = get_absolute_time();
grid->send();
//absolute_time_t t2 = get_absolute_time();
//printf("T0-T1: %lu, T1-T2: %lu\n", (unsigned long)absolute_time_diff_us(t0,t1), (unsigned long)absolute_time_diff_us(t1,t2));
}
////////////////////////////////////////////////////////////////////////////////////////////
class RipplesAction: public ActionBase {
public:
RipplesAction();
virtual void start(NeopixelGrid* grid, Command* cmd);
};
RipplesAction::RipplesAction() : ActionBase() {}
void RipplesAction::start(NeopixelGrid* grid, Command* cmd){
readParameters(grid, cmd);
// Set up basic ripple effect indices based on radius & count.
// Indexing into the sinf array with the phase offset gives the value of the pixel.
for(int i=0; i<PIXEL_COUNT; ++i){
const Coordinate& c = grid->coordinate(i);
int idx = int(c.r * count * SINE_LENGTH) % SINE_LENGTH;
baseIndices[i] = idx;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
class HorizontalAction: public ActionBase {
public:
HorizontalAction();
virtual void start(NeopixelGrid* grid, Command* cmd);
};
HorizontalAction::HorizontalAction() : ActionBase() {}
void HorizontalAction::start(NeopixelGrid* grid, Command* cmd){
readParameters(grid, cmd);
// Set up basic ripple effect indices based on x coordinate.
// X is normalised from -1 to 1 so convert into range 0..1
// Indexing into the sinf array with the phase offset gives the value of the pixel.
for(int i=0; i<PIXEL_COUNT; ++i){
const Coordinate& c = grid->coordinate(i);
int idx = int((1.0f + c.x)/2 * count * SINE_LENGTH) % SINE_LENGTH;
baseIndices[i] = idx;
}
}
class VerticalAction: public ActionBase {
public:
VerticalAction();
virtual void start(NeopixelGrid* grid, Command* cmd);
};
VerticalAction::VerticalAction() : ActionBase() {}
void VerticalAction::start(NeopixelGrid* grid, Command* cmd){
readParameters(grid, cmd);
// Set up basic ripple effect indices based on radius & count.
// Indexing into the sinf array with the phase offset gives the value of the pixel.
for(int i=0; i<PIXEL_COUNT; ++i){
const Coordinate& c = grid->coordinate(i);
int idx = int((1.0f + c.y)/2 * count * SINE_LENGTH) % SINE_LENGTH;
baseIndices[i] = idx;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
class SpokesAction: public ActionBase {
public:
SpokesAction();
virtual void start(NeopixelGrid* grid, Command* cmd);
};
SpokesAction::SpokesAction() : ActionBase() {}
void SpokesAction::start(NeopixelGrid* grid, Command* cmd){
readParameters(grid, cmd);
// Set up basic ripple effect indices based on angle & count.
// Indexing into the sinf array with the phase offset gives the value of the pixel.
// angle is -PI to +PI. Want that range to sweep through SINE_LENGTH and restart to
// zero. So Theta/PI sweeps from -1 to +1, (Theta/PI + 1)/2 => 0..1
for(int i=0; i<PIXEL_COUNT; ++i){
const Coordinate& c = grid->coordinate(i);
int idx = int( (c.theta / (float)M_PI + 1.0f)/2 * count * SINE_LENGTH) % SINE_LENGTH; // theta -pi to pi
baseIndices[i] = idx;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
NullAction nullAction;
SetAction setAction;
ColourChangeAction colourChangeAction;
SparkleAction sparkleAction;
RipplesAction ripplesAction;
SpokesAction spokesAction;
HorizontalAction horizontalAction;
VerticalAction verticalAction;
////////////////////////////////////////////////////////////////////////////////////////////
NeopixelGrid::NeopixelGrid()
: currentAction(0)
, cycle_time(1)
, cycle(1)
, pio(pio0)
, sm(0)
, pixels(buffer)
{
// Setup PIO
pio_sm_claim(pio, sm); // check not used by any other library in the future.
uint offset = pio_add_program(pio, &ws2812_program);
ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW);
// Setup DMA for writing to pixels.
DmaConfig cfg = dma.getDefaultConfig();
cfg.bswap(false);
cfg.transferDataSize(DMA_SIZE_32);
cfg.dreq(pio_get_dreq(pio,sm,true));
cfg.enable(true);
cfg.readIncrement(true);
cfg.writeIncrement(false);
dma.configure(cfg, &pio->txf[sm], 0, 64);
// Zero the pixel buffer.
for(int i=0; i<PIXEL_COUNT*2; ++i){
buffer[i] = 0;
}
queue_init( &commandQueue, sizeof(Command), 16);
initialiseCoordinates();
}
// Precalculate coordinates of each pixel on a -1..1 grid in both cartesian and polar
// values. These are for the animations.
// Pixels at points - 8 pixels but only 7 units between them so logically we have
// the following for uniform spacing.
// 0 -> -3.5
// 1 -> -2.5
// 2 -> -1.5
// 3 -> -0.5
// 4 -> +0.5
// 5 -> 1.5
// 6 -> 2.5
// 7 -> 3.5
// We then normalise the range to lie in -1 to +1 at the edges(approx) and roughly sqrt(2)
// in the corners.
void NeopixelGrid::initialiseCoordinates(){
int idx = 0;
for(int iy=0; iy<GRID_HEIGHT; ++iy){
float y = (-3.5f + iy) * (1.0f / 3.5f);
for(int ix=0; ix<GRID_WIDTH; ++ix){
float x = (-3.5f + ix) * (1.0f / 3.5f);
float radius = std::sqrt(x*x + y*y);
float theta = std::atan2(y,x);
coordinates[idx].x = x;
coordinates[idx].y = y;
coordinates[idx].r = radius;
coordinates[idx].theta = theta;
++idx;
}
}
}
uint32_t NeopixelGrid::hsvToRgb(float hue, float saturation, float value){
float r, g, b;
if(hue > 1.0) hue = 1.0;
if(saturation > 1.0) saturation = 1.0;
if(value > 1.0) value = 1.0;
int h = (int)(hue * 6);
float f = hue * 6 - h;
float p = value * (1 - saturation);
float q = value * (1 - f * saturation);
float t = value * (1 - (1 - f) * saturation);
if (h == 0) {
r = value;
g = t;
b = p;
} else if (h == 1) {
r = q;
g = value;
b = p;
} else if (h == 2) {
r = p;
g = value;
b = t;
} else if (h == 3) {
r = p;
g = q;
b = value;
} else if (h == 4) {
r = t;
g = p;
b = value;
} else if (h <= 6) {
r = value;
g = p;
b = q;
} else {
assert(false);
}
uint32_t rgb =
((int)(r * 255) << 16) +
((int)(g * 255) << 8) +
(int (b * 255));
return rgb;
}
uint32_t NeopixelGrid::hvToRgb(float hue, float value){
float r, g, b;
if(hue > 1.0) hue = 1.0;
if(value > 1.0) value = 1.0;
int h = (int)(hue * 6);
float f = hue * 6 - h;
float p = 0;
float q = value * (1 - f );
float t = value * f;
if (h == 0) {
r = value;
g = t;
b = p;
} else if (h == 1) {
r = q;
g = value;
b = p;
} else if (h == 2) {
r = p;
g = value;
b = t;
} else if (h == 3) {
r = p;
g = q;
b = value;
} else if (h == 4) {
r = t;
g = p;
b = value;
} else if (h <= 6) {
r = value;
g = p;
b = q;
} else {
assert(false);
}
uint32_t rgb =
((int)(r * 255) << 16) +
((int)(g * 255) << 8) +
(int (b * 255));
return rgb;
}
/// @brief Sends the array of pixels to the display.
void NeopixelGrid::send(){
while(dma.isBusy()){
::sleep_ms(1);
}
//dma.waitForFinish();
dma.fromBufferNow((void*)pixels, 64);
// Flip buffer pointer for double buffering so we don't
// write into the memory the DMA is writing.
if(pixels == buffer) {
pixels = buffer + PIXEL_COUNT;
} else {
pixels = buffer;
}
// Old implementation.
// for(int i=0; i<PIXEL_COUNT; ++i){
// pio_sm_put_blocking(pio0, 0, pixels[i]);
// }
}
void NeopixelGrid::set(uint32_t rgb, uint8_t white){
this->colour = rgb;
this->white = white;
uint8_t r = (rgb & 0xFF0000)>>16;
uint8_t g = (rgb & 0x00FF00)>>8;
uint8_t b = (rgb & 0x0000FF);
uint32_t pixel = rgbw(r,g,b,white);
for(int i=0; i<PIXEL_COUNT; ++i){
pixels[i] = pixel;
}
send();
}
/// @brief Sets an individual pixel.
/// @param idx
/// @param rgb
/// @param white
void NeopixelGrid::setPixel(int idx, uint32_t rgb, uint8_t white){
assert(idx >= 0 && idx < PIXEL_COUNT);
uint8_t r = (rgb & 0xFF0000)>>16;
uint8_t g = (rgb & 0x00FF00)>>8;
uint8_t b = (rgb & 0x0000FF);
pixels[idx] = rgbw(r,g,b,white);
}
void NeopixelGrid::setPixelRaw(int idx, uint32_t rgbw){
assert(idx >= 0 && idx < PIXEL_COUNT);
pixels[idx] = rgbw;
}
void NeopixelGrid::tick() {
if(!queue_is_empty(&commandQueue)){
Command cmd;
if(queue_try_remove(&commandQueue,&cmd)){
switch(cmd.code){
case 0: // Turn off any animation
currentAction = 0;
break;
case 1: // set colours of whole grid
set(cmd.params[0], cmd.params[1]);
currentAction = 0;
break;
case 2: // set cycle time for animation.
cycle_time = cmd.params[0];
cycle = cycle_time;
break;
case 3: // Colour cycle.
currentAction = &colourChangeAction;
colourChangeAction.start(this, &cmd);
break;
case 4: // ripples
currentAction = &ripplesAction;
ripplesAction.start(this, &cmd);
break;
case 5: // spokes
currentAction = &spokesAction;
spokesAction.start(this, &cmd);
break;
case 6: // horizontal
currentAction = &horizontalAction;
horizontalAction.start(this, &cmd);
break;
case 7: // vertical
currentAction = &verticalAction;
verticalAction.start(this, &cmd);
break;
case 8:
currentAction = &sparkleAction;
sparkleAction.start(this, &cmd);
break;
}
}
}
--cycle;
if(cycle == 0){
if(currentAction){
currentAction->tick();
}
cycle = cycle_time;
}
}
bool NeopixelGrid::run(Command* cmd){
bool success = queue_try_add(&commandQueue, cmd);
return success;
}
void run_neopixel() {
grid.send();
while(true) {
grid.tick();
::sleep_ms(1);
}
}
void NeopixelGrid::setAsync(uint32_t rgb, uint8_t white){
Command cmd;
cmd.code = 1; // set colour
cmd.params[0] = rgb;
cmd.params[1] = white;
run(&cmd);
}
void NeopixelGrid::rateAsync(unsigned int rate){
Command cmd;
cmd.code = 2; // set rate
cmd.params[0] = rate;
run(&cmd);
}
void NeopixelGrid::colourChangeAsync(float value, float increment, uint8_t white){
Command cmd;
cmd.code = 3; // colour change
cmd.params[0] = (int32_t)(value * SCALE);
cmd.params[1] = (int32_t)(increment * SCALE);
cmd.params[2] = white;
run(&cmd);
}
void NeopixelGrid::sendRippleCmd(uint16_t code, float hue, float hue2, float value, float increment, float count, uint8_t white){
Command cmd;
cmd.code = code;
cmd.params[0] = (int32_t)(hue * SCALE);
cmd.params[1] = (int32_t)(hue2 * SCALE);
cmd.params[2] = (int32_t)(value * SCALE);
cmd.params[3] = increment;
cmd.params[4] = (int32_t)(count * SCALE);
cmd.params[5] = white;
run(&cmd);
}
void NeopixelGrid::rippleAsync(float hue, float hue2, float value, float increment, float count, uint8_t white){
sendRippleCmd(4, hue, hue2, value, increment, count, white);
}
void NeopixelGrid::spokesAsync(float hue, float hue2, float value, float increment, float count, uint8_t white){
sendRippleCmd(5, hue, hue2, value, increment, count, white);
}
void NeopixelGrid::horizontalAsync(float hue, float hue2, float value, float increment, float count, uint8_t white){
sendRippleCmd(6, hue, hue2, value, increment, count, white);
}
void NeopixelGrid::verticalAsync(float hue, float hue2, float value, float increment, float count, uint8_t white){
sendRippleCmd(7, hue, hue2, value, increment, count, white);
}
void NeopixelGrid::sparkleAsync(){
Command cmd;
cmd.code = 8; // sparkle
run(&cmd);
}