-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack_parts.cpp
292 lines (251 loc) · 6.64 KB
/
pack_parts.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <ProtonPack.h>
#include "pack_parts.h"
Powercell::Powercell(int offset) : PackComponent(offset, 15) {
}
void Powercell::onPackInitStart(Pack pack) {
_initializing = true;
}
void Powercell::onPackInitComplete(Pack pack) {
_initializing = false;
}
void Powercell::onUpdate(Pack pack) {
if (_initializing) {
for(int i=0; i < _num_leds; i++) {
if (i == _current_led) {
setLed(i, _MAX_BRIGHTNESS);
} else {
setLed(i, 0);
}
}
if (_current_led > 0) {
setLed(_current_led, _MAX_BRIGHTNESS / 2);
}
if (_current_led > 1) {
setLed(_current_led, _MAX_BRIGHTNESS / 4);
}
} else {
for (int i=0; i<= _num_leds; i++) {
if (i >= _current_led) {
setLed(i, _MAX_BRIGHTNESS);
} else {
setLed(i, 0);
}
}
}
_current_led = pp_decrement(_current_led, _num_leds - 1);
if ( _current_led == 0 ) {
callAgainIn(120);
} else {
callAgainIn(40);
}
}
void Powercell::reset(Pack pack) {
_current_led = 0;
_initializing = true;
for(int i=0; i < _num_leds; i++) {
setLed(i, 0);
}
}
Cyclotron::Cyclotron(int offset) : PackComponent(offset, 14) {
_last_switched_at = millis();
}
void Cyclotron::onUpdate(Pack pack) {
static int BRIGHTNESS_INCREMENT = 80;
int since_last_switched = pack.now - _last_switched_at;
if (since_last_switched >= 1000) {
_current_brightness = 0;
setLed(_current_led, _current_brightness);
_current_led = pp_mod_increment(_current_led, 4);
_last_switched_at = pack.now;
} else if ( since_last_switched <= 500 ) {
_current_brightness += BRIGHTNESS_INCREMENT;
} else if (_current_brightness > 0) {
_current_brightness -= BRIGHTNESS_INCREMENT;
}
setLed(_current_led, _current_brightness);
callAgainIn(20);
}
void Cyclotron::reset(Pack pack) {
_current_led = 0;
_current_brightness = 0;
_last_switched_at = pack.now;
for (int i=0; i < 4; i++) {
setLed(i, 0);
}
}
Graph::Graph(int offset) : PackComponent(offset, 15) {
_direction = 1;
}
void Graph::onPackInitStart(Pack pack){
_iteration = _num_leds - 1;
_fill_pos = 0;
_is_initializing = true;
Serial.println("GRAPH INIT");
}
void Graph::onPackInitComplete(Pack pack){
reset(pack);
}
void Graph::onFiringStart(Pack pack){
reset(pack);
}
void Graph::onFiringStop(Pack pack){
reset(pack);
}
void Graph::onUpdate(Pack pack) {
const int MAX_LEDS_ON_IDLE = 14;
const int BRIGHT = 4000;
const int O = 1000;
const int o = BRIGHT;
const int _ = 0;
const int firing_sequence[][15] = {
{_, _, _, _, _, _, _, O, _, _, _, _, _, _, _},
{_, _, _, _, _, _, o, O, o, _, _, _, _, _, _},
{_, _, _, _, _, o, O, _, O, o, _, _, _, _, _},
{_, _, _, _, o, O, _, _, _, O, o, _, _, _, _},
{_, _, _, o, O, _, _, _, _, _, O, o, _, _, _},
{_, _, o, O, _, _, _, _, _, _, _, O, o, _, _},
{_, o, O, _, _, _, _, _, _, _, _, _, O, o, _},
{o, O, _, _, _, _, _, _, _, _, _, _, _, O, o},
};
if(pack.is_firing) {
for(int i=0; i < _num_leds; i++) {
setLed(i, firing_sequence[_iteration][i]);
}
_iteration = pp_mod_increment(_iteration, 7);
callAgainIn(40);
} else if (_is_initializing) {
if ( _fill_pos == _num_leds - 1) {
callAgainIn(50);
return;
}
for(int i=0; i < _fill_pos; i++) {
setLed(i, BRIGHT);
}
setLed(_iteration, BRIGHT);
setLed(_iteration + 1, 0);
_iteration = pp_decrement(_iteration, _num_leds - 1);
if(_iteration == _fill_pos) {
_fill_pos = pp_mod_increment(_fill_pos, _num_leds - 1);
_iteration = _num_leds - 1;
}
callAgainIn(44);
} else {
for(int i=0; i < _num_leds; i++) {
if (i < _iteration) {
setLed(i, BRIGHT);
} else {
setLed(i, 0);
}
}
_iteration += _direction;
if (_iteration == 0) {
_direction = 1;
} else if (_iteration == MAX_LEDS_ON_IDLE) {
_direction = -1;
}
callAgainIn(50);
}
}
void Graph::reset(Pack pack) {
_iteration = 0;
_direction = 1;
_is_initializing = false;
_fill_pos = 0;
for(int i=0; i < _num_leds; i++) {
setLed(i, 0);
}
}
Nozzle::Nozzle(int offset) : PackComponent(offset, 1) {
}
void Nozzle::onFiringStart(Pack pack) {
_is_firing = true;
}
void Nozzle::onFiringStop(Pack pack) {
_is_firing = false;
setLed(0, 0);
}
void Nozzle::onUpdate(Pack pack) {
if ( _is_firing) {
if (random(0, 100) >= 45) {
setLed(0, 4000);
} else {
setLed(0, 0);
}
}
callAgainIn(50);
}
void Nozzle::reset(Pack pack) {
_is_firing = false;
}
WandLights::WandLights(int offset) : PackComponent(offset, 4) {
}
void WandLights::reset(Pack pack) {
_is_on = false;
_is_firing = false;
_is_initializing = true;
setLed(SLO_BLO, 0);
setLed(TOP_INDICATOR, 1000);
setLed(FRONT_LIGHT, 1000);
setLed(INTERNAL_LIGHT, 4095);
}
void WandLights::onFiringStart(Pack pack) {
_is_firing = true;
callAgainIn(1);
}
void WandLights::onFiringStop(Pack pack) {
_is_firing = false;
}
void WandLights::onPackInitStart(Pack pack) {
_is_initializing = true;
}
void WandLights::onPackInitComplete(Pack pack){
_is_initializing = false;
}
void WandLights::onUpdate(Pack pack) {
if (_is_initializing) {
setLed(SLO_BLO, 0);
callAgainIn(200);
} else {
if (_is_on) {
setLed(SLO_BLO, 0);
} else {
setLed(SLO_BLO, 400);
}
_is_on = !_is_on;
if (_is_firing) {
callAgainIn(500);
} else if (_is_on) {
callAgainIn(2500);
} else {
callAgainIn(250);
}
}
}
Sound::Sound(): PackListener() {
pinMode(POWER_RELAY, OUTPUT);
pinMode(FIRING_START_RELAY, OUTPUT);
pinMode(FIRING_STOP_RELAY, OUTPUT);
digitalWrite(POWER_RELAY, HIGH);
digitalWrite(FIRING_START_RELAY, HIGH);
digitalWrite(FIRING_STOP_RELAY, HIGH);
}
void Sound::reset(Pack pack) {
digitalWrite(POWER_RELAY, HIGH);
digitalWrite(FIRING_START_RELAY, HIGH);
digitalWrite(FIRING_STOP_RELAY, HIGH);
}
void Sound::onFiringStart(Pack pack) {
digitalWrite(FIRING_START_RELAY, LOW);
digitalWrite(FIRING_STOP_RELAY, HIGH);
}
void Sound::onFiringStop(Pack pack) {
digitalWrite(FIRING_START_RELAY, HIGH);
digitalWrite(FIRING_STOP_RELAY, LOW);
}
void Sound::onPackInitStart(Pack pack) {
digitalWrite(POWER_RELAY, LOW);
}
void Sound::onPackInitComplete(Pack pack) {
digitalWrite(POWER_RELAY, HIGH);
}
*