-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM5Fire_PDM.ino
358 lines (296 loc) · 12.3 KB
/
M5Fire_PDM.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
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
/*
Example animated analogue meters using a ILI9341 TFT LCD screen
Needs Font 2 (also Font 4 if using large scale label)
Make sure all the display driver and pin comnenctions are correct by
editting the User_Setup.h file in the TFT_eSPI library folder.
#########################################################################
###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
#########################################################################
*/
#include <M5Stack.h>
#include "driver/i2s.h"
#define PIN_CLK 22
#define PIN_DATA 21
const i2s_port_t I2S_PORT = I2S_NUM_0;
const int BLOCK_SIZE = 2000;
int16_t samples[BLOCK_SIZE];
#define SAMPLE_RATE 16000
long total_read = 0;
#define MICROPHONE_SENSITIVITY -26 // -26 dB(FS) @ 1khz 94 dB
#include <limits.h>
#define TFT_GREY 0x5AEB
#define LOOP_PERIOD 125 // Display updates every x ms
#define MIN_VALUE 30
#define MAX_VALUE 110
#define DEGTORAD 0.0174532925 // degree to radian
float ltx = 0; // Saved x coord of bottom of needle
uint16_t osx = 120, osy = 120; // Saved x & y coords
uint32_t updateTime = 0; // time for next update
setup_t tft_settings;
int old_analog = -999; // Value last displayed
int d = 0;
int display_rms = 0;
double display_dbspl = 0.0f;
//High pass butterworth filter order=2 alpha1=0.00625
class Filter
{
public:
Filter()
{
v[0]=0.0;
v[1]=0.0;
}
private:
double v[3];
public:
double step(double x) //class II
{
v[0] = v[1];
v[1] = v[2];
v[2] = (9.726138984998438097e-1 * x)
+ (-0.94597793623228154658 * v[0])
+ (1.94447765776709369234 * v[1]);
return
(v[0] + v[2])
- 2 * v[1];
}
};
Filter buttHighPass;
void init_pdm() {
esp_err_t err;
i2s_config_t audio_in_i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // although the SEL config should be left, it seems to transmit on right
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
.dma_buf_count = 2,
.dma_buf_len = 128
};
// This function must be called before any I2S driver read/write operations.
err = i2s_driver_install(I2S_PORT, &audio_in_i2s_config, 0, NULL);
if (err != ESP_OK) {
Serial.printf("Failed installing driver: %d\n", err);
while (true);
}
// configure which pin on the ESP32 is connected to which pin on the mic:
i2s_pin_config_t audio_in_pin_config = {
.bck_io_num = I2S_PIN_NO_CHANGE, // not used
.ws_io_num = PIN_CLK, // IO 15 clock pin
.data_out_num = I2S_PIN_NO_CHANGE, // Not used
.data_in_num = PIN_DATA // data pin
};
err = i2s_set_pin(I2S_PORT, &audio_in_pin_config);
if (err != ESP_OK) {
Serial.printf("Failed setting pin: %d\n", err);
while (true);
}
}
void print_number(double val, int precision, int x, int y) {
char buf[16];
dtostrf(val, 4, precision, buf);
M5.Lcd.setTextFont(2);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString(buf, x, y);
}
void process_samples(void *pvParameters) {
while(1){
size_t num_bytes_read;
i2s_read(I2S_PORT,
(char *)samples,
BLOCK_SIZE,
&num_bytes_read,
portMAX_DELAY); // no timeout
if (num_bytes_read > 0) {
int samples_read = num_bytes_read / 2;
total_read += samples_read;
float sample;
double rms = 0;
for(int i=0; i < samples_read; i++) {
double v = buttHighPass.step((double)samples[i]);
rms += v * v;
//Serial.println(v);
}
rms = sqrt(rms / samples_read);
display_rms = rms;
double dbfs = 20 * log10(rms / SHRT_MAX);
display_dbspl = dbfs + (94 - MICROPHONE_SENSITIVITY);
}
}
}
void setup(void) {
M5.begin();
M5.Power.begin();
// Initialize the I2S peripheral
init_pdm();
// Create a task that will read the data
xTaskCreatePinnedToCore(process_samples, "PDM_reader", 2048, NULL, 1, NULL, 1);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.getSetup(tft_settings);
analogMeter(tft_settings.tft_height, tft_settings.tft_width, 0, 0); // Draw analogue meter
updateTime = millis(); // Next update time
}
void loop() {
if (updateTime <= millis()) {
updateTime = millis() + LOOP_PERIOD;
plotNeedle(tft_settings.tft_height, tft_settings.tft_width, display_dbspl, 0);
}
}
// #########################################################################
// Draw the analogue meter on the screen
// #########################################################################
void analogMeter(int width, int height, int pos_x, int pos_y) {
// Meter outline
M5.Lcd.fillRect(pos_x, pos_y, width, height, TFT_GREY);
int border = 3;
int ticks_width = (width - border * 2) * 0.4;
int ticks_height = (height - border * 2) / 2;
int position_midle_analog = width * 0.5;
int yposition_midle_analog = height * 0.8;
int long_scale_length = 30;
int short_scale_length = 16;
M5.Lcd.fillRect(pos_x + border, pos_y + border, width - border * 2, height - border * 2, TFT_WHITE);
M5.Lcd.setTextColor(TFT_BLACK); // Text colour
// Draw ticks every 5 degrees from -50 to +50 degrees (100 deg. FSD swing)
for (int i = -50; i <= 50; i += 5) {
// Long scale tick length
int tl = long_scale_length;
// Coodinates of tick to draw
float sx = cos((i - 90) * DEGTORAD);
float sy = sin((i - 90) * DEGTORAD);
uint16_t x0 = sx * (ticks_width + tl) + position_midle_analog;
uint16_t y0 = sy * (ticks_height + tl) + yposition_midle_analog;
uint16_t x1 = sx * ticks_width + position_midle_analog;
uint16_t y1 = sy * ticks_height + yposition_midle_analog;
// Coordinates of next tick for zone fill
float sx2 = cos((i + 5 - 90) * DEGTORAD);
float sy2 = sin((i + 5 - 90) * DEGTORAD);
int x2 = sx2 * (ticks_width + tl) + position_midle_analog;
int y2 = sy2 * (ticks_height + tl) + yposition_midle_analog;
int x3 = sx2 * ticks_width + position_midle_analog;
int y3 = sy2 * ticks_height + yposition_midle_analog;
// Green zone limits
if (i >= 0 && i < 25) {
M5.Lcd.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_GREEN);
M5.Lcd.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_GREEN);
}
// Orange zone limits
if (i >= 25 && i < 50) {
M5.Lcd.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_ORANGE);
M5.Lcd.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_ORANGE);
}
// Short scale tick length
if (i % 25 != 0) tl = short_scale_length;
// Recalculate coords incase tick lenght changed
x0 = sx * (ticks_width + tl) + position_midle_analog;
y0 = sy * (ticks_height + tl) + yposition_midle_analog;
x1 = sx * ticks_width + position_midle_analog;
y1 = sy * ticks_height + yposition_midle_analog;
// Draw tick
M5.Lcd.drawLine(x0, y0, x1, y1, TFT_BLACK);
// Check if labels should be drawn, with position tweaks
if (i % 25 == 0) {
// Calculate label positions
x0 = sx * (ticks_width + tl + 10) + position_midle_analog;
y0 = sy * (ticks_height + tl + 10) + yposition_midle_analog;
switch (i / 25) {
case -2:
M5.Lcd.drawCentreString("30", x0, y0 - 12, 2);
break;
case -1:
M5.Lcd.drawCentreString("50", x0, y0 - 9, 2);
break;
case 0:
M5.Lcd.drawCentreString("70", x0, y0 - 6, 2);
break;
case 1:
M5.Lcd.drawCentreString("90", x0, y0 - 9, 2);
break;
case 2:
M5.Lcd.drawCentreString("110", x0, y0 - 12, 2);
break;
}
}
// Now draw the arc of the scale
sx = cos((i + 5 - 90) * DEGTORAD);
sy = sin((i + 5 - 90) * DEGTORAD);
x0 = sx * ticks_width + position_midle_analog;
y0 = sy * ticks_height + yposition_midle_analog;
// Draw scale arc, don't draw the last part
if (i < 50) M5.Lcd.drawLine(x0, y0, x1, y1, TFT_BLACK);
}
M5.Lcd.setTextFont(2);
M5.Lcd.setTextDatum(BR_DATUM);
M5.Lcd.drawString("dB(A)", width - border, height / 2);
M5.Lcd.setTextFont(4);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString("dB(A)", width / 2, height / 2);
M5.Lcd.drawRect(pos_x + border, pos_y + border, width - border * 2, height - border * 2, TFT_BLACK); // Draw bezel line
plotNeedle(tft_settings.tft_height, tft_settings.tft_width, 0, 0); // Put meter needle at 0
}
// #########################################################################
// Update needle position
// This function is blocking while needle moves, time depends on ms_delay
// 10ms minimises needle flicker if text is drawn within needle sweep area
// Smaller values OK if text not in sweep area, zero for instant movement but
// does not look realistic... (note: 100 increments for full scale deflection)
// #########################################################################
void plotNeedle(int width, int height, int value, byte ms_delay) {
int border = 3;
int ticks_width = (width - border * 2) * 0.4 - 2;
int ticks_height = (height - border * 2) / 2 - 2;
int position_midle_analog = width * 0.5;
int yposition_midle_analog = height * 0.8;
int long_scale_length = 30;
int short_scale_length = 16;
M5.Lcd.setTextColor(TFT_BLACK, TFT_WHITE);
char buf[40];
sprintf(buf, "RMS: %d ", display_rms);
M5.Lcd.setTextFont(2);
M5.Lcd.setTextDatum(BL_DATUM);
M5.Lcd.drawString(buf, border * 2, height * 0.6);
sprintf(buf, "SPL: %.1f dB", display_dbspl);
M5.Lcd.drawString(buf, border * 2, height * 0.6 + 20);
if (value < MIN_VALUE - 10) value = MIN_VALUE - 10; // Limit value to emulate needle end stops
if (value > MAX_VALUE + 10) value = MAX_VALUE + 10;
// Move the needle util new value reached
while (!(value == old_analog)) {
if (old_analog < value)
old_analog++;
else
old_analog--;
if (ms_delay == 0)
old_analog = value; // Update immediately id delay is 0
float sdeg =
map(old_analog, MIN_VALUE - 10, MAX_VALUE + 10, -150, -30); // Map value to angle
// Calcualte tip of needle coords
float sx = cos(sdeg * DEGTORAD);
float sy = sin(sdeg * DEGTORAD);
// Calculate x delta of needle start (does not start at pivot point)
float tx = tan((sdeg + 90) * DEGTORAD);
// Erase old needle image
M5.Lcd.drawLine(position_midle_analog - 1, yposition_midle_analog, osx - 1, osy, TFT_WHITE);
M5.Lcd.drawLine(position_midle_analog, yposition_midle_analog, osx, osy, TFT_WHITE);
M5.Lcd.drawLine(position_midle_analog + 1, yposition_midle_analog, osx + 1, osy, TFT_WHITE);
// Re-plot text under needle
M5.Lcd.setTextColor(TFT_BLACK);
M5.Lcd.setTextFont(4);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString("dB(A)", width / 2, height / 2);
// Store new needle end coords for next erase
ltx = tx;
osx = sx * ticks_width + position_midle_analog;
osy = sy * ticks_height + yposition_midle_analog;
// Draw the needle in the new postion, magenta makes needle a bit bolder
// draws 3 lines to thicken needle
//M5.Lcd.drawLine(120 + 20 * ltx - 1, 140 - 20, osx - 1, osy, TFT_RED);
M5.Lcd.drawLine(position_midle_analog - 1, yposition_midle_analog, osx - 1, osy, TFT_RED);
M5.Lcd.drawLine(position_midle_analog, yposition_midle_analog, osx, osy, TFT_MAGENTA);
M5.Lcd.drawLine(position_midle_analog + 1, yposition_midle_analog, osx + 1, osy, TFT_RED);
// Slow needle down slightly as it approaches new postion
if (abs(old_analog - value) < 10) ms_delay += ms_delay / 5;
// Wait before next update
delay(ms_delay);
}
}