-
Notifications
You must be signed in to change notification settings - Fork 2
/
PatternList.cpp
69 lines (57 loc) · 1.2 KB
/
PatternList.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
#include "PatternList.h"
#include "Arduino.h"
LED_CONTROLLER_NAMESPACE_USING
PatternList::PatternList(Pattern* pattern) : next(NULL) {
this->pattern = pattern;
}
PatternList::PatternList() : pattern(NULL), next(NULL) {}
void PatternList::insert(Pattern* pattern) {
PatternList* list = new PatternList(pattern);
if (list == NULL) {
Serial.println("!l");
return;
}
insert(list);
}
void PatternList::insert(PatternList* next) {
if (next->next != NULL) {
delete next->next;
}
PatternList* oldNext = this->next;
this->next = next;
this->next->next = oldNext;
}
void PatternList::removeNext() {
if (next == NULL) {
return;
}
PatternList* oldNext = next;
next = oldNext->next;
oldNext->next = NULL;
delete oldNext;
}
bool PatternList::update() {
bool updated = false;
if (pattern != NULL) {
updated |= pattern->update();
}
if (next != NULL) {
updated |= next->update();
if (next->pattern != NULL && next->pattern->isExpired()) {
removeNext();
}
}
return updated;
}
void PatternList::apply(Color* stripColors) {
if (pattern != NULL) {
pattern->apply(stripColors);
}
if (next != NULL) {
next->apply(stripColors);
}
}
PatternList::~PatternList() {
delete next;
delete pattern;
}