-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightshowController.cpp
146 lines (117 loc) · 2.96 KB
/
LightshowController.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
#include "Particle.h";
#include "neopixel.h";
#include "definitions.h";
#include "LightshowController.h";
LightshowController::LightshowController(Adafruit_NeoPixel *strip) {
this->strip = strip;
timer = new Timer(20, &LightshowController::advanceShow, *this);
isWaiting = false;
showPlaying = false;
currentShow = "";
loopShow = false;
showSteps = 0;
currentStep = 0;
}
void LightshowController::playShow(String showId, bool loop, std::function<void()> callback) {
showEndCallback = callback;
loopShow = loop;
currentShow = showId;
currentStep = 0;
showPlaying = true;
}
void LightshowController::tick() {
if(isWaiting) {
return;
}
if(currentShow.equals("idle")) {
idleShow();
} else if(currentShow.equals("live")) {
liveShow();
} else if(currentShow.equals("win")) {
winShow();
} else if(currentShow.equals("lose")) {
loseShow();
}
}
void LightshowController::idleShow() {
showSteps = 255;
this->setLights();
strip->show();
timer->changePeriod(20);
timer->reset();
isWaiting = true;
}
void LightshowController::liveShow() {
showSteps = 1;
uint32_t c = strip->Color(255, 0, 0);
setAll(c);
strip->setBrightness(255);
strip->show();
// WARNING!!! These software timers really seem to interfere with the
// tone library. If we change the period or the timer ends/interrupts
// while tone is playing, it will cause stuttering and/or tone stops.
timer->changePeriod(2000);
timer->reset();
isWaiting = true;
}
void LightshowController::winShow() {
showSteps = 1;
uint32_t c = strip->Color(255, 0, 255);
setAll(c);
strip->show();
timer->changePeriod(2000);
timer->reset();
isWaiting = true;
}
void LightshowController::loseShow() {
showSteps = 1;
blank();
timer->changePeriod(2000);
timer->reset();
isWaiting = true;
}
void LightshowController::advanceShow() {
timer->stop();
isWaiting = false;
currentStep += 1;
if(currentStep > showSteps) {
currentStep = 0;
if(!loopShow) {
endShow();
}
}
}
void LightshowController::endShow() {
currentShow = "";
showPlaying = false;
strip->setBrightness(128);
showEndCallback();
}
void LightshowController::setLights() {
int i;
for(i = 0; i < strip->numPixels(); i++) {
strip->setPixelColor(i, this->wheel((i+currentStep) & 255));
}
}
void LightshowController::setAll(uint32_t color) {
for(int i = 0; i < strip->numPixels(); i++) {
strip->setPixelColor(i, color);
}
}
void LightshowController::blank() {
setAll(strip->Color(0,0,0));
strip->show();
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t LightshowController::wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip->Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip->Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip->Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}