-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pattern.h
167 lines (158 loc) · 4.36 KB
/
Pattern.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
void fadeWhites(uint8_t value){
for(int i=1; i<NUM_LEDS-1; ++i)
if((leds[i].r==leds[i].g && leds[i].g==leds[i].b // if LED is white AND
&&(max(max(leds[i].r, leds[i].g), leds[i].b) > 32)) // is at least 32 brightness
&&((leds[i].r > max(max(leds[i-1].r, leds[i-1].g), leds[i-1].b) // AND LED is brighter than left neighbor OR
||(leds[i-1].r== leds[i-1].g && leds[i-1].g== leds[i-1].b))
&&(leds[i].r > max(max(leds[i+1].r, leds[i+1].g), leds[i+1].b)
||(leds[i+1].r== leds[i+1].g && leds[i+1].g== leds[i+1].b)))
|| leds[i].r > 96)
leds[i].fadeToBlackBy(value);
}
void freshWhites(){
for(int i=0; i<NUM_LEDS; ++i)
if(leds[i].r==leds[i].g && leds[i].g==leds[i].b)
leds[i] = CRGB::White;
}
void goGray(){
for(uint16_t i=0; i<NUM_LEDS; ++i)
if(leds[i]){
newHue = max(max(leds[i].r, leds[i].g), leds[i].b);
leds[i] = CRGB(newHue,newHue,newHue);
}
}
void push(uint16_t k){
for(uint16_t i=k; i>0; --i)
leds[i] = leds[i-1];
}
void pull(uint16_t k){
for(uint16_t i=0; i<k; ++i)
leds[i] = leds[i+1];
}
void mirror(uint16_t k){
for(int i=0; i<k; ++i)
leds[k-i]=leds[i];
}
void copyIt(byte k){
memcpy(leds+NUM_LEDS/k, leds,
modus[k]<1?
NUM_LEDS/k*sizeof(CRGB):
NUM_LEDS/k*sizeof(CRGB)+sizeof(CRGB));
}
uint8_t getAverage255(uint8_t in[]){
uint16_t out = 0;
for(int i=0; i<SAMPLES; ++i)
out+=in[i];
return out/SAMPLES;
}
uint8_t getMax255(uint8_t in[]){
uint8_t out = 0;
for(int i=0; i<SAMPLES; ++i)
out=out<in[i]?in[i]:out;
return out;
}
uint8_t getMaxBass(uint8_t in[]){
uint8_t out = 0, mx = in[out];
for(int i=out+1; i<=6; ++i)
if(mx<in[i]){
mx = in[i];
out = i;
}
return out;
}
uint8_t getMaxTreb(uint8_t in[]){
uint8_t out = 6, mx = in[out];
for(int i=out-1; i>=0; --i)
if(mx<in[i]){
mx = in[i];
out = i;
}
return out;
}
uint16_t idx;
uint8_t bri[NUM_BANDS], dfm[NUM_BANDS], dfa[NUM_BANDS],
smp[NUM_BANDS][SAMPLES],dif[NUM_BANDS][SAMPLES],
bass[NUM_BANDS], treb[NUM_BANDS];
void saveBands(uint16_t in){
for(byte b=0; b<7; ++b)
//smp[b][idx] = pow(spectrumByte[b]>>4,2);
smp[b][idx] = spectrumByte[b];
for(byte b=0; b<NUM_BANDS; ++b)
dif[b][in] = abs(smp[b][in+1]-smp[b][in]);
}
void analyzeBands(){
static bool bb = true;
static bool bt = true;
byte newBass = 0;
byte newTreb = 6;
// Get Average
for(byte b=0; b<NUM_BANDS; ++b)
bri[b] = getAverage255(smp[b]);
// Get Average Difference
for(byte b=0; b<NUM_BANDS; ++b)
dfa[b] = getAverage255(dif[b]);
// Get Biggest Difference
for(byte b=0; b<NUM_BANDS; ++b)
dfm[b] = getMax255(dif[b]);
// Bass Analysis
for(byte b=0; b<NUM_BANDS; ++b)
bass[b] = bri[b]>dfm[b]? 0: dfm[b]-bri[b];
// Treb Analysis
for(byte b=0; b<NUM_BANDS; ++b)
treb[b] = dfa[b]>>2+bri[b]>>3+dfm[b]>>1;
// Weighted Current Band
//bass[bassBand] *= 9>>3;
//treb[trebBand] *= 9>>3;
// Weighted Default Band
bass[0] *= 9>>3;
treb[6] *= 9>>3;
// Band Assignment
newTreb = getMaxTreb(treb);
newBass = getMaxBass(bass);
// 2 Strikes Against The Treb Band
/* if(bt&&(newTreb!=trebBand)) bt = false;
else if(!bt&&newTreb!=trebBand){
trebBand = newTreb;
bt = true;
}*/
// 2 Strikes Against The Bass Band
if(bb&&(newBass!=bassBand)) bb = false;
else if(!bb&&newBass!=bassBand){
bassBand = newBass;
bb = true;
}
}
void newFlow(){
static uint16_t idx;
static uint8_t rgbRate, huey;
uint8_t bri;
EVERY_N_SECONDS(2){ if(spectrumAvg>32) analyzeBands(); else bassBand=0,trebBand=6; }
fadeWhites(16);
bool zero = speed==0? true: false;
uint16_t k = edges[speed];
uint16_t hk = k>>1;
uint16_t ok = 0;
saveBands(idx);
//bri = smp[trebBand][idx];
bri = spectrumByte[6];
bri = bri<32?0:bri;
//Serial.println(rgbRate);
idx = idx==SAMPLES-1? 0: idx+1;
if(rgbRate>3) rgbRate-=rgbRate>>2;
if(rgbRate>0) --rgbRate;
push(zero? k: hk);
//Serial.println(bri);
if(beatDetect()){
freshWhites();
rgbRate+=16;
huey+=rgbRate;
leds[ok] = CRGB::White;
} else{ // beatDetect()
if(trebDetect())
rgbRate+=8;
huey+=rgbRate;
leds[ok] = ColorFromPalette(RainbowColors_p, huey, bri, LINEARBLEND);
} // beatDetect()
if(bri==0) leds[ok] = CRGB(0,1,0);
if(!zero){ mirror(k); copyIt(speed); }
}