-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodes.cpp
395 lines (310 loc) · 9.23 KB
/
modes.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
#include "modes.h"
// Sets the given NeoPixel to be used
// params:
// p - NeoPixel object
void ModeHandler::setupModes(Adafruit_NeoPixel& p) {
pixels = p;
EEPROM.get(0, firstPowerOn);
// 0xAA, because it's 1010 1010 in binary
// Special pattern enough to not be accidental
// and memory garbage
if (firstPowerOn == 0xAA) {
// Not first power on
EEPROM.get(1, currentModeNum);
EEPROM.get(2, currentBrightness);
EEPROM.get(3, fixColor_CurrentColor);
EEPROM.get(4, colorCycle_ChangeSpeed);
EEPROM.get(6, colorCycle_ModePos);
EEPROM.get(8, rainbow_ChangeSpeed);
EEPROM.get(10, rainbow_ModePos);
EEPROM.get(12, breathe_CurrentColor);
} else {
// First power on
//firstPowerOn
EEPROM.put(0, 0xAA);
//currentModeNum
EEPROM.put(1, (byte)0);
//currentBrightness
EEPROM.put(2, (byte)50);
//fixColor_CurrentColor
EEPROM.put(3, (byte)0);
//colorCycle_ChangeSpeed
EEPROM.put(4, (unsigned short)50);
//colorCycle_ModePos
EEPROM.put(6, (short)0);
//rainbow_ChangeSpeed
EEPROM.put(8, (unsigned short)40);
//rainbow_ModePos
EEPROM.put(10, (short)0);
//breathe_CurrentColor
EEPROM.put(12, (byte)0);
#ifdef ESP8266
EEPROM.commit();
#endif
}
}
// Resets all settings to the default value.
void ModeHandler::factoryReset() {
EEPROM.write(0, 0);
setupModes(pixels);
isModeChanged = true;
}
// Switches to the next lighting mode
void ModeHandler::nextMode() {
currentModeNum = (currentModeNum + 1) % TOTAL_MODE_NUM;
isModeChanged = true;
lastModeChanged = millis();
debugLog("Next Mode - #");
debugLogln(currentModeNum);
}
// Updates lighting on current mode
void ModeHandler::updateMode() {
if (pixelPowerState) {
switch(currentModeNum) {
// Full white
case 0:
if (isModeChanged) {
effectFullWhiteUpdate();
}
break;
// Fix color
case 1:
if (isModeChanged) {
effectFixColorUpdate();
}
break;
// Color cycle
case 2:
effectColorCycleUpdate();
break;
// Rainbow
case 3:
effectRainbowUpdate();
break;
// Breathe
case 4:
effectBreatheUpdate();
break;
// Fire
case 5:
effectFireUpdate();
break;
}
} else {
// Turned-off lights
pixels.clear();
}
pixels.show();
if (isModeChanged == true && millis() - lastModeChanged >= 3000) {
uint8_t t = 0;
EEPROM.get(1, t);
if (t != currentModeNum) {
EEPROM.put(1, currentModeNum);
}
EEPROM.get(2, t);
if (t != currentBrightness) {
EEPROM.put(2, currentBrightness);
}
EEPROM.get(3, t);
if (t != fixColor_CurrentColor) {
EEPROM.put(3, fixColor_CurrentColor);
}
EEPROM.get(4, t);
if (t != colorCycle_ChangeSpeed) {
EEPROM.put(4, colorCycle_ChangeSpeed);
}
EEPROM.get(6, t);
if (t != colorCycle_ModePos) {
EEPROM.put(6, colorCycle_ModePos);
}
EEPROM.get(8, t);
if (t != rainbow_ChangeSpeed) {
EEPROM.put(8, rainbow_ChangeSpeed);
}
EEPROM.get(10, t);
if (t != rainbow_ModePos) {
EEPROM.put(10, rainbow_ModePos);
}
EEPROM.get(12, t);
if (t != breathe_CurrentColor) {
EEPROM.put(12, breathe_CurrentColor);
}
#ifdef ESP8266
EEPROM.commit();
#endif
debugLogln("Saved!");
isModeChanged = false;
}
}
// Update encoder position for current mode
// params:
// encoderPos - new rotary encoder position to update to
void ModeHandler::updateModePos(long encoderPos) {
modePosChange = encoderPos - currentEncoderPos;
switch(currentModeNum) {
// Full white
case 0:
effectFullWhitePosUpdate();
break;
// Fix color
case 1:
effectFixColorPosUpdate(modePosChange);
break;
// Color cycle
case 2:
effectColorCyclePosUpdate(modePosChange);
break;
// Rainbow
case 3:
effectRainbowPosUpdate(modePosChange);
break;
// Breathe
case 4:
effectBreathePosUpdate(modePosChange);
break;
// Fire
case 5:
effectFirePosUpdate();
break;
}
currentEncoderPos = encoderPos;
isModeChanged = true;
lastModeChanged = millis();
}
// Toggle power state of the NeoPixel strip
void ModeHandler::togglePowerState() {
pixelPowerState = !pixelPowerState;
isModeChanged = true;
lastModeChanged = millis();
debugLog("Toggle Power - ");
debugLogln(pixelPowerState?"ON":"OFF");
}
// Update brightness according to the given encoder position
// params:
// encoderPos - new encoder position to update to
void ModeHandler::updateBrightness(long encoderPos) {
if (encoderPos - currentEncoderPos <= 0 && currentBrightness <= (currentEncoderPos - encoderPos) * 3) {
currentBrightness = 0;
} else {
currentBrightness += (encoderPos - currentEncoderPos) * 3;
if (currentBrightness > 100) {
currentBrightness = 100;
}
}
debugLog("New Brightness: ");
debugLogln(currentBrightness);
currentEncoderPos = encoderPos;
isModeChanged = true;
lastModeChanged = millis();
}
// Sets a pixels color with the given brightness
// params:
// n - pixel number starting from 0
// red - red intensity ranging 0-255
// green - green intensity ranging 0-255
// blue - blue intensity ranging 0-255
// brightness - brightness of the color in percents ranging 0-100
void ModeHandler::setPixelWithBrightness(uint16_t n, uint8_t red, uint8_t green, uint8_t blue, uint8_t brightness) {
if (brightness > 100) {
brightness = 100;
}
float b = brightness / 100.0;
pixels.setPixelColor(n, (uint8_t)(red * b), (uint8_t)(green * b), (uint8_t)(blue * b));
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t ModeHandler::colorWheelWithBrightness(byte WheelPos, uint8_t brightness) {
if (brightness > 100) {
brightness = 100;
}
float b = brightness / 100.0;
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return pixels.Color((255 - WheelPos * 3) * b, 0, (WheelPos * 3) * b);
}
if(WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, (WheelPos * 3) * b, (255 - WheelPos * 3) * b);
}
WheelPos -= 170;
return pixels.Color((WheelPos * 3) * b, (255 - WheelPos * 3) * b, 0);
}
/*
* MODES
*/
// Full white effect
void ModeHandler::effectFullWhiteUpdate() {
for (uint16_t i = 0; i < pixels.numPixels(); ++i) {
setPixelWithBrightness(i, 255, 255, 255, currentBrightness);
}
}
void ModeHandler::effectFullWhitePosUpdate(){}
// Fix color
void ModeHandler::effectFixColorUpdate() {
uint32_t colorToSet = colorWheelWithBrightness(fixColor_CurrentColor,currentBrightness);
for (uint16_t i = 0; i < pixels.numPixels(); ++i) {
pixels.setPixelColor(i, colorToSet);
}
}
void ModeHandler::effectFixColorPosUpdate(long modePosChange) {
fixColor_CurrentColor = (fixColor_CurrentColor + modePosChange) % 256;
}
// Color cycle
void ModeHandler::effectColorCycleUpdate() {
if (millis() - colorCycle_LastChangeMillis >= colorCycle_ChangeSpeed) {
uint32_t colorToSet = colorWheelWithBrightness(colorCycle_CurrentColor,currentBrightness);
for (uint16_t i = 0; i < pixels.numPixels(); ++i) {
pixels.setPixelColor(i, colorToSet);
}
colorCycle_LastChangeMillis = millis();
colorCycle_CurrentColor++;
}
}
void ModeHandler::effectColorCyclePosUpdate(long modePosChange) {
colorCycle_ModePos += modePosChange;
colorCycle_ChangeSpeed = min(exp((colorCycle_ModePos) / 5.0), 25000.0);
}
// Rainbow
void ModeHandler::effectRainbowUpdate() {
if (millis() - rainbow_LastChangeMillis >= rainbow_ChangeSpeed) {
for (uint16_t i = 0; i < pixels.numPixels(); ++i) {
pixels.setPixelColor(i, colorWheelWithBrightness(rainbow_CurrentColor + (256 / pixels.numPixels()) * i,currentBrightness));
}
rainbow_LastChangeMillis = millis();
rainbow_CurrentColor++;
}
}
void ModeHandler::effectRainbowPosUpdate(long modePosChange) {
rainbow_ModePos += modePosChange;
rainbow_ChangeSpeed = min(exp((rainbow_ModePos) / 5.0), 25000.0);
}
// Breathe
void ModeHandler::effectBreatheUpdate() {
if (millis() - breathe_LastChangeMillis > 80) {
uint32_t colorToSet = colorWheelWithBrightness(breathe_CurrentColor,currentBrightness * ((sin(breathe_CurrentIntensity) + 1) / 2.0));
for (uint16_t i = 0; i < pixels.numPixels(); ++i) {
pixels.setPixelColor(i, colorToSet);
}
breathe_CurrentIntensity += breathe_CurrentWay;
if (breathe_CurrentIntensity >= TWO_PI) {
breathe_CurrentIntensity = 0;
}
breathe_LastChangeMillis = millis();
}
}
void ModeHandler::effectBreathePosUpdate(long modePosChange) {
breathe_CurrentColor = (breathe_CurrentColor + modePosChange) % 256;
}
// Fire
void ModeHandler::effectFireUpdate() {
if (millis() - fire_LastChangeMillis > fire_RandomDelay ) {
for (uint16_t i = 0; i < pixels.numPixels(); ++i) {
uint8_t flicker = random(10, 75);
uint8_t randomBrightness = random(55, 100);
setPixelWithBrightness(i, fire_RedColor - flicker, fire_GreenColor - flicker, 0, currentBrightness * (randomBrightness / 100.0));
}
fire_RandomDelay = random(50, 112);
fire_LastChangeMillis = millis();
}
}
void ModeHandler::effectFirePosUpdate() {}