-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathLedStates.h
82 lines (71 loc) · 1.77 KB
/
LedStates.h
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
/*
Released under Creative Commons Attribution 4.0
by bitluni 2016
https://creativecommons.org/licenses/by/4.0/
Attribution means you can use it however you like as long you
mention that it's base on my stuff.
I'll be pleased if you'd do it by sharing http://youtube.com/bitlunislab
*/
#include <ESP8266WebServer.h>
#include "LedFunction.h"
const int MAX_LED_COUNT = 300;
class LedStates
{
public:
uint8_t values[MAX_LED_COUNT][3] = {{0}};
int count = 0;
bool dirty = false;
Adafruit_NeoPixel &pixels;
LedFunction *function = 0;
LedStates(Adafruit_NeoPixel &ledPixels)
:pixels(ledPixels)
{
count = pixels.numPixels();
}
void setFunction(LedFunction *newFunction)
{
if(function)
delete function;
function = newFunction;
if(!function)
return;
function->state = this;
}
void setRgb(int i, uint8_t r, uint8_t g, uint8_t b)
{
values[i][0] = r;
values[i][1] = g;
values[i][2] = b;
dirty = true;
}
virtual void render()
{
if(function)
function->render();
}
void setValues(LedStates &to)
{
for(int i = 0; i < count; i++)
for(int j = 0; j < 3; j++)
values[i][j] = to.values[i][j];
setFunction(to.function);
to.function = 0;
dirty = true;
}
void commit()
{
if(!dirty)
return;
for(int i = 0; i < count; i++)
pixels.setPixelColor(i, pixels.Color(values[i][0], values[i][1], values[i][2]));
pixels.show();
dirty = false;
}
void fade(LedStates &to, long f0, long f1)
{
for(int i = 0; i < count; i++)
pixels.setPixelColor(i, pixels.Color((values[i][0] * f0 + to.values[i][0] * f1) >> 16, (values[i][1] * f0 + to.values[i][1] * f1) >> 16, (values[i][2] * f0 + to.values[i][2] * f1) >> 16));
pixels.show();
dirty = true;
}
};