-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light.cpp
243 lines (214 loc) · 6.05 KB
/
Light.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
#include "Light.h"
void Light::update() {
(this->*_prog)(_params[1]);
_count++;
}
CHSV Light::getHsv() { return rgb2hsv_approximate(_color); }
CRGB Light::getRgb() { return _color; }
int Light::getParam(int p) { return _params[p]; }
String Light::getName() { return _name; }
void Light::turnOn() { _onoff = 1; }
void Light::turnOff() { _onoff = 0; }
void Light::turn(int onoff) { _onoff = onoff; }
void Light::toggle() { _onoff = (_onoff==1? 0 : 1); }
void Light::setRgb(CRGB color) { _color = color; }
void Light::setHsv(int hue, int sat, int val) { _color = CHSV(hue, sat, val); }
void Light::setHsv(CHSV color) { _color = color; }
void Light::setColor(String color) {
// TODO: make this a mapping or look to see if there's a function in FastLED
if (color == "red") _color = CRGB::Red;
if (color == "orange") _color = CRGB::Orange;
if (color == "blue") _color = CRGB::Blue;
if (color == "green") _color = CRGB::Green;
if (color == "black") _color = CRGB::Black;
if (color == "white") _color = CRGB::White;
}
void Light::setHue(int val) {
CHSV hsv_color = getHsv();
hsv_color.h = val;
setHsv(hsv_color);
}
void Light::setSaturation(int val) {
CHSV hsv_color = getHsv();
hsv_color.s = min(val, 100);
setHsv(hsv_color);
}
void Light::setBrightness(int val) {
CHSV hsv_color = getHsv();
hsv_color.v = min(val, 100);
setHsv(hsv_color);
}
void Light::setProgram(String progName) {
if (progName == "solid") _prog = &Light::_prog_solid;
if (progName == "chase") {
_prog = &Light::_prog_chase;
_params[1] = _params[1]? _params[1] : 35;
}
if (progName == "fade") _prog = &Light::_prog_fade;
if (progName == "blink") _prog = &Light::_prog_blink;
if (progName == "warm") {
_prog = &Light::_prog_warm;
_params[0] = 50;
}
if (progName == "lfo") _prog = &Light::_prog_lfo;
}
void Light::setParams(int* params) {
for (int i=0; i<sizeof(params); i++) {
_params[i] = params[i];
}
}
void Light::setParam(int p, int v) {
_params[p] = v;
}
void Light::setState(State* state) {
try {
Serial << "Setting light state: " << "[" << state->name << ": ";
if (state->onoff != -1) {
Serial << (state->onoff==1? "On" : "Off") << "|";
turn(state->onoff);
}
if (state->program != "") {
Serial << "Prog:" << state->program << "|";
setProgram(state->program);
}
if (state->params != nullptr) {
Serial << "Params...|";
setParams(state->params);
}
if (state->color != "") {
Serial << "Color:" << state->color;
setColor(state->color);
}
if (state->count != -1) {
_count = state->count;
}
Serial << "]\n";
} catch (int e) {
Serial << "An Exception occurred: " << e << "\n";
}
}
void Light::blink() {
for (int i=0; i<_num_leds; i++) {
*(CRGB*)_leds[i] = CRGB::White;
}
update();
delay(25);
for (int i=0; i<_num_leds; i++) {
*(CRGB*)_leds[i] = CRGB::Black;
}
update();
}
// programs
int Light::_prog_solid(int x) {
for (int i=0; i<_num_leds; i++) {
*_leds[i] = _color;
}
return 0;
}
int Light::_prog_fade(int x) {
for(int i=0; i<_num_leds; i++) {
_leds[i]->fadeToBlackBy(x);
}
return 0;
}
int Light::_prog_fadein(int x) {
bool still_fading = false;
for(int i=0; i<_num_leds; i++) {
*_leds[i] = fadeTowardColor(*_leds[i], _color, x);
if (*_leds[i] != _color) still_fading = true;
}
if (!still_fading) _prog = &Light::_prog_solid;
return 0;
}
int Light::_prog_fadeout(int x) {
bool still_fading = false;
for(int i=0; i<_num_leds; i++) {
_leds[i]->fadeToBlackBy(x);
if (*_leds[i]) still_fading = true;
}
if (!still_fading) _onoff = false;
return 0;
}
int Light::_prog_chase(int x) {
// params: 0: Chase Speed
// 1: Fade Speed
_prog_fade(_params[1]);
*_leds[_count%_num_leds] = _color;
return 0;
}
int Light::_prog_warm(int x) {
if (_count%7 == 0) _prog_fade(10);
if (_count%11 == 0) {
_index = random(_num_leds);
CHSV wc = rgb2hsv_approximate(_color);
wc.h = wc.h + random(11)-5;
wc.s = random(128)+128;
wc.v &=x;
_color = wc;
}
*_leds[_index] += _color;
return 0;
}
int Light::_prog_xmas(int x) {
if (_count%7 == 0) _prog_fade(1);
if (_count%11 == 0) {
_index = random(_num_leds);
CHSV wc = rgb2hsv_approximate(_color);
wc.h = wc.h + random(11)-5;
wc.s = random(128)+128;
wc.v &=x;
_color = wc;
}
*_leds[_index] += _color;
return 0;
}
int Light::_prog_lfo(int x) {
int wc = _color;
wc%=(int)round((sin(_count*3.14/180)+0.5)*255);
for(int i=0; i<_num_leds; i++) {
*_leds[i] = wc;
}
return 0;
}
int Light::_prog_longfade(int x) {
bool still_fading = false;
if(_count%10 == 0) {
for(int i=0; i<_num_leds; i++) {
_leds[i]->fadeToBlackBy(1);
if (*_leds[i]) still_fading = true;
}
if (!still_fading) _onoff = false;
}
return 0;
}
int Light::_prog_blink(int x) {
_prog_fade(25);
if (!x) x = 25;
if (_count%x == 0) {
for(int i=0; i<_num_leds; i++) {
*_leds[i] = _color;
}
}
return 0;
}
// Helper Functions
CRGB fadeTowardColor(CRGB cur, CRGB target, uint8_t x) {
CRGB newc;
newc.red = nblendU8TowardU8(cur.red, target.red, x);
newc.green = nblendU8TowardU8(cur.green, target.green, x);
newc.blue = nblendU8TowardU8(cur.blue, target.blue, x);
return newc;
}
uint8_t nblendU8TowardU8(uint8_t cur, const uint8_t target, uint8_t x) {
uint8_t newc;
if (cur == target) return cur;
if (cur < target) {
uint8_t delta = target - cur;
delta = scale8_video(delta, x);
return cur + delta;
} else {
uint8_t delta = cur - target;
delta = scale8_video(delta, x);
return cur - delta;
}
}