forked from brimshot/quickPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qpLayer.cpp
153 lines (109 loc) · 3.76 KB
/
qpLayer.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
#include <qpLayer.h>
qpLayer::qpLayer(CRGB *leds, int numLeds) {
this->leds = leds;
this->numLeds = numLeds;
this->setLayerBrush(OVERLAY);
}
void qpLayer::draw(CRGB *targetLeds, int numLeds) {
bool patternsRendered = false;
if(this->continualFadeAmount) //conceivably we prevent a loop applying 0 to each led with this check
fadeToBlackBy(this->leds, this->numLeds, this->continualFadeAmount);
while(qpPattern *currentPattern = this->patterns.fetch()) {
bool isActive = currentPattern->render();
patternsRendered |= isActive;
// If this pattern isn't active and it's configured to auto delete when finished,
// then destroy and remove from the patterns linked list.
if(!isActive && currentPattern->shouldRemoveWhenDecativated()) {
// If this is considered the lastReferencedPattern, updated it.
if(currentPattern == this->lastReferencedPattern) {
this->lastReferencedPattern = nullptr;
}
this->patterns.remove(currentPattern);
}
}
if(patternsRendered || this->bPersistWhenPatternsInactive)
(this->*applyLeds)(targetLeds, this->leds, numLeds);
}
// Config
qpPattern &qpLayer::addPattern(qpPattern *pattern) {
pattern->assignTargetLeds(this->leds, this->numLeds);
pattern->initialize();
this->lastReferencedPattern = this->patterns.append(pattern);
return *pattern;
}
qpLayer &qpLayer::continuallyFadeLayerBy(int fadeAmount) {
this->continualFadeAmount = constrain(fadeAmount, 0, 255);
return *this;
}
qpLayer &qpLayer::hideWhenNoActivePatterns(bool trueOrFalse) {
this->bPersistWhenPatternsInactive = (! trueOrFalse);
return *this;
}
// Brush config
//preset
qpLayer &qpLayer::setLayerBrush(QP_BRUSH_TYPE brushType) {
switch(brushType) {
case ADD:
this->applyLeds = &qpLayer::addToLeds;
break;
case SUBTRACT:
this->applyLeds = &qpLayer::subtractFromLeds;
break;
case OVERWRITE:
this->applyLeds = &qpLayer::overwriteLeds;
break;
case OVERLAY:
this->applyLeds = &qpLayer::overlayOnLeds;
break;
case COMBINE:
this->applyLeds = &qpLayer::combineWithLeds;
break;
case MASK:
this->applyLeds = &qpLayer::maskLeds;
break;
}
return *this;
}
//external brush assignment - TODO: would all brushes moved to external functions to implement
/*
qpLayer &qpLayer::setLayerBrush(void (*brushFunc)(CRGB *toLeds, CRGB *sourceLeds, int numLeds)) {
this->applyLeds = brushFunc;
return *this;
}
*/
// Brushes
void qpLayer::addToLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++)
targetLeds[i] += sourceLeds[i];
}
void qpLayer::subtractFromLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++)
targetLeds[i] -= sourceLeds[i];
}
void qpLayer::overlayOnLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++) {
if(sourceLeds[i] != CRGB(0, 0, 0))
targetLeds[i] = sourceLeds[i];
}
}
void qpLayer::overwriteLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
memcpy(targetLeds, sourceLeds, (sizeof(CRGB)*numLeds));
}
void qpLayer::combineWithLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++)
targetLeds[i] |= this->leds[i];
// targetLeds[i] = blend(targetLeds[i], sourceLeds[i], 128); //TODO: implement true rgb blend
}
void qpLayer::maskLeds(CRGB *targetLeds, CRGB *sourceLeds, int numLeds) {
for(int i = 0; i < numLeds; i++) {
targetLeds[i] -= -sourceLeds[i];
}
}
// Access
qpPattern &qpLayer::pattern(byte patternIndex) {
this->lastReferencedPattern = this->patterns.getItem(patternIndex);
return *this->lastReferencedPattern;
}
qpPattern &qpLayer::operator()(byte patternIndex) {
return this->pattern(patternIndex);
}