-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathequalizer_in_old_desktop_sourcecode.ino
239 lines (173 loc) · 5.41 KB
/
equalizer_in_old_desktop_sourcecode.ino
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
// 2020 - electronstogo
#include <Adafruit_NeoPixel.h>
// Point structure just describes one point in the LED matrix.
struct Point
{
//char x, y;
char r, g, b;
bool active;
};
// top point structure, that is used to control the falling mode, of the top LEDs in every column.
struct TopPoint
{
int position;
// pushed means that the level has reached a new top level,
// and the top LED will keep the position for at least a moment.
int pushed;
};
// constants which define the LED matrix size.
#define ROWS 10
#define COLUMNS 7
#define NUMPIXELS ROWS * COLUMNS
// pin constants for LED control.
#define LED_DATA_PIN 8
// pin constants for the spectrum analyzer chip msgeq7.
#define STROBE_PIN 10
#define RESET_PIN 3
#define ANALOG_PIN 0
// create a LED matrix of ROWS * COLUMNS points.
// This will be used as buffer, to flush the LEDs.
Point g_led_matrix[ROWS][COLUMNS];
// array of top points, to handle falling mode for top LEDs in the columns.
TopPoint g_array_top[COLUMNS];
// pixel control
Adafruit_NeoPixel g_pixels = Adafruit_NeoPixel(NUMPIXELS, LED_DATA_PIN, NEO_RGB + NEO_KHZ800);
void setup()
{
g_pixels.begin();
g_pixels.show();
pinMode(STROBE_PIN, OUTPUT);
pinMode(RESET_PIN, OUTPUT);
pinMode(ANALOG_PIN, INPUT);
// init msgeq7
digitalWrite(RESET_PIN, LOW);
digitalWrite(STROBE_PIN, LOW);
delay(1);
digitalWrite(RESET_PIN, HIGH);
delay(1);
digitalWrite(RESET_PIN, LOW);
digitalWrite(STROBE_PIN, HIGH);
delay(1);
for(int column = 0; column < COLUMNS; column++)
{
g_array_top[column].position = 0;
g_array_top[column].pushed = 0;
g_led_matrix[column][0].r = 254;
}
}
void loop()
{
// main loop counter, used to control function calls, for the top LED sinking mode.
static unsigned int s_loop_counter = 0;
clear_led_matrix();
digitalWrite(RESET_PIN, HIGH);
delay(5);
digitalWrite(RESET_PIN, LOW);
// Spectrum levels
int spectrum_value[7];
// Get frequency levels.
for(int channel = 0; channel < 7; channel++)
{
digitalWrite(STROBE_PIN, LOW);
delay(10);
spectrum_value[channel] = analogRead(ANALOG_PIN);
// Remove noise.
spectrum_value[channel] = spectrum_value[channel] < 100 ? 0 : spectrum_value[channel];
digitalWrite(STROBE_PIN, HIGH);
// Map frequency value to LED height.
spectrum_value[channel] = constrain(spectrum_value[channel], 0, 1023);
spectrum_value[channel] = map(spectrum_value[channel], 0, 1023, 0, ROWS - 1);
spectrum_value[channel] = spectrum_value[channel] > 9 ? 9 : spectrum_value[channel] < 0 ? 0 : spectrum_value[channel];
}
// Handle all LED columns.
for(int column = 0; column < COLUMNS; column++)
{
// Check all LEDs in the current column, until the spectrum value top point is reached.
for(int row = 0; row < spectrum_value[column]; row++)
{
g_led_matrix[row][COLUMNS - 1 - column].active = true;
g_led_matrix[row][COLUMNS - 1 - column].b = 254;
g_led_matrix[row][COLUMNS - 1 - column].g = 0;
g_led_matrix[row][COLUMNS - 1 - column].r = 0;
}
// Check if the column got a new top level LED.
if(spectrum_value[column] >= g_array_top[column].position)
{
g_array_top[column].position = spectrum_value[column];
g_array_top[column].pushed = 6;
}
g_led_matrix[g_array_top[column].position][COLUMNS - 1 - column].active = true;
g_led_matrix[g_array_top[column].position][COLUMNS - 1 - column].r = 254;
g_led_matrix[g_array_top[column].position][COLUMNS - 1 - column].g = 0;
g_led_matrix[g_array_top[column].position][COLUMNS - 1 - column].b = 0;
}
flush_led_matrix();
if(s_loop_counter % 2)
{
top_sinking();
}
s_loop_counter++;
}
// Handle the sinking mode for the top LEDs in every column.
void top_sinking()
{
for(int column = 0; column < COLUMNS; column++)
{
if(g_array_top[column].position > 0 && g_array_top[column].pushed <= 0)
{
g_array_top[column].pushed = 0;
g_array_top[column].position--;
}
else if(g_array_top[column].pushed > 0)
{
g_array_top[column].pushed--;
}
}
}
// just clear the whole matrix buffer for the LEDs.
void clear_led_matrix()
{
for(int row = 0; row < ROWS; row++)
{
for(int column = 0; column < COLUMNS; column++)
{
g_led_matrix[row][column].active = false;
}
}
}
// Update the LED matrix, with the current buffer data.
// Following describes the pattern, for LED control directions.
// xxxxxxxxxxxxxx
// <--
// xxxxxxxxxxxxxx
// -->
// xxxxxxxxxxxxxx
// <--
// xxxxxxxxxxxxxx
// -->
// xxxxxxxxxxxxxx
// <--
void flush_led_matrix()
{
// Handle all columns.
for(int column = 0; column < COLUMNS; column++)
{
for(int row = 0; row < ROWS; row++)
{
// Data direction of columns variies because of the hardware wiring. Choose direction.
int corrected_row = row;
corrected_row = columns % 2 ? ROWS - 1 - row : corrected_row;
// sendo color config to active LEDs
if(g_led_matrix[corrected_row][column].active)
{
g_pixels.setPixelColor(column * ROWS + row, g_pixels.Color(g_led_matrix[corrected_row][column].r,
g_led_matrix[corrected_row][column].g, g_led_matrix[corrected_row][column].b));
}
else
{
g_pixels.setPixelColor(column * ROWS + row, 0, 0, 0);
}
}
}
g_pixels.show();
}