Skip to content

Commit d6809c8

Browse files
authored
Merge pull request #16 from pauldotknopf/feature/delete-qplayer
Auto delete qpPattern when deactivated.
2 parents f1083b1 + 90f3bd5 commit d6809c8

6 files changed

Lines changed: 80 additions & 5 deletions

File tree

qpLayer.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@ void qpLayer::draw(CRGB *targetLeds, int numLeds) {
1616
if(this->continualFadeAmount) //conceivably we prevent a loop applying 0 to each led with this check
1717
fadeToBlackBy(this->leds, this->numLeds, this->continualFadeAmount);
1818

19-
while(qpPattern *currentPattern = this->patterns.fetch())
20-
patternsRendered |= currentPattern->render();
19+
while(qpPattern *currentPattern = this->patterns.fetch()) {
20+
bool isActive = currentPattern->render();
21+
patternsRendered |= isActive;
22+
23+
// If this pattern isn't active and it's configured to auto delete when finished,
24+
// then destroy and remove from the patterns linked list.
25+
if(!isActive && currentPattern->shouldRemoveWhenDecativated()) {
26+
// If this is considered the lastReferencedPattern, updated it.
27+
if(currentPattern == this->lastReferencedPattern) {
28+
this->lastReferencedPattern = nullptr;
29+
}
30+
this->patterns.remove(currentPattern);
31+
}
32+
}
2133

2234
if(patternsRendered || this->bPersistWhenPatternsInactive)
2335
(this->*applyLeds)(targetLeds, this->leds, numLeds);

qpLinkedList.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ class qpLinkedList {
2323

2424
public:
2525

26+
~qpLinkedList() {
27+
qpListNode <T> *cur = firstElement;
28+
while(cur) {
29+
qpListNode <T> *next = cur->next;
30+
delete cur->item;
31+
delete cur;
32+
cur = next;
33+
}
34+
}
35+
2636
byte numElements = 0;
2737

2838
T *getItem(int index) {
@@ -74,6 +84,44 @@ class qpLinkedList {
7484

7585
}
7686

87+
// Removes an item from the linked list.
88+
bool remove(T *itemToRemove) {
89+
qpListNode <T> *current = this->firstElement;
90+
qpListNode <T> *previous = nullptr;
91+
92+
while(current) {
93+
94+
if(current->item == itemToRemove) {
95+
// If this node is currently marked as the "current node", update it.
96+
if(this->currentElement == current) {
97+
this->currentElement = current->next;
98+
}
99+
100+
// update the previous node to point to the new item
101+
if(previous) {
102+
previous->next = current->next;
103+
}
104+
105+
// Update the first/last items
106+
if(current == this->firstElement) {
107+
this->firstElement = previous;
108+
}
109+
if(current == this->lastElement) {
110+
this->lastElement = current->next;
111+
}
112+
113+
delete current->item;
114+
delete current;
115+
return true;
116+
}
117+
118+
previous = current;
119+
current = current->next;
120+
}
121+
122+
return false;
123+
}
124+
77125
};
78126

79127
#endif

qpPattern.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ void qpPattern::deactivateIfActivePeriodComplete() {
9595

9696
}
9797

98+
bool qpPattern::shouldRemoveWhenDecativated() {
99+
return this->removeOnDeactivation;
100+
}
101+
98102
//direct external control
99103
void qpPattern::deactivate() {
100104

@@ -125,6 +129,12 @@ CRGBPalette16 qpPattern::_getPalette(byte index) {
125129

126130
// ~ Periodic activation config
127131

132+
qpPattern &qpPattern::removeWhenDeactivated(bool value) {
133+
this->removeOnDeactivation = value;
134+
135+
return *this;
136+
}
137+
128138
qpPattern &qpPattern::activatePeriodicallyEveryNTicks(int minTicks, int maxTicks) {
129139

130140
this->isActive = false;

qpPattern.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class qpPattern {
4141

4242
// ~ Periodic activation
4343

44+
bool removeOnDeactivation = false;
4445
unsigned int minTicksBetweenActivations = 0;
4546
unsigned int maxTicksBetweenActivations = 0;
4647
unsigned int ticksUntilActive = 0;
@@ -112,7 +113,7 @@ class qpPattern {
112113

113114

114115
// ~ Periodic activation
115-
116+
qpPattern &removeWhenDeactivated(bool value);
116117
qpPattern &activatePeriodicallyEveryNTicks(int minTicks, int maxTicks = 0);
117118

118119
qpPattern &stayActiveForNTicks(int minTicks, int maxTicks = 0);
@@ -148,7 +149,7 @@ class qpPattern {
148149

149150
bool activate();
150151
void deactivate();
151-
152+
bool shouldRemoveWhenDecativated();
152153
};
153154

154155
#endif

quickPatterns.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ quickPatterns::quickPatterns(CRGB *leds, int numLeds) {
99
random16_add_entropy(analogRead(0));
1010
}
1111

12+
quickPatterns::~quickPatterns() {
13+
delete this->lightStrand;
14+
}
1215

1316
// Render
1417

quickPatterns.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class quickPatterns {
4848
public:
4949

5050
quickPatterns(CRGB *leds, int numLeds);
51-
51+
~quickPatterns();
52+
5253
// ~ Rendering
5354

5455
bool draw();

0 commit comments

Comments
 (0)