-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathledCtrl.h
487 lines (386 loc) · 13.1 KB
/
ledCtrl.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
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
// This file defines the ledCtrl class
#include "globalInclude.h"
#include <NeoPixelBus.h>
#include <NeoPixelAnimator.h>
#include <Preferences.h>
extern Preferences prefs;
extern SemaphoreHandle_t ledMutex;
const uint8_t PixelPin = 13; // make sure to set this to the correct pin, ignored for Esp8266
// A small number
const float epi = 0.00001;
// For Demo use
#ifdef DEMO_MODE
const uint16_t PixelCount = 10; // make sure to set this to the number of pixels in your strip
#define READ_LIGHT_START_INDEX 0
#define READ_LIGHT_STOP_INDEX 4
#define ROOM_LIGHT_START_INDEX 5
#define ROOM_LIGHT_STOP_INDEX 9
#else
const uint16_t PixelCount = 156; // make sure to set this to the number of pixels in your strip
#define READ_LIGHT_START_INDEX 0
#define READ_LIGHT_STOP_INDEX 65
#define ROOM_LIGHT_START_INDEX 66
#define ROOM_LIGHT_STOP_INDEX 155
#endif
NeoPixelBus<NeoGrbwFeature, NeoSk6812Method> strip(PixelCount, PixelPin);
class ledCtrl {
private:
bool roomLightState = false;
bool sunriseLightState = false;
bool readLightState = false;
bool nightLightState = false;
char varName[7];
bool flashState = false;
// what the user set it to
HsbColor readColor;
HsbColor roomColor;
HsbColor nightColor;
// What the last value sent to the strip was
HsbColor lastReadColor;
HsbColor lastRoomColor;
HsbColor lastNightColor;
// What the rest of the program said it should be now
HsbColor activeReadColor;
HsbColor activeRoomColor;
HsbColor blackColor;
NeoGamma<NeoGammaTableMethod> colorGamma;
public:
void ledInit ( void ) {
// read the saved values from EEPOM
strcpy(varName, "RoomH");
float roomH = prefs.getFloat(varName, 0.1);
strcpy(varName, "RoomS");
float roomS = prefs.getFloat(varName, 0.5);
strcpy(varName, "RoomB");
float roomB = prefs.getFloat(varName, 1.0);
strcpy(varName, "ReadH");
float readH = prefs.getFloat(varName, 0.0);
strcpy(varName, "ReadS");
float readS = prefs.getFloat(varName, 0.0);
strcpy(varName, "ReadB");
float readB = prefs.getFloat(varName, 0.5);
strcpy(varName, "NightH");
float nightH = prefs.getFloat(varName, 0.1);
strcpy(varName, "NightS");
float nightS = prefs.getFloat(varName, 0.5);
strcpy(varName, "NightB");
float nightB = prefs.getFloat(varName, 1.0);
roomColor = HsbColor(roomH, roomS, roomB);
readColor = HsbColor(readH, readS, readB);
nightColor = HsbColor(nightH, nightS, nightB);
lastReadColor = HsbColor(0, 0, 0);
lastRoomColor = HsbColor(0, 0, 0);
lastNightColor = HsbColor(0, 0, 0);
blackColor = HsbColor(0, 0, 0);
activeReadColor = blackColor;
activeRoomColor = blackColor;
}
void Show( void ) {
strip.Show();
}
void Dirty ( void ) {
strip.Dirty();
}
bool IsDirty( void ) {
return strip.IsDirty();
}
uint16_t getPixelCount ( void ) {
return PixelCount;
}
bool CanShow () {
return strip.CanShow();
}
void startFlashing( void ) {
flashState = true;
}
void stopFlashing( void ) {
flashState = false;
}
void toggleFlashing( void ) {
if (flashState == true ) flashState = false;
else flashState = true;
}
void saveLedData() {
float tmpVar;
if (xSemaphoreTake(ledMutex, (TickType_t) 100) == pdTRUE ) {
tmpVar = prefs.getFloat("RoomH", -1.0);
if (tmpVar != roomColor.H) prefs.putFloat("RoomH", roomColor.H);
tmpVar = prefs.getFloat("RoomS", -1.0);
if (tmpVar != roomColor.S) prefs.putFloat("RoomS", roomColor.S);
tmpVar = prefs.getFloat("RoomB", -1.0);
if (tmpVar != roomColor.B) prefs.putFloat("RoomB", roomColor.B);
tmpVar = prefs.getFloat("ReadH", -1.0);
if (tmpVar != readColor.H) prefs.putFloat("ReadH", readColor.H);
tmpVar = prefs.getFloat("ReadS", -1.0);
if (tmpVar != readColor.S) prefs.putFloat("ReadS", readColor.S);
tmpVar = prefs.getFloat("ReadB", -1.0);
if (tmpVar != readColor.B) prefs.putFloat("ReadB", readColor.B);
tmpVar = prefs.getFloat("NightH", -1.0);
if (tmpVar != nightColor.H) prefs.putFloat("NightH", nightColor.H);
tmpVar = prefs.getFloat("NightS", -1.0);
if (tmpVar != nightColor.S) prefs.putFloat("NightS", nightColor.S);
tmpVar = prefs.getFloat("NightB", -1.0);
if (tmpVar != nightColor.B) prefs.putFloat("NightB", nightColor.B);
xSemaphoreGive(ledMutex);
}
else {
Serial.println("ledCtrl.saveLedData: Unable to get the ledMutex. Data NOT WRITTEN TO FLASH!");
}
}
// ================================
// ===== Sunrise Light Code =======
// ================================
void sunriseLightOn() {
sunriseLightState = true;
}
void sunriseLightOff() {
sunriseLightState = false;
}
// ================================
// ===== Room Light Code ==========
// ================================
void roomLightOn() {
nightLightState = false;
sunriseLightState = false;
roomLightState = true;
activeRoomColor = roomColor;
}
void roomLightOff() {
roomLightState = false;
activeRoomColor = blackColor;
}
void roomLightToggle() {
if (roomLightState == false) {
nightLightState = false;
sunriseLightState = false;
roomLightState = true;
activeRoomColor = roomColor;
}
else {
roomLightState = false;
activeRoomColor = blackColor;
}
}
bool getRoomLightState() {
return roomLightState;
}
void setRoomLightColorHSB (float h, float s, float b) {
if (h >= (1.0 - epi)) h -= 1.0;
else if (h < (0.0 + epi) ) h += 1.0;
if (s > 1.0) s = 1.0;
else if (s < 0.0) s = 0.0;
if (b > 1.0) b = 1.0;
else if (b < 0.1) b = 0.1;
// round to nearest 0.5
h = round(h * 20) / 20;
s = round(s * 20) / 20;
b = round(b * 20) / 20;
if (h == 1.0) {
h = 0.0;
}
roomColor = HsbColor(h, s, b);
if (roomLightState) lightRoomLight(roomColor);
}
void setActiveRoomLightColorHSB(float h, float s, float b) {
if (h >= (1.0 - epi)) h -= 1.0;
else if (h < (0.0 + epi) ) h += 1.0;
if (s > 1.0) s = 1.0;
else if (s < 0.0) s = 0.0;
if (b > 1.0) b = 1.0;
else if (b < 0.0) b = 0.0;
// round to nearest 0.5
h = round(h * 20) / 20;
s = round(s * 20) / 20;
b = round(b * 20) / 20;
if (h == 1.0) {
h = 0.0;
}
activeRoomColor = HsbColor(h, s, b);
if (roomLightState) {
lightRoomLight(activeRoomColor);
}
}
void setActiveSunriseRoomLightColorHSB(float h, float s, float b) {
activeRoomColor = HsbColor(h, s, b);
if (sunriseLightState) {
lightRoomLight(activeRoomColor);
}
}
HsbColor getRoomLightColorHSB ( void ) {
return roomColor;
}
// ================================
// === Reading Light Code =========
// ================================
void readLightOn() {
readLightState = true;
activeReadColor = readColor;
}
void readLightOff() {
readLightState = false;
activeReadColor = blackColor;
}
void readLightToggle() {
if (readLightState == false) {
readLightState = true;
activeReadColor = readColor;
}
else {
readLightState = false;
activeReadColor = blackColor;
}
}
bool getReadLightState() {
return readLightState;
}
void setReadLightColorHSB (float h, float s, float b) {
if (h >= (1.0 - epi)) h -= 1.0;
else if (h < (0.0 + epi) ) h += 1.0;
if (s > 1.0) s = 1.0;
else if (s < 0.0) s = 0.0;
if (b > 1.0) b = 1.0;
else if (b < 0.1) b = 0.1;
// round to nearest 0.5
h = round(h * 20) / 20;
s = round(s * 20) / 20;
b = round(b * 20) / 20;
if (h == 1.0) {
h = 0.0;
}
readColor = HsbColor(h, s, b);
}
void setActiveReadLightColorHSB(float h, float s, float b) {
if (h >= (1.0 - epi)) h -= 1.0;
else if (h < (0.0 + epi) ) h += 1.0;
if (s > 1.0) s = 1.0;
else if (s < 0.0) s = 0.0;
if (b > 1.0) b = 1.0;
else if (b < 0.0) b = 0.0;
// round to nearest 0.5
h = round(h * 20) / 20;
s = round(s * 20) / 20;
b = round(b * 20) / 20;
if (h == 1.0) {
h = 0.0;
}
activeReadColor = HsbColor(h, s, b);
}
HsbColor getReadLightColorHSB ( void ) {
return readColor;
}
// ================================
// ===== Night Light Code =========
// ================================
void nightLightOn() {
nightLightState = true;
roomLightState = false;
activeRoomColor = nightColor;
}
void nightLightOff() {
nightLightState = false;
activeRoomColor = blackColor;
}
void nightLightToggle() {
if (nightLightState == false) {
nightLightState = true;
roomLightState = false;
activeRoomColor = nightColor;
}
else {
nightLightState = false;
activeRoomColor = blackColor;
}
}
bool getNightLightState() {
return nightLightState;
}
void setNightLightColorHSB (float h, float s, float b) {
if (h >= (1.0 - epi)) h -= 1.0;
else if (h < (0.0 + epi) ) h += 1.0;
if (s > 1.0) s = 1.0;
else if (s < 0.0) s = 0.0;
if (b > 1.0) b = 1.0;
else if (b < 0.1) b = 0.1;
// round to nearest 0.5
h = round(h * 20) / 20;
s = round(s * 20) / 20;
b = round(b * 20) / 20;
if (h == 1.0) {
h = 0.0;
}
nightColor = HsbColor(h, s, b);
if (nightLightState) lightRoomLight(nightColor);
}
void setActiveNightLightColorHSB(float h, float s, float b) {
if (h >= (1.0 - epi)) h -= 1.0;
else if (h < (0.0 + epi) ) h += 1.0;
if (s > 1.0) s = 1.0;
else if (s < 0.0) s = 0.0;
if (b > 1.0) b = 1.0;
else if (b < 0.0) b = 0.0;
// round to nearest 0.5
h = round(h * 20) / 20;
s = round(s * 20) / 20;
b = round(b * 20) / 20;
if (h == 1.0) {
h = 0.0;
}
activeRoomColor = HsbColor(h, s, b);
if (nightLightState) lightRoomLight(activeRoomColor);
}
HsbColor getNightLightColorHSB ( void ) {
return nightColor;
}
// ================================
// ===== Strip Setting Code =======
// ================================
void updateStrip ( void ) { // call this regularly!
static bool flashOn = false;
const uint16_t flashTriggerTime = 250;
static unsigned long lastFlashTime = 0;
if (flashState && readLightState == false) { // if we are supposed to be flasheing the ReadLight and the readLight is not supposed to be constantly on
unsigned long timeNow = millis();
if (timeNow - lastFlashTime > flashTriggerTime) {
if (flashOn) {
lastReadColor = HsbColor(0, 0, 0.0f);
flashOn = false;
lastFlashTime = timeNow;
}
else {
lastReadColor = HsbColor(0, 0, 1.0f);
flashOn = true;
lastFlashTime = timeNow;
}
lightReadLight(lastReadColor);
}
}
else lightReadLight(activeReadColor);
lightRoomLight (activeRoomColor);
}
void lightReadLight (HsbColor inColor) {
static RgbwColor lastGammaColor = RgbwColor(0, 0, 0, 0);
HsbColor filtColor = HsbColor::LinearBlend<NeoHueBlendShortestDistance>(lastReadColor, inColor, 0.45f);
lastReadColor = filtColor;
RgbwColor gammaColor = colorGamma.Correct(RgbwColor(filtColor));
if (gammaColor != lastGammaColor) {
if (xSemaphoreTake(ledMutex, (TickType_t) 10) == pdTRUE ) {
strip.ClearTo(gammaColor, READ_LIGHT_START_INDEX, READ_LIGHT_STOP_INDEX);
xSemaphoreGive(ledMutex);
lastGammaColor = gammaColor;
}
}
}
void lightRoomLight (HsbColor inColor) {
static RgbwColor lastGammaColor = RgbwColor(0, 0, 0, 0);
HsbColor filtColor = HsbColor::LinearBlend<NeoHueBlendShortestDistance>(lastRoomColor, inColor, 0.45f);
lastRoomColor = filtColor;
RgbwColor gammaColor = colorGamma.Correct(RgbwColor(filtColor));
if (gammaColor != lastGammaColor) {
if (xSemaphoreTake(ledMutex, (TickType_t) 10) == pdTRUE ) {
strip.ClearTo(gammaColor, ROOM_LIGHT_START_INDEX, ROOM_LIGHT_STOP_INDEX);
xSemaphoreGive(ledMutex);
lastGammaColor = gammaColor;
}
}
}
};