/
MSGEQ7.cpp
355 lines (297 loc) · 6.5 KB
/
MSGEQ7.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
/*
MSGEQ7.cpp - MSGEQ7 Graphic Equalizer library for Arduino
Version: 0.1
Copyright (c) 2013 Joseph Diaz. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define NOP __asm__ __volatile__ ("nop\n\t")
#include "MSGEQ7.h"
#include "Arduino.h"
// NOP is 62.5ns @ 16MHz
//TODO Timings for multiple processor speeds
//TODO Change offset detect to a minimum detect?
//Constructor
MSGEQ7::MSGEQ7(int reset, int strobe, int output){
_reset=reset; //Reset Digital Output
_strobe=strobe; //Strobe Digital Output
_output=output; //Analog "Output" Input
_correctOffset=false; //Do not adjust offset by default
//Initialize Measurement Variables
for(int i=0;i<7;i++){
_values[i]=0;
}
pinMode(reset, OUTPUT);
pinMode(strobe, OUTPUT);
pinMode(output, INPUT);
}
//Constructor with offset Correct
MSGEQ7::MSGEQ7(int reset, int strobe, int output, bool correctOffset){
_reset=reset; //Reset Digital Output
_strobe=strobe; //Strobe Digital Output
_output=output; //Analog "Output" Input
_correctOffset=correctOffset; //Offset adjustment Flag
//Initialize Measurement Variables
for(int i=0;i<7;i++){
_values[i]=0;
}
pinMode(reset, OUTPUT);
pinMode(strobe, OUTPUT);
pinMode(output, INPUT);
}
//Send a Reset Pulse to the MSGEQ7
void MSGEQ7::reset(){
digitalWrite(_reset, HIGH);
//100nS minimum reset pulse
NOP;
NOP;
digitalWrite(_reset, LOW);
}
//Send a Strobe Pulse to the MSGEQ7
void MSGEQ7::strobe(){
//Strobe to Strobe and Reset to Strobe delay 72uS Min
//76.8uS @ 16MHz
for(int i=0;i<165;i++){
NOP;
}
digitalWrite(_strobe, HIGH);
//Strobe Pulse 18uS Min.
//21.6uS @ 16MHz
for(int i=0;i<40;i++){
NOP;
}
digitalWrite(_strobe, LOW);
}
//Detect the Average Output DC Offset.
int MSGEQ7::detectOffset(){
//Must be run with no Signal Input
//Reset Offset Variable
_offset = read63();
//Cycle through all input signals.
for(int i=0;i<5;i++){
readAll();
for(int j=0;j<7;j++){
_offset = (_offset+_values[j])/2;
}
}
return _offset;
}
//Set the Offset Value
void MSGEQ7::setOffset(int offset){
_offset=offset;
}
//Get the Offset Value
int MSGEQ7::getOffset(){
return _offset;
}
//Set offset adjusmtent flag
void MSGEQ7::correctOffset(bool value){
_correctOffset=value;
}
//Read the Active Frequency - May return negative values if offset correction is used.
int MSGEQ7::read(){
//Output Settling Time 36uS Min
//40.8uS @ 16MHz
for(int i=0;i<80;i++){
NOP;
}
if(_correctOffset)
return analogRead(_output)-_offset;
return analogRead(_output);
}
//Updates all frequency values.
void MSGEQ7::readAll(){
reset();
for(int i=0;i<7;i++){
strobe();
_values[i]=read();
}
}
//Read Frequency 1 (63Hz)
int MSGEQ7::read63(){
reset();
strobe();
_values[0] = read();
if(_correctOffset)
if((_values[0]-_offset)>0)
return _values[0]-_offset;
else
return 0;
return _values[0];
}
//Read Frequency 2 (160Hz)
int MSGEQ7::read160(){
reset();
strobe();
strobe();
_values[1] = read();
if(_correctOffset)
if((_values[1]-_offset)>0)
return _values[1]-_offset;
else
return 0;
return _values[1];
}
//Read Frequency 3 (400Hz)
int MSGEQ7::read400(){
reset();
strobe();
strobe();
strobe();
_values[2] = read();
if(_correctOffset)
if((_values[2]-_offset)>0)
return _values[2]-_offset;
else
return 0;
return _values[2];
}
//Read Frequency 4 (1kHz)
int MSGEQ7::read1000(){
reset();
strobe();
strobe();
strobe();
strobe();
_values[3] = read();
if(_correctOffset)
if((_values[3]-_offset)>0)
return _values[3]-_offset;
else
return 0;
return _values[3];
}
//Read Frequency 5 (2.5kHz)
int MSGEQ7::read2500(){
reset();
strobe();
strobe();
strobe();
strobe();
strobe();
_values[4] = read();
if(_correctOffset)
if((_values[4]-_offset)>0)
return _values[4]-_offset;
else
return 0;
return _values[4];
}
//Read Frequency 6 (6.25kHz)
int MSGEQ7::read6250(){
reset();
strobe();
strobe();
strobe();
strobe();
strobe();
strobe();
_values[5] = read();
if(_correctOffset)
if((_values[5]-_offset)>0)
return _values[5]-_offset;
else
return 0;
return _values[5];
}
//Read Frequency 7 (16kHz)
int MSGEQ7::read16000(){
reset();
strobe();
strobe();
strobe();
strobe();
strobe();
strobe();
strobe();
_values[6] = read();
if(_correctOffset)
if((_values[6]-_offset)>0)
return _values[6]-_offset;
else
return 0;
return _values[6];
}
//Return Frequency at specified index
int MSGEQ7::getValue(int index){
if(index>6 || index<0)
return 0;
if(_correctOffset)
if((_values[index]-_offset)>0)
return _values[index]-_offset;
else
return 0;
return _values[index];
}
//Return Frequency 1 stored value.
int MSGEQ7::get63(){
if(_correctOffset)
if((_values[0]-_offset)>0)
return _values[0]-_offset;
else
return 0;
return _values[0];
}
//Return Frequency 2 stored value.
int MSGEQ7::get160(){
if(_correctOffset)
if((_values[1]-_offset)>0)
return _values[1]-_offset;
else
return 0;
return _values[1];
}
//Return Frequency 3 stored value.
int MSGEQ7::get400(){
if(_correctOffset)
if((_values[2]-_offset)>0)
return _values[2]-_offset;
else
return 0;
return _values[2];
}
//Return Frequency 4 stored value.
int MSGEQ7::get1000(){
if(_correctOffset)
if((_values[3]-_offset)>0)
return _values[3]-_offset;
else
return 0;
return _values[3];
}
//Return Frequency 5 stored value.
int MSGEQ7::get2500(){
if(_correctOffset)
if((_values[4]-_offset)>0)
return _values[4]-_offset;
else
return 0;
return _values[4];
}
//Return Frequency 6 stored value.
int MSGEQ7::get6250(){
if(_correctOffset)
if((_values[5]-_offset)>0)
return _values[5]-_offset;
else
return 0;
return _values[5];
}
//Return Frequency 7 stored value.
int MSGEQ7::get16000(){
if(_correctOffset)
if((_values[6]-_offset)>0)
return _values[6]-_offset;
else
return 0;
return _values[6];
}