-
Notifications
You must be signed in to change notification settings - Fork 3
/
patFireworks.h
276 lines (236 loc) · 6.65 KB
/
patFireworks.h
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
#include "SFx.h"
#include "FastLED_RGBW.h"
// Define our different types of explotion
enum EXPLOTION {
SPIRAL,
COLUMN
};
//We use the palette to dictate the color of the spark during its lifespan
//In this instance we use 4 entries per lifespan so for our palette16 we can
// represent 4 different colour scienarios
CRGBPalette16 initPalette()
{
CRGB col1 = CRGB::White ;
CRGB col2 = CRGB::Fuchsia;
CRGB col3 = CRGB::Maroon;
CRGB black = CRGB::Black;
CRGB col4 = CRGB::Green;
CRGB col5 = CRGB:: Yellow;
CRGB col6 = CRGB:: Blue;
CRGBPalette16 pal;
pal = CRGBPalette16(
col1,
col1,
col1,
col3,
col5,
col5,
col5,
col1,
col6,
col6,
col6,
col5,
col4,
col4,
col4,
col1 );
return pal;
}
// class representing a spark
class spark {
private:
float speed;
float fPos;
float fRow;
float fCol;
EXPLOTION effect;
int pos;
int life;
int lifeExp;
int palShift;
CRGBW color;
bool lit;
CRGBPalette16 pal;
public:
spark(){
lit = false;
};
void light(float c, float r, float s, CRGBPalette16 p, EXPLOTION e = SPIRAL ){
palShift = 1;
lit = true;
effect = e;
fPos = XY((long)c, (long)r);
fCol = c;
fRow = r;
speed = s;
pal = p;
lifeExp = random8(200) + 1000;
life = lifeExp;
if (effect==COLUMN) speed/=5;
};
bool display(){
int index;
if (!lit ) return lit ;
life--;
index =64.0f - ((float)life/(float)lifeExp * 64.0f) + (64.0f * palShift);
color = ColorFromPalette(pal,index);
switch (effect) {
case SPIRAL:{
fPos+=speed;
pos = (long)fPos;
// apply gravity to speed
// Make gravity have less of an effect as
// the spark reaches the end of its lifespan
// i.e.as it gets lighter and floats
speed -= .00015 * (float)life/(float)lifeExp;
// put the spark out if it reaches the edge
// or reaches the end of its lifespan
if (fPos<0 || fPos>=NUM_LEDS || life<0){
lit = false;
return lit;
}
DrawPixels(fPos, 1, color);
break;
}
case COLUMN:{
fRow+=speed;
// Slow down fast initially and then more gentally as the spark loses mass
speed *= 0.0075f * (float)life/(float)lifeExp;
// Apply gravity less and less as spark loses mass
speed -= 0.000005f * (float)life/(float)lifeExp;
// put the spark out if it reaches the edge
// or reaches the end of its lifespan
if (fRow<0 || fRow>=NUM_ROWS || life<0){
lit = false;
return lit;
}
DrawColumn(fCol, fRow, 1, color);
break;
}
}
return lit;
}
};
class rocket{
public:
rocket (){
stage = 0;
}
void fire(void){
if (stage) return;
nextStage();
}
void explode(int p, EXPLOTION e){
if (stage) return;
pos = p;
effect = e;
fRow = (long)pos/5;
fCol = mod8(pos, 5);
stage =1;
nextStage();
}
void draw() {
switch (stage) {
case 0: //Not lit yet
return;
case 1: // Shoot up into the sky
pos = XY((long)fCol, (long)fRow);
Serial.println (pos);
DrawColumn(fCol, fRow, 1, color);
for (int i = 5; i < 5*tail; i+=5) {
if (pos>i) leds[pos-i].fadeToBlackBy(random8(180)); // Fade tail
}
speed -= 0.0004; //Apply some gravity
fRow += speed;
//Check if we are ready to explode
if (((long)fRow >= NUM_ROWS)||(speed < -0.005)) {
nextStage(); // Time to Explode
return;
}
break;
case 2: { // Display sparks
bool finished = true;
bool lit;
for(int i=0; i<numSparks; i++){
//Advance each spark
lit = Spark[i].display();
if (lit) finished = false;
}
if (finished) nextStage();
break;
}
}
}
private:
const uint8_t tail=6; //half pixels so double up
int numSparks = 14;
spark Spark[14];
uint8_t stage;
uint8_t col;
uint8_t row =0;
int pos;
float fPos;
float fCol;
float fRow;
float speed;
CRGBW color;
CRGBPalette16 pal;
EXPLOTION effect = COLUMN;
void nextStage() {
switch (stage) {
case 0: // Light the touch paper
Serial.println("Fire");
col = random8(NUM_COLS);
fRow=0;
fCol = col;
fPos=0;
pos=0;
speed = 0.120 ;
color = CRGB(0,0,255);
color.w = 100;
stage = 1; // Advance to shooting up into the sky
break;
case 1: // Set off Explotion
{
Serial.println("Explode");
switch (effect){
case SPIRAL:{
numSparks =14;
break;
}
case COLUMN:{
numSparks = 6;
break;
}
}
pal = initPalette();
for(int i=0; i<numSparks; i++){
speed = (0.17f * (float)i/numSparks) + (random8(10)/500.0f) ;
if (random8(2) ==1) speed *= -1;
//Ignite each spark
Spark[i].light(fCol, fRow, speed, pal, effect);
}
stage = 2; // Advance to Explotion
break;
}
case 2: // Advance to being spent and wait to be lit again
stage = 0;
break;
}
}
};
void drawFireworks(){
static int frame = 0;
static rocket Rocket[3];
// if (!frame) rocket1.fire();
if (frame == 0) Rocket[0].explode(75, COLUMN);
// if (frame == 100) Rocket[1].explode(76, COLUMN);
// if (frame == 150) Rocket[2].explode(77, COLUMN);
frame++;
if (frame==1150) frame = 0;
FastLED.clear(false);
for (int i=0; i<3; i++) {
Rocket[i].draw();
}
}