-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBioSignal-Recorder.ino
301 lines (255 loc) · 9.13 KB
/
BioSignal-Recorder.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
// EXG-Visualizer
// https://github.com/upsidedownlabs/BioSignal-Recorder
// Copyright (c) 2023 Mahesh Tupe [email protected]
// Copyright (c) 2021 Moteen Shah [email protected]
// Upside Down Labs invests time and resources providing this open source code,
// please support Upside Down Labs and open-source hardware by purchasing
// products from Upside Down Labs!
// Copyright (c) 2023 Upside Down Labs - [email protected]
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <WebSocketsServer.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
#include <driver/adc.h>
#include <math.h>
// Add SSID and PASSWORD for network you are connected to
const char *SSID = "";
const char *PASSWORD = "";
// Create asyncwebserver object on port 80
AsyncWebServer server(80);
// Create websocketserver object on port 81
// Websocket is used to send actual analog data
WebSocketsServer webSocket = WebSocketsServer(81);
// Initialize timer interrupts
hw_timer_t * timer_1 = NULL;
hw_timer_t * timer_2 = NULL;
hw_timer_t * timer_3 = NULL;
hw_timer_t * timer_4 = NULL;
portMUX_TYPE timerMux_1 = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE timerMux_2 = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE timerMux_3 = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE timerMux_4 = portMUX_INITIALIZER_UNLOCKED;
// Counter for counting number of times each timer gets triggered
volatile int interruptCounter[4] = {0};
// counter for each timer
void IRAM_ATTR onTimer_1() {
portENTER_CRITICAL_ISR(&timerMux_1);
interruptCounter[0]++;
portEXIT_CRITICAL_ISR(&timerMux_1);
}
void IRAM_ATTR onTimer_2() {
portENTER_CRITICAL_ISR(&timerMux_2);
interruptCounter[1]++;
portEXIT_CRITICAL_ISR(&timerMux_2);
}
void IRAM_ATTR onTimer_3() {
portENTER_CRITICAL_ISR(&timerMux_3);
interruptCounter[2]++;
portEXIT_CRITICAL_ISR(&timerMux_3);
}
void IRAM_ATTR onTimer_4() {
portENTER_CRITICAL_ISR(&timerMux_4);
interruptCounter[3]++;
portEXIT_CRITICAL_ISR(&timerMux_4);
}
void setup()
{
Serial.begin(115200);
// SPIFFS is used for storing HTML, CSS and JS code on esp32
if (!SPIFFS.begin()) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Connect to WiFi
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Local IP is where our webserver will be hosted
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Send HTML file when server requests it
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
{
request->send(SPIFFS, "/index.html");
});
// start server
server.begin();
// Start websocket connection
webSocket.begin();
// Whenever a message is recieved on websocket, callback function is called
webSocket.onEvent(callback);
}
bool sample = false; // Boolean value to tell ESP32 when to start or stop sampling
int sampling_rate = 0; // For storing sampling rate of given channel
int adc[4]; //
int channel_count = 0; // For storing channel number of channel in use
int total_channel = 0; // For storing total number of channel
void callback(byte num, WStype_t type, uint8_t * payload, size_t length)
{
// Switch case based on type of message recieved
switch (type)
{
case WStype_DISCONNECTED:
Serial.println("Client Disconnected");
sample = false;
channel_count = 0;
total_channel = 0;
break;
case WStype_CONNECTED:
Serial.println("Client connected");
sample = true;
break;
case WStype_TEXT:
String rate;
String gpio;
// Last character of msg recived is GPIO number
gpio += (char)payload[length - 1];
gpio += '\n';
if (gpio.toInt() == 9)
{
String temp;
temp += (char)payload[0];
total_channel = temp.toInt();
Serial.print("Total Channels: ");
Serial.println(temp);
}
else {
adc[channel_count] = gpio.toInt();
Serial.print("Channel: ");
Serial.println(gpio);
// Remaining characters form sampling rate
for (int i = 0; i < length - 1; i++)
{
rate += (char)payload[i];
}
rate += '\n';
sampling_rate = rate.toInt();
Serial.print("Sampling rate: ");
Serial.println(rate);
send_samples(sampling_rate, adc[channel_count], channel_count);
channel_count++;
}
}
}
// We will use 2D array for storing data of all 4 channels
// buffer_add has 4 sub arrays
uint16_t **buffer_add = (uint16_t **)calloc(4, sizeof(uint16_t *));
void send_samples(int sampling_rate, int adc, int channel_count)
{
// Fix tick count for timer according to sampling rate for given channel
int tick_count = 1000000 / sampling_rate;
// Start timer for corresponding channel
// Allocate space for data worth 1 Frame (for 0.33 seconds as chart updates at 30FPS)
// Extra 2 blocks for storing channel number and packet number
switch (channel_count)
{
case 0:
timer_1 = timerBegin(0, 80, true);
timerAttachInterrupt(timer_1, &onTimer_1, true);
timerAlarmWrite(timer_1, tick_count, true);
timerAlarmEnable(timer_1);
buffer_add[0] = (uint16_t*)calloc(round((float)sampling_rate / 30.0) + 2, sizeof(uint16_t));
break;
case 1:
timer_2 = timerBegin(1, 80, true);
timerAttachInterrupt(timer_2, &onTimer_2, true);
timerAlarmWrite(timer_2, tick_count, true);
timerAlarmEnable(timer_2);
buffer_add[1] = (uint16_t*)calloc(round((float)sampling_rate / 30.0) + 2, sizeof(uint16_t));
break;
case 2:
timer_3 = timerBegin(2, 80, true);
timerAttachInterrupt(timer_3, &onTimer_3, true);
timerAlarmWrite(timer_3, tick_count, true);
timerAlarmEnable(timer_3);
buffer_add[2] = (uint16_t*)calloc(round((float)sampling_rate / 30.0) + 2, sizeof(uint16_t));
break;
case 3:
timer_4 = timerBegin(3, 80, true);
timerAttachInterrupt(timer_4, &onTimer_4, true);
timerAlarmWrite(timer_4, tick_count, true);
timerAlarmEnable(timer_4);
buffer_add[3] = (uint16_t*)calloc(round((float)sampling_rate / 30.0) + 2, sizeof(uint16_t));
break;
}
// Configure ADC of ESP32 for 12 bit resolution and highest attenuation
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten((adc1_channel_t)(adc), ADC_ATTEN_DB_11);
}
static long packet_counter = 0; // Stores current packet number
static long buffer_counter[4] = {0}; // Stores size of packet
portMUX_TYPE * timer_mux = NULL;
void loop() {
webSocket.loop();
// Loop through selected channels and select corresponding timerMux
for (int i = 0; i < total_channel; i++)
{
switch (i)
{
case 0:
timer_mux = &timerMux_1;
break;
case 1:
timer_mux = &timerMux_2;
break;
case 2:
timer_mux = &timerMux_3;
break;
case 3:
timer_mux = &timerMux_4;
break;
}
// If corresponding timer has triggered sample once and decrement interrupt counter
if (interruptCounter[i] > 0)
{
portENTER_CRITICAL(timer_mux);
interruptCounter[i]--;
portEXIT_CRITICAL(timer_mux);
// Store samples in buffer until enough samples are taken for one frame
if (buffer_counter[i] < round((float)sampling_rate / 30.0))
{
buffer_add[i][buffer_counter[i]] = adc1_get_raw((adc1_channel_t)adc[i]) & 0x0FFF;
buffer_counter[i]++;
}
// If packet for a frame is ready increment packet number and transmit packet
else
{
if (packet_counter < 100)
{
packet_counter++;
}
else
{
packet_counter = 0;
}
// Second last index contains packet number
buffer_add[i][buffer_counter[i]] = packet_counter & 0x0FFF;
buffer_counter[i]++;
// Last index contains channel number
buffer_add[i][buffer_counter[i]] = i & 0x0FFF;
buffer_counter[i]++;
// Transmit data packet
webSocket.sendBIN(0, (uint8_t *)&buffer_add[i][0], buffer_counter[i]*sizeof(uint16_t));
buffer_counter[i] = 0; // empty buffer
}
}
}
}