-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombined-code.ino
382 lines (323 loc) · 10 KB
/
combined-code.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <Adafruit_LSM6DSOX.h>
#include <bluefruit.h>
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
#include <RV3028C7.h>
// oximeter setup
#define REPORTING_PERIOD_MS 1000
PulseOximeter pox;
uint32_t tsLastReport = 0;
// accelerometer setup
Adafruit_LSM6DSOX lsm;
// BLE setup
BLEDfu bledfu; // OTA DFU service
BLEDis bledis; // device information
BLEUart bleuart; // uart over ble
BLEBas blebas; // battery
RV3028C7 rtc;
// display variables
#define LSM_CS 12
#define LSM_SCK 13
#define LSM_MISO 12
#define LSM_MOSI 11
#if defined(ARDUINO_FEATHER_ESP32) // Feather Huzzah32
#define TFT_CS 14
#define TFT_RST 15
#define TFT_DC 32
#elif defined(ESP8266)
#define TFT_CS 4
#define TFT_RST 16
#define TFT_DC 5
#else
// For the breakout board, you can use any 2 or 3 pins.
// These pins will also work for the 1.8" TFT shield.
#define TFT_CS 10
#define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 7
#endif
#define backlight_pin 12
#define button_pin 13
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
float p = 3.1415926;
int scroll = 0;
int timeout = 0;
String data;
bool backlightOff = false;
bool beatDetected = false;
unsigned long timePressed;
bool prevPressed = false;
int detection = 0;
int heartRate = 0;
int SpO2;
String heartRateString;
String SpO2String;
void setup() {
Serial.begin(9600);
Wire.begin();
tft.init(240,240);
tft.fillScreen(ST77XX_BLACK);
while (rtc.begin() == false){
Serial.println("Failed to detect RV-3028-C7!");
}
if (!pox.begin()){
Serial.println("Oximeter failed");
for(;;);
} else {
Serial.println("Oximeter success");
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
pox.setOnBeatDetectedCallback(onBeatDetected);
pox.shutdown();
delay(500);
pox.resume();
if (!lsm.begin_I2C()) {
Serial.println("Failed to find LSM6DSOX chip");
} else {
Serial.println("LSM6DSOX found!");
}
lsm.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
lsm.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS);
lsm.setAccelDataRate(LSM6DS_RATE_26_HZ);
lsm.setGyroDataRate(LSM6DS_RATE_26_HZ);
lsm.configInt1(false, false, false, true);
lsm.enablePedometer(true);
pinMode(backlight_pin, OUTPUT);
pinMode(button_pin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(button_pin) == 0 && prevPressed == false){
timePressed = millis();
prevPressed = true;
}
if (timeout <= 20000 && !backlightOff && digitalRead(button_pin) != 0) {
timeout++;
Serial.println(timeout);
delay(1);
}
if(digitalRead(button_pin) == 1 && prevPressed == true){
if (millis() - timePressed > 2000){
digitalWrite(backlight_pin,HIGH);
tft.fillScreen(ST77XX_BLACK);
bluetooth();
while ( !bleuart.available() && digitalRead(button_pin) == 1)
{
delay(1);
}
while ( bleuart.available() && digitalRead(button_pin) == 1)
{
delay(1000);
pox.update();
data = "Steps taken: " + bluetoothstepcount() + "\nHeart rate: " + bluetoothheart() + "\nOxygen level: " + bluetoothoxygen() + "\n";
bleuart.println(data);
}
delay(5);
timeout = 0;
}
prevPressed = false;
}
Serial.println(digitalRead(button_pin));
if (digitalRead(button_pin) == 0){
digitalWrite(backlight_pin, HIGH);
if (scroll == 0){
tft.fillScreen(ST77XX_BLACK);
showTime();
Serial.println("Time displayed");
scroll = 1;
pox.shutdown();
}
else if (scroll == 1){
tft.fillScreen(ST77XX_BLACK);
stepcount();
scroll = 2;
pox.shutdown();
}
else if (scroll == 2)
{
tft.fillScreen(ST77XX_BLACK);
pox.resume();
while (!beatDetected){
pox.update();
heartRate = int(pox.getHeartRate());
SpO2 = int(pox.getSpO2());
Serial.print("Heart rate: ");
Serial.println(heartRate);
Serial.println(SpO2);
delay(1);
if (String(heartRate) != "0"){
beatDetected = true;
}
}
drawtext(String(heartRate), ST77XX_WHITE, 6);
drawtext(String(SpO2), ST77XX_WHITE, 11);
scroll = 0;
}
timeout = 0;
delay(500);
backlightOff = false;
}
if(timeout >= 20000) {
digitalWrite(backlight_pin, LOW);
Serial.println("Backlight off");
timeout = 0;
scroll = 0;
backlightOff = true;
pox.shutdown();
}
}
void oximeterreadings() {
while(heartRate < 1){
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
heartRate = pox.getHeartRate();
SpO2 = pox.getSpO2();
tsLastReport = millis();
}
delay(10);
}
Serial.println(heartRate);
Serial.println(SpO2);
drawtext(String(heartRate), ST77XX_WHITE, 6);
drawtext(String(SpO2), ST77XX_WHITE, 11);
}
// function for stepcount
void stepcount() {
int stepcount = lsm.readPedometer();
String stepcountString = "Steps taken: "+ String(stepcount);
drawtext(stepcountString, ST77XX_WHITE, 10);
}
// function for stepcount for bluetooth
String bluetoothstepcount() {
int stepcount = lsm.readPedometer();
String stepcountString = String(stepcount);
return stepcountString;
}
String bluetoothheart() {
// Make sure to call update as fast as possible
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
int heartRate = int(pox.getHeartRate());
String heartRateString = String(heartRate);
tsLastReport = millis();
return heartRateString;
}
}
String bluetoothoxygen() {
// Make sure to call update as fast as possible
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
int SpO2 = int(pox.getSpO2());
String SpO2String = String(SpO2);
tsLastReport = millis();
return SpO2String;
}
}
void showTime(){
//String time = .rtc.getCurrentDateTime();
//String h= time.substrate(13, 14);
//String m = time.substrate(16,17)
//tft.println(h + ":" +m);
//drawtext(currentTime,ST77XX_WHITE, 10);
drawtext(String(rtc.getCurrentDateTime()), ST77XX_WHITE, 3);
delay(1000);
}
void bluetooth() {
Serial.println("Bluefruit52 BLEUART Example");
Serial.println("---------------------------\n");
// Setup the BLE LED to be enabled on CONNECT
// Note: This is actually the default behaviour, but provided
// here in case you want to control this LED manually via PIN 19
Bluefruit.autoConnLed(false);
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin();
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.setName("Bluefruit52");
//Bluefruit.setName(getMcuUniqueID()); // useful testing with multiple central connections
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
// To be consistent OTA DFU should be added first if it exists
bledfu.begin();
// Configure and Start Device Information Service
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather52");
bledis.begin();
// Configure and Start BLE Uart Service
bleuart.begin();
// Start BLE Battery Service
blebas.begin();
blebas.write(100);
// Set up and start advertising
startAdv();
Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
Serial.println("Once connected, enter character(s) that you wish to send");
}
void drawtext(String text, uint16_t color, int line) {
tft.setCursor(0, line*10);
tft.setTextColor(color);
tft.setTextSize(3);
tft.setTextWrap(true);
tft.println(text);
}
void startAdv()
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
// Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(bleuart);
// Secondary Scan Response packet (optional)
// Since there is no room for 'Name' in Advertising packet
Bluefruit.ScanResponse.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
// callback invoked when central connects
void connect_callback(uint16_t conn_handle)
{
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
char central_name[32] = { 0 };
connection->getPeerName(central_name, sizeof(central_name));
Serial.print("Connected to ");
Serial.println(central_name);
}
/**
* Callback invoked when a connection is dropped
* @param conn_handle connection where this event happens
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
Serial.println();
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
}
void onBeatDetected()
{
Serial.println("Beat!");
}