-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonometer_calibration.js
146 lines (134 loc) · 4.39 KB
/
sonometer_calibration.js
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
//load with this firmware
//https://github.com/nicolas-f/Espruino/actions/runs/7608361443/artifacts/1184976653
var PIN_BUZZER = D5; // Yellow cable pin Buzzer is connected to
var PIN_PDM_CLOCK = 8;
var PIN_PDM_DIN = 9;
var button_watch = [0,0,0,0];
Bluetooth.setConsole(1);
backlight = 0;
Pixl.setLCDPower(false);
var idRefreshInterval = 0;
var gainCalibration=117.5;
var bufA = new Int16Array(2500);
var bufB = new Int16Array(2500);
var filter_num = new Float32Array([0.475780,-0.951561,-0.475780,1.903122,-0.475780,-0.951561,0.475780]);
var filter_den = new Float32Array([1.000000,-3.118098,2.994414,-0.331733,-0.772673,0.153549,0.074541]);
var filter_buffer = new Float32Array(filter_num.length+filter_den.length);
var gainChanged = false;
var buzzerEnabled = false;
var micEnabled = false;
var screenEnabled = false;
var sample_rate = 20000;
var spl= new Float32Array(1);
function readGainCalibration() {
fp = require("Storage").open("gaincalib.cfg", 'r');
data=fp.readLine();
if(data) {
gainCalibration = parseFloat(data);
}
}
function writeGainCalibration() {
fp = require("Storage").open("gaincalib.cfg", 'w');
fp.write(gainCalibration.toFixed(1)+"\n");
}
readGainCalibration();
var mainmenu = {
"" : {
"title" : "-- Settings --"
},
"Backlight On" : function() { LED1.set(); },
"Backlight Off" : function() { LED1.reset(); },
"Calibration" : {
value : gainCalibration,
min:-50,max:50,step:0.1,
onchange : v => { gainCalibration=v;gainChanged=true; }
},
"Exit" : function() { if(gainChanged) {writeGainCalibration();gainChanged=false;} homeScreen(); },
};
function onSamples(samples, sumSquared) {
spl[0]=sumSquared;
}
function disableButtons() {
for(id=0;id<4;id++) {
if(button_watch[id] > 0) {
clearWatch(button_watch[id]);
}
button_watch[id] = 0;
}
}
function switchBuzzerState() {
buzzerEnabled = !buzzerEnabled;
if(buzzerEnabled) {
analogWrite(PIN_BUZZER,0.5,{freq:4000});
} else {
digitalWrite(PIN_BUZZER,0);
}
homeScreen();
}
function switchMicrophoneState() {
micEnabled=!micEnabled;
if(micEnabled) {
Pdm.setup({"clock" : PIN_PDM_CLOCK, "din" : PIN_PDM_DIN, frequency: sample_rate});
Pdm.init(onSamples, bufA, bufB);
Pdm.filter_init(filter_num,filter_den,filter_buffer);
Pdm.start();
if(idRefreshInterval > 0) {
clearInterval(idRefreshInterval);
idRefreshInterval = 0;
}
idRefreshInterval = setInterval(homeScreen, 250);
} else {
Pdm.stop();
Pdm.uninit();
if(idRefreshInterval > 0) {
clearInterval(idRefreshInterval);
idRefreshInterval = 0;
}
}
homeScreen();
}
function switchScreen() {
screenEnabled=!screenEnabled;
if(screenEnabled) {
LED1.set();
homeScreen();
} else {
LED1.reset();
Pixl.setLCDPower(false);
if(idRefreshInterval > 0) {
clearInterval(idRefreshInterval);
idRefreshInterval = 0;
}
if(micEnabled) {
switchMicrophoneState();
}
}
}
function homeScreen() {
Pixl.setLCDPower(true);
g.clear();
g.setFontBitmap();
var t = new Date(); // get the current date and time
var time_date = t.getFullYear()+"/"+("0"+(t.getMonth()+1)).substr(-2)+"/"+("0"+t.getDate()).substr(-2)+" "+t.getHours()+":"+("0"+t.getMinutes()).substr(-2);
g.setFontAlign(1, -1).drawString((micEnabled ? "Stop" : "Start")+" Mic-", g.getWidth(), 2);
g.setFontAlign(1, 1).drawString((buzzerEnabled ? "Stop" : "Start")+" Buzzer-", g.getWidth(), g.getHeight());
g.setFontAlign(-1, 1).drawString("-On/Off", 0, g.getHeight());
g.setFontAlign(-1, -1).drawString("-Settings", 0, 0);
var level = "-";
if(micEnabled) {
level = 10*Math.log(spl[0]/bufA.length/1073741824)/2.302585092994046;
if(gainCalibration > -0.09 && gainCalibration < 0.08) {
level = spl[0].toFixed(1) + " dBFS";
} else {
level = (level + gainCalibration).toFixed(1) + " dBA";
}
}
g.setFont("Vector16").setFontAlign(0, 0).drawString(level, g.getWidth()/2, g.getHeight()/2);
g.flip();
disableButtons();
button_watch[0] = setWatch(function() { disableButtons(); Pixl.menu(mainmenu); }, BTN1, { repeat: true, edge: 'rising'});
button_watch[1] = setWatch(switchMicrophoneState, BTN2, { repeat: true, edge: 'rising'});
button_watch[2] = setWatch(switchBuzzerState, BTN3, { repeat: true, edge: 'rising'});
button_watch[3] = setWatch(switchScreen, BTN4, { repeat: true, edge: 'rising'});
}
switchScreen();