|
@@ -31,7 +31,12 @@
|
31
|
31
|
// 256 / 8 = 32 => Divide by 31 (FACTOR) to get num of leds to light
|
32
|
32
|
// 255 / FACTOR = 8,...
|
33
|
33
|
// 127 / FACTOR = 4,...
|
|
34
|
+ #define THRESHOLD (FACTOR * 10 / 17)
|
34
|
35
|
|
|
36
|
+uint8_t maxVal(uint8_t data, uint8_t log);
|
|
37
|
+void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf);
|
|
38
|
+uint8_t average(uint8_t *data);
|
|
39
|
+void filterData(uint8_t *data);
|
35
|
40
|
void simpleVisualization(uint8_t *data);
|
36
|
41
|
void fullDepthVisualization(uint8_t *data);
|
37
|
42
|
void horribleWave(uint8_t *audioData);
|
|
@@ -39,33 +44,56 @@ void simpleLog(uint8_t *data);
|
39
|
44
|
void fullDepthLog(uint8_t *data);
|
40
|
45
|
|
41
|
46
|
#define NUMOFVISUALIZATIONS 5
|
42
|
|
-
|
43
|
47
|
void (*visualizations[NUMOFVISUALIZATIONS])(uint8_t *data) = { &simpleVisualization,
|
44
|
48
|
&fullDepthVisualization, &horribleWave,
|
45
|
49
|
&simpleLog, &fullDepthLog };
|
|
50
|
+uint8_t logScale[8] = { 2, 4, 8, 16, 31, 63, 125, 250 }; // --> ca. (1 << (led + 1));
|
46
|
51
|
|
47
|
52
|
uint8_t numberOfVisualizations(void) {
|
48
|
53
|
return NUMOFVISUALIZATIONS;
|
49
|
54
|
}
|
50
|
55
|
|
51
|
56
|
void runVisualization(uint8_t *data, uint8_t id) {
|
52
|
|
- if (id < NUMOFVISUALIZATIONS)
|
|
57
|
+ if (id < NUMOFVISUALIZATIONS) {
|
|
58
|
+ filterData(data);
|
53
|
59
|
visualizations[id](data);
|
|
60
|
+ }
|
54
|
61
|
}
|
55
|
62
|
|
56
|
|
-uint8_t logScale[8] = { 2, 4, 8, 16, 31, 63, 125, 250 };
|
57
|
|
-// --> ca. (1 << (led + 1));
|
|
63
|
+uint8_t maxVal(uint8_t data, uint8_t log) {
|
|
64
|
+ uint8_t max = 0;
|
|
65
|
+ if (log) {
|
|
66
|
+ while ((max <= 7) && (data > logScale[max])) // Some bitshifting would do it...
|
|
67
|
+ max++; // But this is more fine grained
|
|
68
|
+ } else {
|
|
69
|
+ max = data / FACTOR;
|
|
70
|
+ }
|
|
71
|
+ return max;
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+uint8_t average(uint8_t *data) {
|
|
75
|
+ uint16_t sum = 0;
|
|
76
|
+ uint8_t i;
|
|
77
|
+ for (i = 0; i < 7; i++) {
|
|
78
|
+ sum += data[i];
|
|
79
|
+ }
|
|
80
|
+ sum /= 7;
|
|
81
|
+ return (uint8_t)sum;
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+void filterData(uint8_t *data) {
|
|
85
|
+ uint8_t i;
|
|
86
|
+ if (average(data) < THRESHOLD) {
|
|
87
|
+ for (i = 0; i < 7; i++) {
|
|
88
|
+ data[i] = 0;
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+}
|
58
|
92
|
|
59
|
93
|
void simpleVUMeter(uint8_t *data, uint8_t *buff, uint8_t z, uint8_t log) {
|
60
|
94
|
uint8_t i, h = 0, max;
|
61
|
95
|
for(i = 0; i < 7; i++) {
|
62
|
|
- if (log) {
|
63
|
|
- while ((h <= 7) && (data[i] > logScale[h])) // Some bitshifting would do it...
|
64
|
|
- h++; // But this is more fine grained
|
65
|
|
- max = h;
|
66
|
|
- } else {
|
67
|
|
- max = data[i] / FACTOR;
|
68
|
|
- }
|
|
96
|
+ max = maxVal(data[i], log);
|
69
|
97
|
for (h = 0; h < max; h++) {
|
70
|
98
|
if (i == 0) {
|
71
|
99
|
buffSetPixel(buff, i, (h * 10 / 15), z);
|
|
@@ -81,6 +109,7 @@ void simpleLog(uint8_t *data) {
|
81
|
109
|
buff = buffNew();
|
82
|
110
|
buffClearAllPixels(buff);
|
83
|
111
|
|
|
112
|
+ setRow(0, 0, maxVal(average(data), 1), buff); // Show average
|
84
|
113
|
simpleVUMeter(data, buff, 7, 1);
|
85
|
114
|
|
86
|
115
|
setImage(buff);
|
|
@@ -93,6 +122,7 @@ void simpleVisualization(uint8_t *data) {
|
93
|
122
|
|
94
|
123
|
buffClearAllPixels(buff);
|
95
|
124
|
|
|
125
|
+ setRow(0, 0, maxVal(average(data), 0), buff); // Show average
|
96
|
126
|
simpleVUMeter(data, buff, 7, 0);
|
97
|
127
|
|
98
|
128
|
setImage(buff);
|
|
@@ -129,14 +159,10 @@ void fullDepthVisualization(uint8_t *data) {
|
129
|
159
|
buffFree(buff);
|
130
|
160
|
}
|
131
|
161
|
|
132
|
|
-void setPixelBuffer(uint8_t x, uint8_t y, uint8_t z, uint8_t *buf) {
|
133
|
|
- buf[(8 * (7 - z)) + (7 - y)] |= (1 << x); // z is inverted for beauty reasons
|
134
|
|
-}
|
135
|
|
-
|
136
|
162
|
void setRow(uint8_t x, uint8_t z, uint8_t height, uint8_t *buf) {
|
137
|
|
- uint8_t i = 0;
|
138
|
|
- for (; i < height; i++) {
|
139
|
|
- setPixelBuffer(x, i, z, buf);
|
|
163
|
+ uint8_t i;
|
|
164
|
+ for (i = 0; i < height; i++) {
|
|
165
|
+ buffSetPixel(buf, x, i, z);
|
140
|
166
|
}
|
141
|
167
|
}
|
142
|
168
|
|