forked from brimshot/quickPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickPatterns.cpp
127 lines (74 loc) · 2.58 KB
/
quickPatterns.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
#include <quickPatterns.h>
quickPatterns::quickPatterns(CRGB *leds, int numLeds) {
this->lightStrand = new qpLightStrand(leds, numLeds);
random16_add_entropy(random(0, 1000));
random16_add_entropy(random(0, 1000));
random16_add_entropy(analogRead(0));
}
quickPatterns::~quickPatterns() {
delete this->lightStrand;
}
// Render
bool quickPatterns::draw() {
uint32_t currentMillis = millis();
if(currentMillis >= this->nextTickMillis) {
this->nextTickMillis = (currentMillis + this->tickLengthInMillis);
this->lightStrand->clearMain();
this->currentScene->draw(this->lightStrand->getLeds(), this->lightStrand->getNumLeds());
return true;
}
return false;
}
// Quick add pattern
qpPattern &quickPatterns::addPattern(qpPattern *pattern) {
return this->scene(0).newLayer().addPattern(pattern);
}
// Returns requested scene or automatically adds and returns a new scene if index does not yet exist
qpScene &quickPatterns::scene(byte index) {
if(index > (this->scenes.numElements - 1))
return this->newScene();
this->lastReferencedScene = this->scenes.getItem(index);
return *this->lastReferencedScene;
}
qpScene &quickPatterns::newScene() {
this->lastReferencedScene = this->scenes.append(new qpScene(this->lightStrand));
// if this is the first scene added, make it our current scene
if(this->scenes.numElements == 1)
this->currentScene = this->lastReferencedScene;
return *this->lastReferencedScene;
}
// ~ Access
qpLayer &quickPatterns::layer(byte layerIndex) {
return this->scene(0).layer(layerIndex);
}
// ~ Scene navigation
void quickPatterns::nextScene() {
this->sceneIndex++;
if(this->sceneIndex >= this->scenes.numElements)
this->sceneIndex = 0;
this->playScene(this->sceneIndex);
}
void quickPatterns::playScene(byte index) {
if(index >= this->scenes.numElements)
return;
this->lightStrand->clearAll();
this->sceneIndex = index;
this->currentScene = this->scenes.getItem(index);
}
void quickPatterns::playRandomScene() {
byte index;
do {
index = random8(0, (this->scenes.numElements));
} while(index == this->sceneIndex);
this->playScene(index);
}
// Quick access operators
qpPattern&quickPatterns::operator()(byte layerIndex) {
return this->scene(0).layer(layerIndex).pattern(0);
}
qpPattern&quickPatterns::operator()(byte sceneIndex, byte layerIndex) {
return this->scene(sceneIndex).layer(layerIndex).pattern(0);
}
qpPattern&quickPatterns::operator()(byte sceneIndex, byte layerIndex, byte patternIndex) {
return this->scene(sceneIndex).layer(layerIndex).pattern(patternIndex);
}