forked from pixelmatix/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStar.h
More file actions
376 lines (276 loc) · 7.54 KB
/
Star.h
File metadata and controls
376 lines (276 loc) · 7.54 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*******************************************************
Star.h - star class - creates and moves the stars
This is the star class, and typically 200-400 stars are
instaniated in an array. A star may be tagged as 'alive'
or not. Dead stars are simply black and do not move.
Based on the audio input, the next "available" dead star is
assigned a color, speed/direction, speed decay, brightness,
brightness decay, and is marked 'alive'. Functions in the
class then move the star across the display, decrementing
its brightness and speed until it reaches a lower threshold
where it "dies" and get recycled ("rebirth").
This variation creates stars randomly in x-axis, with a CCW
direction. If star hits lower edge, it is transformed onto
the bottom panel with same speed & heading, but due to the
45 degree rotation of the bottom panel, the tranformation
is angled. Maybe I'll fix that someday. If the stars on bottom
panel hit an edge, they die (for now).Stars also have long
'tails'. This started in dev to follow movements, and I kinda
liked it. Adjust using the persistance setting.
*********************************************************/
#pragma once
#include "colorUtils.h"
// dev star print tool
//#define PRINT_ENABLE
// constants for starburst
const uint16_t NUM_STARS = 300; // number of total 'stars'
uint8_t MAX_COLOR_MODES = 4; // number of available color modes
uint8_t MAX_PATTERN_MODES = 3; // number of available color modes
// prototypes - non-class functions
void incrementAngle();
// settings constants/globals
int8_t patternMode = 0; // curent pattern selection
int8_t colorMode = 0; // color generation mode - see star class
uint8_t currentColor = 0;
float angle = 0;
uint8_t wheelPos = 0;
const uint8_t MIN_BRIGHTNESS = 5;
const float MIN_SPEED = 0.08;
class Star
{
public:
Star();
bool starBirth();
void tick();
bool isAlive();
private:
void setPattern(int8_t);
void setColor(int8_t);
void drawStar();
void eraseStar();
void bounceOffWalls();
void printStar();
bool _alive;
float _x;
float _y;
rgb24 _color;
float _brightnessDecay;
float _xSpeed;
float _ySpeed;
float _speedDecay;
};
// quickie function to return absolute value of float
float pos(float num)
{
return (num > 0) ? num : -num;
}
// instantiate new empty star
Star :: Star()
{
_alive = true;
// start in center
_x = float(kMatrixCenterX);
_y = float(kMatrixCenterY);
}
bool Star :: starBirth()
{
// create new star if power on and audio exceeds threshold
if (maxLevel > lowThreshold)
{
// start in center
_x = float(kMatrixCenterX);
_y = float(kMatrixCenterY);
// configure star charactoristics
setColor(colorMode);
setPattern(patternMode);
// increment angle for next star
incrementAngle();
_alive = true;
}
else
{
_color = BLACK;
_alive = false;
}
return _alive;
}
void Star :: setColor(int8_t mode)
{
switch (mode)
{
// basic colors based on eq level (default mode)
case 0:
_color = rgb24Colors8[maxBand];
break;
// cycle through color wheel
case 1:
wheelPos++;
if (wheelPos > 254) wheelPos = 0;
_color = wheel8(wheelPos);
break;
// assign color based on ticks
case 2:
_color = wheel8(ticks % 256);
break;
// color based on distance from center
case 3:
// color updated in tick() function
_color = rgb24Colors8[0];
break;
default:
colorMode = 0;
Serial.println("Error: Invalid color mode");
}
}
void Star :: setPattern(int8_t mode)
{
float speed;
switch (mode)
{
case 0:
// increase speed with volume
speed = 0.50 + (avgLevel / 300);
speed = constrain(speed, 0.25, 2.0);
_xSpeed = cos(angle) * speed;
_ySpeed = sin(angle) * speed;
// speed decay factor
_speedDecay = (float)random(980, 990) / 1000.0;
// brightness decay value. 1.00 is no decay
_brightnessDecay = (float)random(980, 999) / 1000.0;
persistance = 220;
break;
// long tails
case 1:
// increase speed with volume
speed = 0.50 + (avgLevel / 300);
speed = constrain(speed, 0.25, 2.0);
// calc x & y speeds
_xSpeed = cos(angle) * speed;
_ySpeed = sin(angle) * speed;
// speed decay factor
_speedDecay = 0.990; // nominal decay value
// brightness decay value
_brightnessDecay = (float)random(940, 999) / 1000.0;
persistance = 240;
break;
case 2:
// fixed settings
speed = 0.99;
_xSpeed = cos(angle) * speed;
_ySpeed = sin(angle) * speed;
_speedDecay = 0.990;
_brightnessDecay = 0.998;
persistance = 220;
break;
default:
patternMode = 0;
Serial.println("Error: Invalid pattern mode");
}
}
// increment star position
void Star :: tick()
{
// old method to erase star if dimming is not used
// eraseStar();
// color mode 4 must be updated while star is moving
if (colorMode == 4)
{
// calc distance from center
float x = pos(_x - kMatrixCenterX);
float y = pos(_y - kMatrixCenterY);
float dist = sqrt((x * x) + (y * y));
// scale for best looking color range
dist = constrain(dist * (180 / kMatrixCenterX), 0, 255);
_color = wheel8Sat(((uint8_t)dist & 255), 84);
}
// increment x & y positions
_x += _xSpeed;
_y += _ySpeed;
// hit wall?
bounceOffWalls();
// decrease speed each tick
_xSpeed *= _speedDecay;
_ySpeed *= _speedDecay;
// check speed and die if too slow
if (abs(_xSpeed) < MIN_SPEED && abs(_ySpeed) < MIN_SPEED)
{
_alive = false;
return;
}
// decrease star brightness each tick
_color.red *= _brightnessDecay;
_color.green *= _brightnessDecay;
_color.blue *= _brightnessDecay;
// check brightness and die if too dim
if (_color.red < MIN_BRIGHTNESS && _color.green < MIN_BRIGHTNESS && _color.blue < MIN_BRIGHTNESS)
{
_alive = false;
return;
}
// now draw updated star
drawStar();
}
void Star :: eraseStar()
{
backgroundLayer.drawPixel((uint16_t)_x, (uint16_t)_y, BLACK);
}
void Star :: drawStar()
{
// don't need to update dead (black) stars
if (!_alive)
return;
// draw the pixel (a.k.a. star)
backgroundLayer.drawPixel(uint16_t(_x), uint16_t(_y), _color);
}
bool Star :: isAlive()
{
return _alive;
}
void incrementAngle()
{
// increment global var angle
angle += 1.2; //(float)random(10, 200) / 100;
if (angle >= TWO_PI) angle -= TWO_PI;
}
void Star :: bounceOffWalls()
{
// bounce off walls
if (_x > kScreenWidth - 1)
{
_x = kScreenWidth - 1;
_xSpeed *= -1.0;
}
if (_x < 0)
{
_x = 0;
_xSpeed *= -1.0;
}
if (_y > kScreenHeight - 1)
{
_y = kScreenHeight - 1;
_ySpeed *= -1.0;
}
if ( _y < 0)
{
_y = 0;
_ySpeed *= -1.0;
}
}
#ifdef PRINT_ENABLE
void Star :: printStar()
{
if (_alive && ticks % 100)
{
Serial.print("x: "); Serial.print(_x);
Serial.print(" y: "); Serial.println(_y);
Serial.print("xSpeed: "); Serial.print(_xSpeed);
Serial.print(" ySpeed: "); Serial.println(_ySpeed);
Serial.print("spdDecay: "); Serial.println(_speedDecay, 4);
Serial.print("red: 0x"); Serial.println(_color.red, HEX);
Serial.print("green: 0x"); Serial.println(_color.green, HEX);
Serial.print("blue: 0x"); Serial.println(_color.blue, HEX);
Serial.print("brightDecay: "); Serial.println(_brightnessDecay);
Serial.println();
}
}
#endif