-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightUnit.cpp
190 lines (164 loc) · 5.93 KB
/
lightUnit.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
#include "lightUnit.h"
#include "common.h"
#include <Arduino.h>
#include <map>
typedef std::map<int, LightUnit> LightUnits;
static LightUnits lightUnits;
static const LightUnit lightUnitNull = {0};
static const LightUnitState &lightUnitStateNull = lightUnitNull.state;
int /*LightUnitId*/ addLightUnit(const LightUnit &lightUnit)
{
static const int maxAttempts = 1024;
for (int currAttempt = 0; currAttempt < maxAttempts; ++currAttempt)
{
const LightUnitId id = (LightUnitId)random(minDynamicId + 1, 0x0fffffffUL);
if (lightUnits.find(id) == lightUnits.end())
{
setLightUnit(id, lightUnit, false /*rmBeforeAdd*/);
return id;
}
}
// if we made it here, we failed finding a good id.
// go away and play the lottery! ;)
gameOver("Fatal: unable to generate id for lightUnit");
return ~0;
}
void setLightUnit(int /*LightUnitId*/ id, const LightUnit &lightUnit,
bool rmBeforeAdd, bool quiet)
{
if (id == 0)
gameOver("Fatal: 0 is a reserved LightUnitId");
LightUnit newLightUnit;
const bool exists = lightUnitExists(id, &newLightUnit);
// When not replacing, run a "pretend" expiration to clear state, since that gets reset
if (rmBeforeAdd)
rmLightUnit(id);
else if (exists)
lightUnitFinalIteration(&newLightUnit);
newLightUnit = lightUnit;
newLightUnit.id = id;
if (newLightUnit.animation.rainbowColor ||
newLightUnit.animation.randomColor ||
newLightUnit.animation.sameRandomColor)
newLightUnit.animation.pulse = false;
if (newLightUnit.animation.frames == 0)
newLightUnit.animation.frames = 1;
if (newLightUnit.animation.speed == 0 || newLightUnit.animation.pulse)
{
newLightUnit.animation.speed = 1;
}
newLightUnit.state = lightUnitStateNull;
lightUnits[id] = newLightUnit;
if (!quiet || !exists)
{
#ifdef DEBUG
Serial.printf("%s LightUnit %d. There are now %zu entries.\n",
(exists ? (rmBeforeAdd ? "Replaced" : "Setting") : "Adding"),
id, lightUnits.size());
#endif
}
}
void rmLightUnit(int /*LightUnitId*/ id)
{
LightUnits::iterator iter(lightUnits.find(id));
if (iter == lightUnits.end())
return; // noop
// First: get a local copy and remove it from lightUnits
LightUnit lightUnit((*iter).second);
lightUnits.erase(iter);
lightUnitFinalIteration(&lightUnit);
if (lightUnit.doneCallback)
lightUnit.doneCallback(lightUnit);
#ifdef DEBUG
Serial.printf("Removed LightUnit %d. There are now %zu entries.\n",
id, lightUnits.size());
#endif
// Handle cases when this lightUnit depends on another via recursion
rmLightUnit(lightUnit.animation.dependsOn);
}
void rmLightUnits()
{
if (lightUnits.empty())
return; // noop
#ifdef DEBUG
Serial.printf("Removing all %zu LightUnits\n", lightUnits.size());
#endif
while (!lightUnits.empty())
{
auto iter(lightUnits.rbegin());
rmLightUnit((*iter).first);
}
// lightUnits.clear(); // cannot use it bc we want to check done callback
}
bool lightUnitExists(int /*LightUnitId*/ id, LightUnit *lightUnitPtr)
{
LightUnits::const_iterator iter(lightUnits.find((LightUnitId)id));
const bool result = iter != lightUnits.end();
if (lightUnitPtr)
*lightUnitPtr = result ? (*iter).second : lightUnitNull;
return result;
}
void * /*LightUnit**/ getFirstLightUnit()
{
// Note: iterate backwards to give priority to ids explicitly used
auto iter(lightUnits.rbegin());
return (iter == lightUnits.rend()) ? nullptr : &(*iter).second;
}
void * /*LightUnit**/ getNextLightUnit(LightUnitId id)
{
// Note: iterate backwards to give priority to ids explicitly used
// https://stackoverflow.com/questions/23011463/how-to-find-the-first-value-less-than-the-search-key-with-stl-set
auto iter(lightUnits.lower_bound(id));
if (iter == lightUnits.begin() || iter == lightUnits.end())
return nullptr;
iter--;
return &(*iter).second;
}
uint32_t lightUnitsSize() { return (uint32_t)lightUnits.size(); }
bool equivalentLightUnits(const LightUnit &left, const LightUnit &right)
{
if (left.id != right.id)
return false;
if (left.pixelMask != right.pixelMask)
return false;
if (left.color != right.color)
return false;
if (left.brightness != right.brightness)
return false;
if (memcmp(&left.animation, &right.animation, sizeof(left.animation)))
return false;
if (left.iterateCallback != right.iterateCallback)
return false;
if (left.doneCallback != right.doneCallback)
return false;
// LightUnitState state;
return true;
}
void dumpLightUnit(const LightUnit &lightUnit, const char *msg)
{
#ifdef DEBUG
char buff[40];
Serial.printf("%s LightUnit %d\n", msg, (int)lightUnit.id);
snprintf(buff, sizeof(buff), "0x%016llx", lightUnit.pixelMask);
Serial.printf(" pixelMask: %s\n", buff);
Serial.printf(" color: %d\n", lightUnit.color);
Serial.printf(" brightness: %d\n", (int)lightUnit.brightness);
Serial.printf(" iterateCallback: %p\n", lightUnit.iterateCallback);
Serial.printf(" doneCallback: %p\n", lightUnit.doneCallback);
Serial.printf(" anim.frames: %d\n", lightUnit.animation.frames);
Serial.printf(" anim.step: %d\n", lightUnit.animation.step);
Serial.printf(" anim.speed: %d\n", lightUnit.animation.speed);
snprintf(buff, sizeof(buff), "0x%016llx", lightUnit.animation.expiration);
Serial.printf(" anim.expiration: %s\n", buff);
Serial.printf(" anim.dependsOn: %d\n", (int)lightUnit.animation.dependsOn);
Serial.printf(" anim.flags: randomPixels %d sameRandomColor %d randomColor %d rainbowColor %d keepPixelWhenDone %d blink %d pulse %d \n",
(int)lightUnit.animation.randomPixels,
(int)lightUnit.animation.sameRandomColor,
(int)lightUnit.animation.randomColor,
(int)lightUnit.animation.rainbowColor,
(int)lightUnit.animation.keepPixelWhenDone,
(int)lightUnit.animation.blink,
(int)lightUnit.animation.pulse);
// LightUnitState state;
#endif // ifdef DEBUG
}