forked from brimshot/quickPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqpPattern.h
155 lines (96 loc) · 4.04 KB
/
qpPattern.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
#ifndef QP_PATTERN_H
#define QP_PATTERN_H
#include <FastLED.h>
#include <qpLinkedList.h>
#include <qpColor.h>
#define DIR_FORWARD 1
#define DIR_REVERSE -1
class qpColor;
class qpLayer;
class qpPattern {
friend class qpColor;
private:
qpLayer *parentLayer;
bool isActive = true;
// Period counters
int ticks = 0;
int updates = 0;
int cycles = 0;
int activations = 0;
int nextRenderTick = 0;
// ~ Colors
qpLinkedList <qpColor> colors;
qpColor *lastReferencedColor;
// Animation
int ticksBetweenFrames = 1;
// ~ Periodic activation
bool removeOnDeactivation = false;
unsigned int minTicksBetweenActivations = 0;
unsigned int maxTicksBetweenActivations = 0;
unsigned int ticksUntilActive = 0;
int *activePeriodsCounter = NULL;
unsigned int periodCountAtLastActivation = 0;
unsigned int minPeriodsToStayActive = 1;
unsigned int maxPeriodsToStayActive = 0;
unsigned int currentPeriodsToStayActive = 0;
byte chanceToActivatePattern = 0;
void setActivePeriod(int minPeriods, int maxPeriods);
void (qpPattern::*activateIfConditionsMet)(); //periodically or nothing
void activatePeriodically();
void resetActivationTimer();
void (qpPattern::*deactivateIfConditionsMet)(); //active period check or nothing
void deactivateIfActivePeriodComplete();
void doNothing() { /* empty function for pointers to pattern update steps that do nothing as per pattern config */ }
protected:
// Members and methods used in pattern animation code
CRGB *_targetLeds;
int _numLeds = 0;
virtual void draw() = 0; //called at each update interval, must be implemented by child classes
// ~ Color
CRGB _getColor(byte index = 0);
CRGBPalette16 _getPalette(byte index = 0);
// ~ Animation util
inline bool _inBounds(int pos) { return ((pos >= 0) && (pos < _numLeds)); }
inline void _countCycle() { this->cycles++; }
inline void _clearLeds() { fill_solid(_targetLeds, _numLeds, CRGB::Black); }
public:
qpPattern();
// ~ Setup
void assignTargetLeds(CRGB *leds, int numLeds); // Called when pattern is added to layer
/*-->!! Note:
* _numLeds and _targetLeds are undefined (empty pointers) when the pattern constructors are called.
* Any pre-rendering calculations that require the number of LEDs to be known should be put in the initialize() function
*/
virtual void initialize() { } // called once when pattern is created, after LEDs are assigned
// ~ Render
// Public render hook
bool render();
// Pattern speed
qpPattern &drawEveryNTicks(int ticks);
// ~ Periodic activation
qpPattern &removeWhenDeactivated(bool value);
qpPattern &activatePeriodicallyEveryNTicks(int minTicks, int maxTicks = 0);
qpPattern &stayActiveForNTicks(int minTicks, int maxTicks = 0);
qpPattern &stayActiveForNFrames(int minUpdates, int maxUpdates = 0);
qpPattern &stayActiveForNCycles(int minCycles, int maxCycles = 0);
qpPattern &withChanceOfActivation(byte percentage);
// ~ Colors
qpPattern &color(byte index); //sets last referenced color ptr for continuing fluent config calls
qpColor &sameColor() { return *this->lastReferencedColor; }
qpPattern &singleColor(CRGB color);
qpPattern &usePalette(CRGBPalette16 colorPalette);
qpPattern &chooseColorFromPalette(CRGBPalette16 colorPalette, QP_COLOR_MODE mode);
qpPattern &useColorSet(CRGB *colorSet, byte numColorsInSet);
qpPattern &chooseColorFromSet(CRGB *colorSet, byte numElements, QP_COLOR_MODE mode);
// Timing
qpPattern &changeColorEveryNTicks(int minTicks, int maxTicks = 0);
qpPattern &changeColorEveryNCycles(int minCycles, int maxCycles = 0);
qpPattern &changeColorEveryNFrames(int minFrames, int maxFrames = 0);
qpPattern &changeColorEveryNActivations(int minActivations, int maxActivations = 0);
qpPattern &withChanceToChangeColor(byte percentage);
// ~ Status control
bool activate();
void deactivate();
bool shouldRemoveWhenDecativated();
};
#endif