-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.cpp
386 lines (301 loc) · 9.69 KB
/
display.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
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
377
378
379
380
381
382
383
384
385
386
/*
*/
#include <Arduino.h>
#include "arm_math.h"
#include "devconf.h"
#include "display.h"
#define FASTLED_INTERNAL
#include "FastLED.h"
// ======================================================================================================
CRGB leds[NUM_LEDS];
float hueStep;
int numBands;
int nextBall = 0;
uint32_t lastMoveMs = 0;
float pos[MAX_BALLS];
float vel[MAX_BALLS];
uint8_t band[MAX_BALLS];
uint8_t numBalls[NUM_BANDS];
short displayMode;
uint32_t modeChangeRelease = 0;
double gainSlope = 1.0;
double minScale = 0.0;
double lowTrip = 0;
double highTrip = 1;
int orangeLED;
int redLED;
double peakFilter = 0;
double levelFilter = 0;
// ======================================================================================================
// Generic Display Functions
// ======================================================================================================
void setDisplayMode(short mode) {
displayMode = mode;
}
short getDisplayMode() {
return(displayMode);
}
short switchDisplayMode() {
displayMode = (short)((displayMode + 1 ) % NUM_MODES);
modeChangeRelease = millis() + MODE_CHANGE_PAUSE;
showMode();
return (displayMode);
}
void initDisplay(){
delay(250); // sanity check delay
FastLED.addLeds<APA102, BGR>(leds, NUM_LEDS);
FastLED.clear();
switch (displayMode) {
default:
case 0:
break;
case 1:
orangeLED = (int)((ORANGE_DB - MIN_DB) / DB_PER_LED);
redLED = (int)((RED_DB - MIN_DB) / DB_PER_LED);
break;
case 2:
initFFTDisplay(NUM_BANDS);
case 3:
initFFTDisplay(NUM_BANDS);
break;
case 4:
initBallDisplay(NUM_BANDS);
break;
}
}
void updateDisplay(uint32_t * bandValues) {
// display the data in the selected way.
if (millis() > modeChangeRelease) {
switch (displayMode) {
default:
case 0:
break;
case 2:
updateFFTDisplay(bandValues);
break;
case 3:
updateToneDisplay(bandValues);
break;
case 4:
updateBallDisplay(bandValues);
break;
}
}
}
// ======================================================================================================
// trajectory functions
// ======================================================================================================
// ======================================================================================================
// Basic display functions
// ======================================================================================================
void setLEDBand(int band, int intensity) {
setLED(band, (uint16_t)(band * hueStep), intensity);
}
int flipLEDs(int num) {
return (NUM_LEDS - num - 1);
}
void setLED(int pos, int hue, int intensity) {
if (pos < NUM_LEDS) {
if (FLIP_LED_ORDER){
pos = flipLEDs(pos);
}
leds[pos].setHSV(hue, 255, intensity);
}
}
void showMode () {
FastLED.clearData();
FastLED.show();
for (int I = 0; I < displayMode; I++) {
setLEDBand(I * 8, MAX_LED_BRIGHTNESS);
}
FastLED.show();
}
// ======================================================================================================
// FFT display functions
// ======================================================================================================
// Configure the LED string and preload the color values for each band.
void initFFTDisplay(int numberBands) {
const double MIN_GAIN_SCALE = 0.00125;
const double MAX_GAIN_SCALE = 0.10;
const double LOW_TRIP = 0.05;
const double HIGH_TRIP = 0.25; // Was 0.3
minScale = MIN_GAIN_SCALE;
gainSlope = pow((MAX_GAIN_SCALE / MIN_GAIN_SCALE), 1.0 / MAX_GAIN_NUM);
lowTrip = LOW_TRIP;
highTrip = HIGH_TRIP;
numBands = numberBands;
hueStep = TOP_HUE_NUMBER / numBands; // How much the LED Hue changes for each step.
}
// Update the LED string based on the intensities of all the Frequency bins.
void updateFFTDisplay (uint32_t * bandValues){
uint16_t ledBrightness;
// Process the LED buckets into LED Intensities
for (byte band = 0; band < numBands; band++) {
// Scale the bars for the display
ledBrightness = bandValues[band];
if (ledBrightness > MAX_LED_BRIGHTNESS) {
ledBrightness = MAX_LED_BRIGHTNESS;
}
// Display LED Band in the correct Hue.
setLEDBand(band, ledBrightness);
}
// Update LED display
FastLED.show();
}
// ======================================================================================================
// Tone display functions
// ======================================================================================================
// Update the LED string based on the intensities of all the Frequency bins.
void updateToneDisplay (uint32_t * bandValues){
uint16_t ledBrightness = 0;
uint16_t maxBand = -1;
// Find the band with the strongets signal and light it up.
for (byte band = 0; band < numBands; band++) {
if (bandValues[band] > ledBrightness) {
ledBrightness = bandValues[band];
maxBand = band;
}
}
if (ledBrightness > 0) {
FastLED.clearData();
// Display LED Band in the correct Hue.
if (ledBrightness > MAX_LED_BRIGHTNESS) {
ledBrightness = MAX_LED_BRIGHTNESS;
}
// Update LED display
setLEDBand(maxBand, ledBrightness);
FastLED.show();
}
}
// ======================================================================================================
// Air Balls display functions
// ======================================================================================================
// Configure the LED string and preload the color values for each band.
void initBallDisplay(int numberBands) {
numBands = numberBands;
hueStep = TOP_HUE_NUMBER / numBands; // How much the LED Hue changes for each step.
const double MIN_GAIN_SCALE = 0.001;
const double MAX_GAIN_SCALE = 0.05;
const double LOW_TRIP = 0.04;
const double HIGH_TRIP = 0.30; // Was 0.35
minScale = MIN_GAIN_SCALE;
gainSlope = pow((MAX_GAIN_SCALE / MIN_GAIN_SCALE), 1.0 / MAX_GAIN_NUM);
lowTrip = LOW_TRIP;
highTrip = HIGH_TRIP;
// initialize ball data;
memset(pos, 0, sizeof(pos));
memset(vel, 0, sizeof(vel));
memset(band, 0, sizeof(band));
memset(numBalls, 0, sizeof(numBalls));
}
void updateBallDisplay (uint32_t * bandValues){
// see if we need to add some new balls.
addBalls(bandValues);
// Update LED display
displayBalls();
moveBalls();
}
void addBalls(uint32_t * bandValues){
uint32_t val;
double velocity;
for (int b = 0; b < numBands; b++ ){
val = bandValues[b];
if (val > MAX_LED_BRIGHTNESS) {
val = MAX_LED_BRIGHTNESS;
}
if (val > BALL_THRESHOLD) {
velocity = (double)val / 100.0;
addBall(velocity, b);
}
}
}
void addBall(float avel, int aband) {
// add new ball to end of list of there is room
if (nextBall < MAX_BALLS) {
if (numBalls[aband] < MAX_BAND_BALLS) {
pos[nextBall] = 0;
vel[nextBall] = avel;
band[nextBall] = aband;
numBalls[aband]++;
nextBall++;
} else {
}
}
}
void moveBalls() {
uint32_t tnow = millis();
double elapsed = (double)(tnow - lastMoveMs) * 0.001;
double deltaV = GRAVITY * elapsed ;
double halfATSquared = deltaV * elapsed * 0.5 ;
// process each ball
for (int ball = 0; ball < nextBall; ball++) {
pos[ball] += (halfATSquared + (vel[ball] * elapsed));
vel[ball] += deltaV ;
// has the ball hit the ground?
if (pos[ball] <= 0) {
// remove this ball and move all others down.
nextBall--;
numBalls[band[ball]]--;
for (int b = ball; b < nextBall; b++) {
pos[b] = pos[b+1];
vel[b] = vel[b+1];
band[b] = band[b+1];
}
}
}
lastMoveMs =tnow;
}
void displayBalls() {
// process each ball
FastLED.clearData();
for (int ball = 0; ball < nextBall; ball++) {
setLED((int)(pos[ball] * LED_PER_METER), band[ball] * hueStep, MAX_LED_BRIGHTNESS);
}
FastLED.show();
}
// ======================================================================================================
// VU Meter display
// ======================================================================================================
void updateVuDisplay(double p2p) {
double db = (9.1024 * log(p2p)) + 115.82;
if (millis() > modeChangeRelease) {
levelFilter = spikeFilter(levelFilter, db, 0.2, 0.05);
peakFilter = spikeFilter(peakFilter, db, 1.0, 0.001);
int levelLED = (int)((levelFilter - MIN_DB) / DB_PER_LED);
int peakLED = (int)((peakFilter - MIN_DB) / DB_PER_LED);
if (peakLED < levelLED) {
peakLED = levelLED;
}
// run up from bottom of display to top.
FastLED.clearData();
short hue = 96;
for (short l=0; l < NUM_LEDS; l++) {
if (l >= redLED)
hue = 0;
else if (l >= orangeLED)
hue = 32;
if (l == peakLED){
setLED(l, hue, MAX_LED_BRIGHTNESS);
} else if (l <= levelLED) {
setLED(l, hue, MAX_LED_BRIGHTNESS / 4);
} else {
setLED(l, hue, DIM_LED_BRIGHTNESS);
}
}
FastLED.show();
}
/*
Serial.print(peakFilter);
Serial.print(" ");
Serial.print(levelFilter);
Serial.println(" 50 100");
*/
}
double spikeFilter(double filter, double live, double upTC, double downTC){
if (live > filter) {
filter += ((live - filter) * upTC);
} else {
filter += ((live - filter) * downTC);
}
return (filter);
}