-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathbooknook.ino
488 lines (409 loc) · 11.2 KB
/
booknook.ino
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
#include <FastLED.h>
#define NUM_LEDS 19
#define LED_PIN 2
#define STATIC_COLOR CHSV(30, 208, 127)
#define DYNAMIC_COLOR CHSV(30, 208, 127)
#define EXPLOSION_CHANCE 30 //1 in x chance every second
#define DYNAMIC_CHANCE 10 // 1 in x chance every second
/* Once a dynamic light has turned on or off, how long should we wait before
it can toggle again? The system will generate a random duration between these
limits. During that time, and randomly generated toggle events will be ignored */
#define MIN_DYNAMIC_STAY_ONOFF 5
#define MAX_DYNAMIC_STAY_ONOFF 8
// How long do the on/off transitions take, 1000 = 1 second
#define DYNAMIC_TOGGLE_DURATION_MILLISECONDS 1000
CRGB leds[NUM_LEDS];
enum LED_TYPE {
TYPE_STATIC,
TYPE_DYNAMIC,
TYPE_EXPLOSION,
TYPE_TORCH,
};
enum DISRUPTIVE_EVENT_STAGES {
EVENT_ALL_BLACK,
EVENT_STATIC_FLICKER,
EVENT_ALL_FLASH,
EVENT_RANDOM_HUE_EXPLOSION
};
class LedGroup
{
private:
LED_TYPE m_type;
uint32_t* m_indexes;
size_t m_indexCount;
unsigned long m_lastTick;
unsigned long m_disruptiveEventRunning;
public:
LED_TYPE getType() {
return m_type;
}
unsigned long getLastTick() {
return m_lastTick;
}
unsigned long setLastTick() {
m_lastTick = millis();
}
LedGroup* nextGroup;
LedGroup(uint32_t* t_indexes, size_t t_indexCount, LED_TYPE t_type) :
m_indexes(t_indexes),
m_indexCount(t_indexCount),
m_lastTick(millis()),
m_disruptiveEventRunning(false),
nextGroup(nullptr),
m_type(t_type) {}
virtual void tick() = 0;
virtual bool specialTick() = 0;
void setColor(CRGB color)
{
uint32_t* ptr = m_indexes;
for (int i = 0; i < m_indexCount; i++)
{
leds[*ptr].setRGB(color.r, color.g, color.b);
ptr++;
}
}
void setColor(CHSV color)
{
uint32_t* ptr = m_indexes;
for (int i = 0; i < m_indexCount; i++)
{
leds[*ptr].setHSV(color.hue, color.sat, color.val);
ptr++;
}
}
};
class DynamicLamp : public LedGroup
{
CHSV m_staticColor;
const float m_ToggleLightChancePerSecond = DYNAMIC_CHANCE;
bool m_lightsOn;
uint32_t dontChangeAgainUntil;
public:
DynamicLamp(uint32_t* t_indexes, size_t t_indexCount, CHSV t_color) : LedGroup(t_indexes, t_indexCount, TYPE_DYNAMIC), m_staticColor(t_color), m_lightsOn(true)
{
setColor(m_staticColor);
dontChangeAgainUntil = millis();
}
void tick()
{
if (millis() - getLastTick() > 1000)
{
setLastTick();
if (random(0, m_ToggleLightChancePerSecond) == 0)
{
toggle();
} else {
}
}
}
bool specialTick()
{
return false;
}
void toggle()
{
if (millis() >= dontChangeAgainUntil)
{
//Serial.println("Changed");
if (m_lightsOn)
{
turnOffSlowly();
}
else
{
turnOnSlowly(m_staticColor);
}
m_lightsOn = !m_lightsOn;
dontChangeAgainUntil = millis() + random(MIN_DYNAMIC_STAY_ONOFF * 1000, MIN_DYNAMIC_STAY_ONOFF * 1000);
}
else {
//Serial.println("Should have changed but didn't");
}
}
void turnOffSlowly()
{
CHSV color;
for (int i = m_staticColor.val; i >= 0; i--)
{
color = CHSV( m_staticColor.hue, m_staticColor.sat, i);
setColor(color);
FastLED.show();
delay(DYNAMIC_TOGGLE_DURATION_MILLISECONDS / m_staticColor.val);
}
}
void turnOnSlowly(CHSV targetColor)
{
CHSV color;
for (int i = 0; i <= targetColor.val; i++)
{
color = CHSV( targetColor.hue, targetColor.sat, i);
setColor(color);
FastLED.show();
delay(DYNAMIC_TOGGLE_DURATION_MILLISECONDS / targetColor.val);
}
}
};
class StaticLamp : public LedGroup
{
private:
CHSV m_staticColor;
public:
StaticLamp(uint32_t* t_indexes, size_t t_indexCount, CHSV t_color) : LedGroup(t_indexes, t_indexCount, TYPE_STATIC), m_staticColor(t_color)
{
}
bool specialTick()
{
return false;
}
void tick()
{
setColor(m_staticColor);
}
};
class ExplosionLamp : public LedGroup
{
private:
uint16_t explosionFadeDurationMs = 400;
uint16_t explosionDurationMs = 50;
bool running;
public:
ExplosionLamp(uint32_t* t_indexes, size_t t_indexCount) : LedGroup(t_indexes, t_indexCount, TYPE_EXPLOSION), running(false)
{
}
void tick()
{
}
// This is blocking and it sucks, but that's how it is now.
// Ideally this shouldn't use delays.
bool specialTick()
{
uint8_t hue = random(0, 255);
CHSV color;
for (int i = 0; i < 255; i += 6)
{
color = CHSV( hue, 255, i);
setColor(color);
if (random(0, 400) == 5)
specialTick();
FastLED.show();
delay(2);
}
delay(random(0, 1000));
for (int i = 255; i >= 0; i--)
{
color = CHSV( hue, 255, i);
if (random(0, 400) == 5)
specialTick();
setColor(color);
FastLED.show();
}
return false;
}
};
class House
{
private:
bool hasEvents;
LedGroup* groups;
DISRUPTIVE_EVENT_STAGES m_eventPos;
bool m_eventRunning;
unsigned long m_lastTick;
uint16_t m_eventDelay;
uint16_t m_eventCounter;
const uint16_t m_ToggleEventChancePerSecond = EXPLOSION_CHANCE;
LedGroup* getLastGroup()
{
LedGroup* current = groups;
while (current->nextGroup != nullptr)
{
current = current->nextGroup;
}
return current;
}
void insertGroup(LedGroup* t_group)
{
if (groups == nullptr)
{
groups = t_group;
}
else
{
LedGroup *last = getLastGroup();
if (last != nullptr)
{
last->nextGroup = t_group;
}
}
}
public:
House() : groups(nullptr), m_eventRunning(false), m_eventPos(EVENT_ALL_BLACK), m_eventDelay(0), m_eventCounter(0), hasEvents(false)
{
}
void createGroup(LED_TYPE t_type, uint32_t* indexes, size_t length)
{
LedGroup* newGroup = nullptr;
switch (t_type)
{
case TYPE_STATIC:
newGroup = new StaticLamp(indexes, length, STATIC_COLOR);
break;
case TYPE_DYNAMIC:
newGroup = new DynamicLamp(indexes, length, DYNAMIC_COLOR);
break;
case TYPE_EXPLOSION:
hasEvents = true;
newGroup = new ExplosionLamp(indexes, length);
break;
}
if (newGroup != nullptr)
{
insertGroup(newGroup);
}
}
void setAllColor(CHSV color)
{
LedGroup* current = groups;
while (current != nullptr)
{
current->setColor(color);
current = current->nextGroup;
}
}
bool normalTick()
{
LedGroup* current = groups;
while (current != nullptr)
{
current->tick();
current = current->nextGroup;
}
return true;
}
bool specialTick(LED_TYPE type)
{
LedGroup* current = groups;
while (current != nullptr)
{
if (current->getType() == type)
{
current->specialTick();
}
current = current->nextGroup;
}
}
void eventTick()
{
if (millis() - m_lastTick > m_eventDelay) {
m_lastTick = millis();
//Serial.println(m_eventPos);
switch (m_eventPos)
{
case EVENT_ALL_BLACK:
//Serial.println("all black");
m_eventDelay = 500;
setAllColor(CHSV(0, 0, 0));
m_eventPos = EVENT_STATIC_FLICKER;
break;
//This is blocking for now. Should ideally use timers, no delays.
case EVENT_STATIC_FLICKER:
//Serial.println("static flicker");
for (int i = 0; i < random(3, 6); i++)
{
setAllColor(STATIC_COLOR);
FastLED.show();
delay(random(0, 250));
setAllColor(CHSV(0, 0, 0));
FastLED.show();
delay(random(0, 450));
}
////Serial.println("static flicker");
setAllColor(CHSV(0, 0, 0));
delay(2000);
m_eventPos = EVENT_RANDOM_HUE_EXPLOSION;
break;
case EVENT_RANDOM_HUE_EXPLOSION:
//Serial.println("explosion");
m_lastTick = millis();
specialTick(TYPE_EXPLOSION);
resetEvent();
break;
}
}
}
void resetEvent()
{
m_eventRunning = false;
m_eventPos = EVENT_ALL_BLACK;
}
bool isEventRunning()
{
if (!hasEvents)
return false;
if (!m_eventRunning)
{
if (millis() - m_lastTick > 1000 )
{
m_lastTick = millis();
if (random(0, m_ToggleEventChancePerSecond) == 0) {
m_eventRunning = true;
}
}
}
return m_eventRunning;
}
void tick()
{
if (isEventRunning() == false)
{
normalTick();
}
else
{
eventTick();
}
}
};
#define NUM_HOUSES 4
House* houses[NUM_HOUSES];
uint32_t house1_floor[] = { 0, 2, 4, 6};
uint32_t house1_room1[] = {3};
uint32_t house1_explosion[] = {1, 5, 0, 6};
uint32_t house2_floor[] = {8, 9, 10, 11};
uint32_t house2_room1[] = {7};
uint32_t house2_room2[] = {12};
uint32_t house3_floor[] = {14, 15, 16};
uint32_t house3_room1[] = {17};
uint32_t house3_room2[] = {18};
uint32_t streetlight[] = {13};
void createGroup() {
}
void setup() {
randomSeed(analogRead(0));
Serial.begin(9600);
while (!Serial);
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
//Depending on your LEDs the above line needs to be different. Example for a WS2812B below.
//FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
houses[0] = new House();
houses[0]->createGroup(TYPE_STATIC, &house1_floor[0], (size_t)(sizeof(house1_floor) / sizeof(house1_floor[0])));
houses[0]->createGroup(TYPE_DYNAMIC, &house1_room1[0], (size_t)(sizeof(house1_room1) / sizeof(house1_room1[0])));
houses[0]->createGroup(TYPE_EXPLOSION, &house1_explosion[0], (size_t)(sizeof(house1_explosion) / sizeof(house1_explosion[0])));
houses[1] = new House();
houses[1]->createGroup(TYPE_STATIC, &house2_floor[0], (size_t)(sizeof(house2_floor) / sizeof(house2_floor[0])));
houses[1]->createGroup(TYPE_DYNAMIC, &house2_room1[0], (size_t)(sizeof(house2_room1) / sizeof(house2_room1[0])));
houses[1]->createGroup(TYPE_DYNAMIC, &house2_room2[0], (size_t)(sizeof(house2_room2) / sizeof(house2_room2[0])));
houses[2] = new House();
houses[2]->createGroup(TYPE_STATIC, &house3_floor[0], (size_t)(sizeof(house3_floor) / sizeof(house3_floor[0])));
houses[2]->createGroup(TYPE_DYNAMIC, &house3_room1[0], (size_t)(sizeof(house3_room1) / sizeof(house3_room1[0])));
houses[2]->createGroup(TYPE_DYNAMIC, &house3_room2[0], (size_t)(sizeof(house3_room2) / sizeof(house3_room2[0])));
houses[3] = new House();
houses[3]->createGroup(TYPE_STATIC, &streetlight[0], (size_t)(sizeof(streetlight) / sizeof(streetlight[0])));
}
void loop() {
for (int i = 0; i < NUM_HOUSES; i++)
{
houses[i]->tick();
}
FastLED.show();
delay(30);
}