forked from brimshot/quickPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqpPattern.cpp
300 lines (176 loc) · 6.27 KB
/
qpPattern.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
#include <qpPattern.h>
#include <qpColor.h>
qpPattern::qpPattern() {
this->activateIfConditionsMet = (&qpPattern::doNothing);
this->deactivateIfConditionsMet = (&qpPattern::doNothing);
this->lastReferencedColor = this->colors.append(new qpColor(this));
}
// ~ Render
bool qpPattern::render() {
//bool qpPattern::render(CRGB *targetLeds, int numLeds) {
(this->*activateIfConditionsMet)();
if(this->isActive) {
if(this->ticks == this->nextRenderTick) {
this->updates++;
this->nextRenderTick += this->ticksBetweenFrames;
this->draw();
}
//update colors after rendering frame so not to skip initial color
while(qpColor *currentColor = this->colors.fetch())
(currentColor->*(currentColor->updateColorFunction))();
this->ticks++;
(this->*deactivateIfConditionsMet)();
return true; //something was rendered
}
return false; //nothing rendered
}
void qpPattern::activatePeriodically() {
if(this->isActive)
return;
if(this->ticksUntilActive > 0) {
this->ticksUntilActive--;
return;
}
this->activate();
this->resetActivationTimer();
}
bool qpPattern::activate() {
// If we are only activating with a chance, check that here
if(this->chanceToActivatePattern > 0) {
if(random16(100) > this->chanceToActivatePattern) {
return false;
}
}
// If we are staying active for a random period count, set it here
if(this->maxPeriodsToStayActive)
this->currentPeriodsToStayActive = random16(this->minPeriodsToStayActive, this->maxPeriodsToStayActive);
this->periodCountAtLastActivation = *this->activePeriodsCounter;
this->activations++;
this->isActive = true;
return true;
}
void qpPattern::resetActivationTimer() {
// If we are activating at a random interval, calculate the next interval
if(this->maxTicksBetweenActivations)
this->ticksUntilActive = random16(this->minTicksBetweenActivations, this->maxTicksBetweenActivations);
else
this->ticksUntilActive = this->minTicksBetweenActivations;
}
void qpPattern::deactivateIfActivePeriodComplete() {
if((*this->activePeriodsCounter - this->periodCountAtLastActivation) >= this->currentPeriodsToStayActive)
this->deactivate();
}
bool qpPattern::shouldRemoveWhenDecativated() {
return this->removeOnDeactivation;
}
//direct external control
void qpPattern::deactivate() {
this->isActive = false;
}
// ~ Animation
// Pattern speed
qpPattern &qpPattern::drawEveryNTicks(int ticks) {
this->ticksBetweenFrames = ticks;
return *this;
}
// Color to draw
CRGB qpPattern::_getColor(byte index) {
return this->colors.getItem(index)->getColor();
}
CRGBPalette16 qpPattern::_getPalette(byte index) {
return this->colors.getItem(index)->getPalette();
}
// ~ Periodic activation config
qpPattern &qpPattern::removeWhenDeactivated(bool value) {
this->removeOnDeactivation = value;
return *this;
}
qpPattern &qpPattern::activatePeriodicallyEveryNTicks(int minTicks, int maxTicks) {
this->isActive = false;
this->minTicksBetweenActivations = minTicks;
this->maxTicksBetweenActivations = maxTicks;
this->activateIfConditionsMet = (&qpPattern::activatePeriodically);
this->resetActivationTimer();
return *this;
}
// Active period duration
qpPattern &qpPattern::stayActiveForNTicks(int minTicks, int maxTicks) {
this->activePeriodsCounter = &this->ticks;
this->setActivePeriod(minTicks, maxTicks);
return *this;
}
qpPattern &qpPattern::stayActiveForNFrames(int minUpdates, int maxUpdates) {
this->activePeriodsCounter = &this->updates;
this->setActivePeriod(minUpdates, maxUpdates);
return *this;
}
qpPattern &qpPattern::stayActiveForNCycles(int minCycles, int maxCycles) {
this->activePeriodsCounter = &this->cycles;
this->setActivePeriod(minCycles, maxCycles);
return *this;
}
qpPattern &qpPattern::withChanceOfActivation(byte percentage) {
this->chanceToActivatePattern = constrain(percentage, 0, 100);
return *this;
}
void qpPattern::setActivePeriod(int minPeriods, int maxPeriods) {
this->currentPeriodsToStayActive = this->minPeriodsToStayActive = max(1, minPeriods);
this->maxPeriodsToStayActive = max(0, maxPeriods);
this->deactivateIfConditionsMet = (&qpPattern::deactivateIfActivePeriodComplete);
}
// ~ Color config
//sets lastreferenced ptr so fluent chain can continue
qpPattern &qpPattern::color(byte index) {
if(index > (this->colors.numElements - 1))
this->lastReferencedColor = this->colors.append(new qpColor(this));
else
this->lastReferencedColor = this->colors.getItem(index);
return *this;
}
qpPattern &qpPattern::singleColor(CRGB color) {
this->sameColor().singleColor(color);
return *this;
}
qpPattern &qpPattern::usePalette(CRGBPalette16 colorPalette) {
this->sameColor().usePalette(colorPalette);
return *this;
}
qpPattern &qpPattern::useColorSet(CRGB *colorSet, byte numColorsInSet){
this->sameColor().useColorSet(colorSet, numColorsInSet);
return *this;
}
// Color timing
qpPattern &qpPattern::changeColorEveryNTicks(int minTicks, int maxTicks) {
this->sameColor().changeColorEveryNTicks(minTicks, maxTicks);
return *this;
}
qpPattern &qpPattern::changeColorEveryNCycles(int minCycles, int maxCycles) {
this->sameColor().changeColorEveryNCycles(minCycles, maxCycles);
return *this;
}
qpPattern &qpPattern::changeColorEveryNFrames(int minFrames, int maxFrames) {
this->sameColor().changeColorEveryNFrames(minFrames, maxFrames);
return *this;
}
qpPattern &qpPattern::changeColorEveryNActivations(int minActivations, int maxActivations) {
this->sameColor().changeColorEveryNActivations(minActivations, maxActivations);
return *this;
}
qpPattern &qpPattern::withChanceToChangeColor(byte percentage) {
this->sameColor().withChanceToChangeColor(percentage);
return *this;
}
// Setup - called by Layer
void qpPattern::assignTargetLeds(CRGB *leds, int numLeds) {
this->_targetLeds = leds;
this->_numLeds = numLeds;
}
// ~ Palette config
qpPattern &qpPattern::chooseColorFromPalette(CRGBPalette16 colorPalette, QP_COLOR_MODE mode) {
this->sameColor().chooseColorFromPalette(colorPalette, mode);
return *this;
}
qpPattern &qpPattern::chooseColorFromSet(CRGB *colorSet, byte numElements, QP_COLOR_MODE mode) {
this->sameColor().chooseColorFromSet(colorSet, numElements, mode);
return *this;
}