Graduation Thesis, use Nucleo and X-Nucleo BLE

Dependencies:   PulseSensor GSM Thermometer KalmanFilter

Committer:
DuyLionTran
Date:
Sat Jun 23 18:12:35 2018 +0000
Revision:
25:8621ebb6ea0c
Parent:
23:720a27ce4567
* version 1.4.4   23-06-2018  Added max30100 still developing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 0:64ca984b3efd 1 /**
DuyLionTran 2:16f6cfcd7505 2 * This is the project for "BLE HealthCare". The device is attached on any patient's body at will.
DuyLionTran 0:64ca984b3efd 3
DuyLionTran 0:64ca984b3efd 4 * Revision:
DuyLionTran 2:16f6cfcd7505 5 * version 0.8 02-12-2018
DuyLionTran 2:16f6cfcd7505 6 * version 0.8.5 02-14-2018
DuyLionTran 4:e44cd8682f1c 7 * version 0.9 02-15-2018 Pulse sensor and thermometer added
DuyLionTran 4:e44cd8682f1c 8 * version 0.9.5 02-16-2018 Calculation for pulse sensor and thermometer. GSM library added
DuyLionTran 6:26a4b005cb75 9 * version 0.9.6 02-21-2018 Update mbed-os
DuyLionTran 7:d8032114995b 10 * version 0.9.6 02-21-2018 Some modification for LM35
DuyLionTran 8:3384286a8498 11 * version 0.9.8 03-04-2018 Data receiving from client device added
DuyLionTran 9:be09a9bf2e2e 12 * version 1.0 03-09-2018 Some minor bugs fixed
DuyLionTran 10:4e0f5173269e 13 * version 1.0.5 03-09-2018 Some minor bugs fixed
DuyLionTran 23:720a27ce4567 14 * version 1.3.7 04-06-2018 Some minor bugs fixed
DuyLionTran 25:8621ebb6ea0c 15 * version 1.4.4 23-06-2018 Added max30100 still developing
DuyLionTran 23:720a27ce4567 16
DuyLionTran 0:64ca984b3efd 17 /* ======================== INCLUDES ========================= */
DuyLionTran 0:64ca984b3efd 18 #include <events/mbed_events.h>
DuyLionTran 0:64ca984b3efd 19 #include <mbed.h>
DuyLionTran 0:64ca984b3efd 20 #include "ble/BLE.h"
DuyLionTran 0:64ca984b3efd 21 #include "ble_healthcare_service.h"
DuyLionTran 4:e44cd8682f1c 22 #include "LM35.h"
DuyLionTran 3:9b552b775c6e 23 #include "PulseSensor.h"
DuyLionTran 25:8621ebb6ea0c 24 #include "MAX30100_PulseOximeter.h"
DuyLionTran 21:fa70fabe6c49 25 #include "KalmanFilterPulse.h"
DuyLionTran 23:720a27ce4567 26
DuyLionTran 0:64ca984b3efd 27 /* ======================== DEFINES ========================== */
DuyLionTran 23:720a27ce4567 28 #define PULSE_SENSOR_PIN A2
DuyLionTran 23:720a27ce4567 29 #define THERM_SENSOR_PIN A3
DuyLionTran 23:720a27ce4567 30
DuyLionTran 19:a4f3328e9d09 31 #define START_SEND_INT_TEMP 13
DuyLionTran 19:a4f3328e9d09 32 #define START_SEND_FLOAT_TEMP 10
DuyLionTran 19:a4f3328e9d09 33 #define STOP_SEND_TEMP 20
DuyLionTran 25:8621ebb6ea0c 34
DuyLionTran 25:8621ebb6ea0c 35 #define SAMPLE_HEARTRATE 16
DuyLionTran 25:8621ebb6ea0c 36 #define AVERAGE_HEARTRATE 8
DuyLionTran 25:8621ebb6ea0c 37 #define REPORTING_PERIOS 3125 /* 1/16 s */
DuyLionTran 25:8621ebb6ea0c 38
DuyLionTran 0:64ca984b3efd 39 /* ======================= VARIABLES ========================= */
DuyLionTran 0:64ca984b3efd 40 /* GLOBAL VARIABLES */
DuyLionTran 25:8621ebb6ea0c 41 static float processedTemperature = 36.9;
DuyLionTran 25:8621ebb6ea0c 42 static uint16_t processedHRMCounter;
DuyLionTran 17:b7c2db3e7282 43 static float sendCombinedTempAndHR;
DuyLionTran 17:b7c2db3e7282 44 static uint16_t sendCombinedHRAndTemp;
DuyLionTran 25:8621ebb6ea0c 45
DuyLionTran 25:8621ebb6ea0c 46 bool isEmptyavgHR = true;
DuyLionTran 25:8621ebb6ea0c 47 bool startCalculatingStandardDeviation = true;
DuyLionTran 25:8621ebb6ea0c 48 std::vector<float> valuesHeartRate;
DuyLionTran 25:8621ebb6ea0c 49 std::vector<uint8_t> valuesSpO2;
DuyLionTran 25:8621ebb6ea0c 50
DuyLionTran 25:8621ebb6ea0c 51 uint8_t avgHRIndex = 0;
DuyLionTran 25:8621ebb6ea0c 52 float avgHR[AVERAGE_HEARTRATE] = {0};
DuyLionTran 25:8621ebb6ea0c 53 uint16_t avgHeartRate;
DuyLionTran 23:720a27ce4567 54
DuyLionTran 0:64ca984b3efd 55 /* PRIVATE VARIABLES */
DuyLionTran 9:be09a9bf2e2e 56 uint8_t cnt;
DuyLionTran 23:720a27ce4567 57
DuyLionTran 17:b7c2db3e7282 58 uint8_t startSendFloat = 0;
DuyLionTran 23:720a27ce4567 59
DuyLionTran 19:a4f3328e9d09 60 bool isConnectedToDevice = false;
DuyLionTran 23:720a27ce4567 61
DuyLionTran 9:be09a9bf2e2e 62 /* No need to display the device name */
DuyLionTran 9:be09a9bf2e2e 63 //const static char DEVICE_NAME[] = "BODY SENSOR";
DuyLionTran 23:720a27ce4567 64
DuyLionTran 8:3384286a8498 65 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
DuyLionTran 8:3384286a8498 66 GattService::UUID_HEALTH_THERMOMETER_SERVICE,
DuyLionTran 9:be09a9bf2e2e 67 HealthCareService::USER_DATA_SERVICE_UUID,
DuyLionTran 9:be09a9bf2e2e 68 HealthCareService::DEVICE_INFO_SERVICE_UUID
DuyLionTran 9:be09a9bf2e2e 69 };
DuyLionTran 17:b7c2db3e7282 70
DuyLionTran 17:b7c2db3e7282 71 uint8_t htsPosition = HealthCareService::HealthCareService::TEMPERATURE_LOCATION_FINGER;
DuyLionTran 17:b7c2db3e7282 72 uint8_t hrmPosition = HealthCareService::HealthCareService::HRM_LOCATION_FINGER;
DuyLionTran 23:720a27ce4567 73
DuyLionTran 0:64ca984b3efd 74 /* STRUCTS/CLASSESS */
DuyLionTran 25:8621ebb6ea0c 75 typedef struct {
DuyLionTran 25:8621ebb6ea0c 76 float heartRate;
DuyLionTran 25:8621ebb6ea0c 77 float SpO2;
DuyLionTran 25:8621ebb6ea0c 78 } message_t;
DuyLionTran 25:8621ebb6ea0c 79
DuyLionTran 17:b7c2db3e7282 80 HealthCareService *HealthCareServicePtr;
DuyLionTran 25:8621ebb6ea0c 81
DuyLionTran 25:8621ebb6ea0c 82 MemoryPool<message_t, 32> mpool;
DuyLionTran 25:8621ebb6ea0c 83 Queue<message_t, 32> queue;
DuyLionTran 25:8621ebb6ea0c 84
DuyLionTran 25:8621ebb6ea0c 85 Thread thread;
DuyLionTran 25:8621ebb6ea0c 86 PulseOximeter pox;
DuyLionTran 23:720a27ce4567 87
DuyLionTran 25:8621ebb6ea0c 88 static EventQueue eventQueue(EVENTS_EVENT_SIZE * 32);
DuyLionTran 23:720a27ce4567 89 Serial serial(USBTX, USBRX);
DuyLionTran 23:720a27ce4567 90 KalmanFilterPulse kalman(0.5, 10, 1);
DuyLionTran 23:720a27ce4567 91 KalmanFilterPulse kalman1(2, 10, 0.1);
DuyLionTran 23:720a27ce4567 92
DuyLionTran 0:64ca984b3efd 93 /* ================== FUNCTION PROTOTYPES ==================== */
DuyLionTran 19:a4f3328e9d09 94 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *event);
DuyLionTran 4:e44cd8682f1c 95 void onBleInitError(BLE &ble, ble_error_t error);
DuyLionTran 4:e44cd8682f1c 96 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params);
DuyLionTran 8:3384286a8498 97 void onDataWrittenCallback(const GattWriteCallbackParams *params);
DuyLionTran 4:e44cd8682f1c 98 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context);
DuyLionTran 23:720a27ce4567 99
DuyLionTran 20:d88c0f1fd8cc 100 void sendDataToProcessing(char symbol, int data);
DuyLionTran 25:8621ebb6ea0c 101 void onBeatDetected();
DuyLionTran 25:8621ebb6ea0c 102 void beatEvent();
DuyLionTran 9:be09a9bf2e2e 103 void updatePayload(void);
DuyLionTran 4:e44cd8682f1c 104 void main_event(void);
DuyLionTran 4:e44cd8682f1c 105 void periodicCallback(void);
DuyLionTran 23:720a27ce4567 106
DuyLionTran 25:8621ebb6ea0c 107 PulseSensor sensor(PULSE_SENSOR_PIN, sendDataToProcessing, 20);
DuyLionTran 25:8621ebb6ea0c 108 LM35Therm lm35(THERM_SENSOR_PIN, 4.56);
DuyLionTran 0:64ca984b3efd 109
DuyLionTran 0:64ca984b3efd 110 /* ==================== FUNCTION DETAILS ===================== */
DuyLionTran 19:a4f3328e9d09 111 /* Restart Advertising on disconnection */
DuyLionTran 19:a4f3328e9d09 112 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *event)
DuyLionTran 19:a4f3328e9d09 113 {
DuyLionTran 0:64ca984b3efd 114 BLE::Instance().gap().startAdvertising();
DuyLionTran 8:3384286a8498 115 printf("Device disconnected with mobile/table\r\n");
DuyLionTran 19:a4f3328e9d09 116 isConnectedToDevice = false;
DuyLionTran 0:64ca984b3efd 117 }
DuyLionTran 23:720a27ce4567 118
DuyLionTran 20:d88c0f1fd8cc 119 void sendDataToProcessing(char symbol, int data)
DuyLionTran 20:d88c0f1fd8cc 120 {
DuyLionTran 21:fa70fabe6c49 121 int filtedData;
DuyLionTran 20:d88c0f1fd8cc 122 if (symbol == 'B')
DuyLionTran 20:d88c0f1fd8cc 123 {
DuyLionTran 21:fa70fabe6c49 124 filtedData = kalman.kalmanUpdate(data);
DuyLionTran 21:fa70fabe6c49 125 filtedData = kalman.kalmanUpdate(filtedData);
DuyLionTran 25:8621ebb6ea0c 126 processedHRMCounter = filtedData/1;
DuyLionTran 21:fa70fabe6c49 127 printf("%c%d %d\r\n", symbol, data,(uint8_t)(filtedData/1));
DuyLionTran 20:d88c0f1fd8cc 128 }
DuyLionTran 20:d88c0f1fd8cc 129 }
DuyLionTran 23:720a27ce4567 130
DuyLionTran 19:a4f3328e9d09 131 void updatePayload(void)
DuyLionTran 19:a4f3328e9d09 132 {
DuyLionTran 9:be09a9bf2e2e 133 // Update the count in the SERVICE_DATA field of the advertising payload
DuyLionTran 9:be09a9bf2e2e 134 cnt++;
DuyLionTran 10:4e0f5173269e 135 uint8_t service_data[8];
DuyLionTran 10:4e0f5173269e 136 /* first 2 bytes are for service UUID */
DuyLionTran 9:be09a9bf2e2e 137 service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF;
DuyLionTran 9:be09a9bf2e2e 138 service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8;
DuyLionTran 10:4e0f5173269e 139 /* next 4 bytes are for client ID */
DuyLionTran 10:4e0f5173269e 140 service_data[2] = 0x07;
DuyLionTran 10:4e0f5173269e 141 service_data[3] = 0x09;
DuyLionTran 10:4e0f5173269e 142 service_data[4] = 0x9A;
DuyLionTran 10:4e0f5173269e 143 service_data[5] = 0xAC;
DuyLionTran 10:4e0f5173269e 144 /* last 2 bytes are sensor data */
DuyLionTran 10:4e0f5173269e 145 service_data[6] = cnt;
DuyLionTran 10:4e0f5173269e 146 service_data[7] = cnt;
DuyLionTran 9:be09a9bf2e2e 147 ble_error_t err = BLE::Instance().gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data));
DuyLionTran 23:720a27ce4567 148
DuyLionTran 9:be09a9bf2e2e 149 }
DuyLionTran 23:720a27ce4567 150
DuyLionTran 19:a4f3328e9d09 151 void main_event(void)
DuyLionTran 19:a4f3328e9d09 152 {
DuyLionTran 19:a4f3328e9d09 153 uint16_t intTemperatureValuex100;
DuyLionTran 19:a4f3328e9d09 154 uint8_t fractionalTemperature;
DuyLionTran 19:a4f3328e9d09 155 uint8_t decimalTemperature;
DuyLionTran 19:a4f3328e9d09 156
DuyLionTran 19:a4f3328e9d09 157 if (BLE::Instance().gap().getState().connected)
DuyLionTran 19:a4f3328e9d09 158 {
DuyLionTran 19:a4f3328e9d09 159 isConnectedToDevice = true;
DuyLionTran 19:a4f3328e9d09 160 }
DuyLionTran 19:a4f3328e9d09 161
DuyLionTran 0:64ca984b3efd 162 /* Do blocking calls or whatever is necessary for sensor polling.
DuyLionTran 0:64ca984b3efd 163 In our case, we simply update the Temperature measurement. */
DuyLionTran 17:b7c2db3e7282 164 /* TODO Read temperature */
DuyLionTran 23:720a27ce4567 165 lm35.getAverageValue();
DuyLionTran 25:8621ebb6ea0c 166 processedTemperature = kalman1.kalmanUpdate(lm35.getTempInC());
DuyLionTran 25:8621ebb6ea0c 167 processedTemperature = kalman1.kalmanUpdate(processedTemperature);
DuyLionTran 17:b7c2db3e7282 168
DuyLionTran 17:b7c2db3e7282 169 /* TODO Read Heart Rate */
DuyLionTran 20:d88c0f1fd8cc 170 /* Updated in callback function */
DuyLionTran 17:b7c2db3e7282 171
DuyLionTran 19:a4f3328e9d09 172 /* Some little tricks here to make the temperature decimal and fractional parts different from the send codes */
DuyLionTran 25:8621ebb6ea0c 173 intTemperatureValuex100 = processedTemperature * 100;
DuyLionTran 19:a4f3328e9d09 174 fractionalTemperature = intTemperatureValuex100 % 100;
DuyLionTran 19:a4f3328e9d09 175 decimalTemperature = intTemperatureValuex100 / 100;
DuyLionTran 19:a4f3328e9d09 176 if ((fractionalTemperature == START_SEND_INT_TEMP) ||
DuyLionTran 19:a4f3328e9d09 177 (fractionalTemperature == START_SEND_FLOAT_TEMP) ||
DuyLionTran 19:a4f3328e9d09 178 (fractionalTemperature == STOP_SEND_TEMP) ||
DuyLionTran 19:a4f3328e9d09 179 (fractionalTemperature == decimalTemperature))
DuyLionTran 19:a4f3328e9d09 180 {
DuyLionTran 19:a4f3328e9d09 181 fractionalTemperature = fractionalTemperature + 1;
DuyLionTran 19:a4f3328e9d09 182 }
DuyLionTran 19:a4f3328e9d09 183
DuyLionTran 19:a4f3328e9d09 184
DuyLionTran 17:b7c2db3e7282 185 /* TODO Update Service data */
DuyLionTran 9:be09a9bf2e2e 186 updatePayload();
DuyLionTran 11:5a4313edf10d 187
DuyLionTran 25:8621ebb6ea0c 188 /* sendCombinedTempAndHR = (processedTemperature * 100)* 1000 + processedHRMCounter */
DuyLionTran 25:8621ebb6ea0c 189 sendCombinedTempAndHR = intTemperatureValuex100 * 10.0; /* Temperature float to int conversion */
DuyLionTran 25:8621ebb6ea0c 190 sendCombinedTempAndHR = sendCombinedTempAndHR + (float)(processedHRMCounter/100.0);
DuyLionTran 19:a4f3328e9d09 191
DuyLionTran 17:b7c2db3e7282 192 switch (startSendFloat) {
DuyLionTran 25:8621ebb6ea0c 193 case 0: sendCombinedHRAndTemp = processedHRMCounter * 100;
DuyLionTran 19:a4f3328e9d09 194 sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)START_SEND_INT_TEMP;
DuyLionTran 19:a4f3328e9d09 195 break;
DuyLionTran 19:a4f3328e9d09 196
DuyLionTran 25:8621ebb6ea0c 197 case 1: /* sendCombinedHRAndTemp = (processedHRMCounter * 100) + decimalTemperature */
DuyLionTran 17:b7c2db3e7282 198 /* Because the maximum size of HRM Value is 2 bytes */
DuyLionTran 25:8621ebb6ea0c 199 sendCombinedHRAndTemp = processedHRMCounter * 100;
DuyLionTran 19:a4f3328e9d09 200 sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)decimalTemperature;
DuyLionTran 17:b7c2db3e7282 201 break;
DuyLionTran 19:a4f3328e9d09 202
DuyLionTran 25:8621ebb6ea0c 203 case 2: sendCombinedHRAndTemp = processedHRMCounter * 100;
DuyLionTran 19:a4f3328e9d09 204 sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)START_SEND_FLOAT_TEMP;
DuyLionTran 17:b7c2db3e7282 205 break;
DuyLionTran 17:b7c2db3e7282 206
DuyLionTran 25:8621ebb6ea0c 207 case 3: sendCombinedHRAndTemp = processedHRMCounter * 100;
DuyLionTran 19:a4f3328e9d09 208 sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)fractionalTemperature;
DuyLionTran 17:b7c2db3e7282 209 break;
DuyLionTran 19:a4f3328e9d09 210
DuyLionTran 17:b7c2db3e7282 211 default: break;
DuyLionTran 17:b7c2db3e7282 212 }
DuyLionTran 11:5a4313edf10d 213 // printf("sendCombinedTempAndHR %d\r\n", sendCombinedTempAndHR);
DuyLionTran 16:0325e647496f 214 // printf("sendCombinedTempAndHR %.2f\r\n\r\n", (float)sendCombinedTempAndHR);
DuyLionTran 25:8621ebb6ea0c 215 // printf("processedHRMCounter %d\r\n", processedHRMCounter);
DuyLionTran 25:8621ebb6ea0c 216 // printf("processedTemperature %d\r\n", (uint8_t)processedTemperature);
DuyLionTran 17:b7c2db3e7282 217 // printf("sendCombinedHRAndTemp %d\r\n", sendCombinedHRAndTemp);
DuyLionTran 17:b7c2db3e7282 218
DuyLionTran 19:a4f3328e9d09 219 if (isConnectedToDevice)
DuyLionTran 19:a4f3328e9d09 220 {
DuyLionTran 12:dc974f6fed97 221 HealthCareServicePtr->updateTemperature(sendCombinedTempAndHR);
DuyLionTran 19:a4f3328e9d09 222 HealthCareServicePtr->updateHeartRate(sendCombinedHRAndTemp);
DuyLionTran 19:a4f3328e9d09 223 startSendFloat = (startSendFloat + 1) % 4;
DuyLionTran 4:e44cd8682f1c 224 }
DuyLionTran 19:a4f3328e9d09 225 else
DuyLionTran 19:a4f3328e9d09 226 {
DuyLionTran 19:a4f3328e9d09 227 startSendFloat = 0;
DuyLionTran 19:a4f3328e9d09 228 }
DuyLionTran 0:64ca984b3efd 229 }
DuyLionTran 25:8621ebb6ea0c 230
DuyLionTran 25:8621ebb6ea0c 231 void beatEvent()
DuyLionTran 25:8621ebb6ea0c 232 {
DuyLionTran 25:8621ebb6ea0c 233 float heartRate;
DuyLionTran 25:8621ebb6ea0c 234 float rawHeartRate;
DuyLionTran 25:8621ebb6ea0c 235 uint8_t spO2;
DuyLionTran 25:8621ebb6ea0c 236 uint16_t rawSpO2;
DuyLionTran 25:8621ebb6ea0c 237 uint8_t counterMAX30100 = 0;
DuyLionTran 25:8621ebb6ea0c 238 bool newValueMAX30100 = false;
DuyLionTran 25:8621ebb6ea0c 239 uint32_t loopCount = 0;
DuyLionTran 25:8621ebb6ea0c 240 printf("Thread\r\n");
DuyLionTran 25:8621ebb6ea0c 241 while (1)
DuyLionTran 25:8621ebb6ea0c 242 {
DuyLionTran 25:8621ebb6ea0c 243 pox.update();
DuyLionTran 25:8621ebb6ea0c 244 if (loopCount >= REPORTING_PERIOS)
DuyLionTran 25:8621ebb6ea0c 245 {
DuyLionTran 25:8621ebb6ea0c 246 heartRate = pox.getHeartRate();
DuyLionTran 25:8621ebb6ea0c 247 spO2 = pox.getSpO2();
DuyLionTran 25:8621ebb6ea0c 248
DuyLionTran 25:8621ebb6ea0c 249 if(heartRate != 0 && spO2 != 0)
DuyLionTran 25:8621ebb6ea0c 250 {
DuyLionTran 25:8621ebb6ea0c 251 printf("Heart rate: %f",heartRate);
DuyLionTran 25:8621ebb6ea0c 252 printf(" bpm / SpO2: %d%\r\n", spO2);
DuyLionTran 25:8621ebb6ea0c 253 valuesHeartRate.push_back(heartRate);
DuyLionTran 25:8621ebb6ea0c 254 valuesSpO2.push_back(spO2);
DuyLionTran 25:8621ebb6ea0c 255 counterMAX30100++;
DuyLionTran 25:8621ebb6ea0c 256 }
DuyLionTran 25:8621ebb6ea0c 257 else
DuyLionTran 25:8621ebb6ea0c 258 {
DuyLionTran 25:8621ebb6ea0c 259 printf("No finger\r\n");
DuyLionTran 25:8621ebb6ea0c 260 }
DuyLionTran 25:8621ebb6ea0c 261
DuyLionTran 25:8621ebb6ea0c 262 if(counterMAX30100 == SAMPLE_HEARTRATE) {
DuyLionTran 25:8621ebb6ea0c 263 rawHeartRate = 0;
DuyLionTran 25:8621ebb6ea0c 264 rawSpO2 = 0;
DuyLionTran 25:8621ebb6ea0c 265 for(int i = 0; i < SAMPLE_HEARTRATE; i++){
DuyLionTran 25:8621ebb6ea0c 266 rawHeartRate += valuesHeartRate[i];
DuyLionTran 25:8621ebb6ea0c 267 rawSpO2 += valuesSpO2[i];
DuyLionTran 25:8621ebb6ea0c 268 }
DuyLionTran 25:8621ebb6ea0c 269
DuyLionTran 25:8621ebb6ea0c 270 rawHeartRate /= SAMPLE_HEARTRATE;
DuyLionTran 25:8621ebb6ea0c 271 rawSpO2 /= SAMPLE_HEARTRATE;
DuyLionTran 25:8621ebb6ea0c 272
DuyLionTran 25:8621ebb6ea0c 273 counterMAX30100 = 0;
DuyLionTran 25:8621ebb6ea0c 274 valuesHeartRate.clear();
DuyLionTran 25:8621ebb6ea0c 275 valuesSpO2.clear();
DuyLionTran 25:8621ebb6ea0c 276 newValueMAX30100 = true;
DuyLionTran 25:8621ebb6ea0c 277 }
DuyLionTran 25:8621ebb6ea0c 278 loopCount = 0;
DuyLionTran 25:8621ebb6ea0c 279 }
DuyLionTran 25:8621ebb6ea0c 280 if (newValueMAX30100)
DuyLionTran 25:8621ebb6ea0c 281 {
DuyLionTran 25:8621ebb6ea0c 282 message_t *message = mpool.alloc();
DuyLionTran 25:8621ebb6ea0c 283 message->heartRate = rawHeartRate;
DuyLionTran 25:8621ebb6ea0c 284 message->SpO2 = rawSpO2;
DuyLionTran 25:8621ebb6ea0c 285 printf("Send vales %.2f %.2f \r\n", rawHeartRate, rawSpO2);
DuyLionTran 25:8621ebb6ea0c 286 queue.put(message);
DuyLionTran 25:8621ebb6ea0c 287 newValueMAX30100 = false;
DuyLionTran 25:8621ebb6ea0c 288 }
DuyLionTran 25:8621ebb6ea0c 289 loopCount++;
DuyLionTran 25:8621ebb6ea0c 290 }
DuyLionTran 25:8621ebb6ea0c 291 }
DuyLionTran 23:720a27ce4567 292
DuyLionTran 19:a4f3328e9d09 293 void periodicCallback(void)
DuyLionTran 19:a4f3328e9d09 294 {
DuyLionTran 4:e44cd8682f1c 295 /* call main_event immediately */
DuyLionTran 4:e44cd8682f1c 296 eventQueue.call(main_event);
DuyLionTran 0:64ca984b3efd 297 }
DuyLionTran 23:720a27ce4567 298
DuyLionTran 0:64ca984b3efd 299 void printMacAddress() {
DuyLionTran 0:64ca984b3efd 300 /* Print out device MAC address to the console*/
DuyLionTran 0:64ca984b3efd 301 Gap::AddressType_t addr_type;
DuyLionTran 0:64ca984b3efd 302 Gap::Address_t address;
DuyLionTran 0:64ca984b3efd 303 BLE::Instance().gap().getAddress(&addr_type, address);
DuyLionTran 0:64ca984b3efd 304 printf("DEVICE MAC ADDRESS: ");
DuyLionTran 0:64ca984b3efd 305 for (int i = 5; i >= 1; i--) {
DuyLionTran 0:64ca984b3efd 306 printf("%02x:", address[i]);
DuyLionTran 0:64ca984b3efd 307 }
DuyLionTran 0:64ca984b3efd 308 printf("%02x\r\n", address[0]);
DuyLionTran 0:64ca984b3efd 309 }
DuyLionTran 23:720a27ce4567 310
DuyLionTran 19:a4f3328e9d09 311 void onBleInitError(BLE &ble, ble_error_t error)
DuyLionTran 19:a4f3328e9d09 312 {
DuyLionTran 0:64ca984b3efd 313 /* Initialization error handling should go here */
DuyLionTran 8:3384286a8498 314 printf("BLE init error!\r\n");
DuyLionTran 8:3384286a8498 315 }
DuyLionTran 23:720a27ce4567 316
DuyLionTran 8:3384286a8498 317 /**
DuyLionTran 8:3384286a8498 318 * This callback allows the HealthCareService to receive updates to the controlState Characteristic.
DuyLionTran 8:3384286a8498 319 *
DuyLionTran 8:3384286a8498 320 * @param[in] params
DuyLionTran 8:3384286a8498 321 * Information about the characterisitc being updated.
DuyLionTran 8:3384286a8498 322 */
DuyLionTran 19:a4f3328e9d09 323 void onDataWrittenCallback(const GattWriteCallbackParams *params)
DuyLionTran 19:a4f3328e9d09 324 {
DuyLionTran 16:0325e647496f 325 // printf("Write callback, value %d\r\n", *(params->data));
DuyLionTran 19:a4f3328e9d09 326 if ((params->handle == HealthCareServicePtr->getTypeHandle()) && (params->len >= 1))
DuyLionTran 19:a4f3328e9d09 327 {
DuyLionTran 14:e6029b780879 328 uint8_t oldPosition = htsPosition;
DuyLionTran 14:e6029b780879 329 htsPosition = *(params->data);
DuyLionTran 16:0325e647496f 330 printf("Old type: %d, New type %d\r\n", oldPosition, htsPosition);
DuyLionTran 16:0325e647496f 331 HealthCareServicePtr->updateType(htsPosition);
DuyLionTran 8:3384286a8498 332 }
DuyLionTran 19:a4f3328e9d09 333 if ((params->handle == HealthCareServicePtr->getLocationHandle()) && (params->len >= 1))
DuyLionTran 19:a4f3328e9d09 334 {
DuyLionTran 16:0325e647496f 335 uint8_t oldPosition = hrmPosition;
DuyLionTran 16:0325e647496f 336 hrmPosition = *(params->data);
DuyLionTran 16:0325e647496f 337 printf("Old location: %d, New location %d\r\n", oldPosition, hrmPosition);
DuyLionTran 16:0325e647496f 338 HealthCareServicePtr->updateLocation(hrmPosition);
DuyLionTran 16:0325e647496f 339 }
DuyLionTran 0:64ca984b3efd 340 }
DuyLionTran 23:720a27ce4567 341
DuyLionTran 0:64ca984b3efd 342 /**
DuyLionTran 0:64ca984b3efd 343 * @brief Callback triggered when the ble initialization process has finished
DuyLionTran 0:64ca984b3efd 344 */
DuyLionTran 19:a4f3328e9d09 345 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
DuyLionTran 19:a4f3328e9d09 346 {
DuyLionTran 0:64ca984b3efd 347 BLE& ble = params->ble;
DuyLionTran 0:64ca984b3efd 348 ble_error_t error = params->error;
DuyLionTran 19:a4f3328e9d09 349 uint8_t service_data[8];
DuyLionTran 23:720a27ce4567 350
DuyLionTran 19:a4f3328e9d09 351 if (error != BLE_ERROR_NONE)
DuyLionTran 19:a4f3328e9d09 352 {
DuyLionTran 0:64ca984b3efd 353 onBleInitError(ble, error);
DuyLionTran 0:64ca984b3efd 354 return;
DuyLionTran 0:64ca984b3efd 355 }
DuyLionTran 23:720a27ce4567 356
DuyLionTran 0:64ca984b3efd 357 /* Ensure that it is the default instance of BLE */
DuyLionTran 19:a4f3328e9d09 358 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE)
DuyLionTran 19:a4f3328e9d09 359 {
DuyLionTran 0:64ca984b3efd 360 return;
DuyLionTran 0:64ca984b3efd 361 }
DuyLionTran 8:3384286a8498 362 uint8_t initial_HRMIncreasement = 1;
DuyLionTran 19:a4f3328e9d09 363 ble.gap().onDisconnection(&disconnectionCallback);
DuyLionTran 8:3384286a8498 364 ble.gattServer().onDataWritten(onDataWrittenCallback);
DuyLionTran 8:3384286a8498 365
DuyLionTran 25:8621ebb6ea0c 366 HealthCareServicePtr = new HealthCareService(ble, processedTemperature, htsPosition,
DuyLionTran 25:8621ebb6ea0c 367 processedHRMCounter, hrmPosition,
DuyLionTran 8:3384286a8498 368 initial_HRMIncreasement);
DuyLionTran 23:720a27ce4567 369
DuyLionTran 0:64ca984b3efd 370 /* setup advertising */
DuyLionTran 0:64ca984b3efd 371 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
DuyLionTran 0:64ca984b3efd 372 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
DuyLionTran 2:16f6cfcd7505 373 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER );
DuyLionTran 0:64ca984b3efd 374 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
DuyLionTran 9:be09a9bf2e2e 375
DuyLionTran 9:be09a9bf2e2e 376 /* No need to display the device name */
DuyLionTran 9:be09a9bf2e2e 377 // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME , (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
DuyLionTran 19:a4f3328e9d09 378 /* uint8_t service_data[8]; */
DuyLionTran 9:be09a9bf2e2e 379 /* first 2 bytes are for service UUID */
DuyLionTran 9:be09a9bf2e2e 380 service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF;
DuyLionTran 9:be09a9bf2e2e 381 service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8;
DuyLionTran 10:4e0f5173269e 382 /* next 4 bytes are for client ID */
DuyLionTran 10:4e0f5173269e 383 service_data[2] = 0x07;
DuyLionTran 10:4e0f5173269e 384 service_data[3] = 0x09;
DuyLionTran 11:5a4313edf10d 385 service_data[4] = 0x89;
DuyLionTran 11:5a4313edf10d 386 service_data[5] = 0xAB;
DuyLionTran 9:be09a9bf2e2e 387 /* last 2 bytes are sensor data */
DuyLionTran 10:4e0f5173269e 388 service_data[6] = cnt;
DuyLionTran 10:4e0f5173269e 389 service_data[7] = cnt;
DuyLionTran 23:720a27ce4567 390
DuyLionTran 9:be09a9bf2e2e 391 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA , (uint8_t *)service_data, sizeof(service_data));
DuyLionTran 9:be09a9bf2e2e 392
DuyLionTran 0:64ca984b3efd 393 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
DuyLionTran 13:e6ddc458904e 394 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
DuyLionTran 0:64ca984b3efd 395 ble.gap().startAdvertising();
DuyLionTran 0:64ca984b3efd 396
DuyLionTran 0:64ca984b3efd 397 printMacAddress();
DuyLionTran 8:3384286a8498 398 printf("BLE init successfully\r\n");
DuyLionTran 0:64ca984b3efd 399 }
DuyLionTran 23:720a27ce4567 400
DuyLionTran 19:a4f3328e9d09 401 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context)
DuyLionTran 19:a4f3328e9d09 402 {
DuyLionTran 0:64ca984b3efd 403 BLE &ble = BLE::Instance();
DuyLionTran 0:64ca984b3efd 404 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
DuyLionTran 0:64ca984b3efd 405 }
DuyLionTran 23:720a27ce4567 406
DuyLionTran 0:64ca984b3efd 407 /* MAIN FUNCTION */
DuyLionTran 19:a4f3328e9d09 408 int main()
DuyLionTran 19:a4f3328e9d09 409 {
DuyLionTran 4:e44cd8682f1c 410 serial.baud(115200);
DuyLionTran 4:e44cd8682f1c 411 printf("\r\n BODY WIRELESS SENSOR NETWORK\r\n");
DuyLionTran 25:8621ebb6ea0c 412
DuyLionTran 25:8621ebb6ea0c 413 // if(!pox.begin())
DuyLionTran 25:8621ebb6ea0c 414 // {
DuyLionTran 25:8621ebb6ea0c 415 // printf("No sensor detected\r\n");
DuyLionTran 25:8621ebb6ea0c 416 // }
DuyLionTran 25:8621ebb6ea0c 417 // else
DuyLionTran 25:8621ebb6ea0c 418 // {
DuyLionTran 25:8621ebb6ea0c 419 // printf("Sensor found\r\n");
DuyLionTran 25:8621ebb6ea0c 420 // }
DuyLionTran 25:8621ebb6ea0c 421 // thread.start(callback(beatEvent));
DuyLionTran 25:8621ebb6ea0c 422
DuyLionTran 4:e44cd8682f1c 423 /* call periodicCallback every 500ms */
DuyLionTran 13:e6ddc458904e 424 eventQueue.call_every(1000, periodicCallback);
DuyLionTran 4:e44cd8682f1c 425
DuyLionTran 4:e44cd8682f1c 426 /* init BLE */
DuyLionTran 0:64ca984b3efd 427 BLE &ble = BLE::Instance();
DuyLionTran 0:64ca984b3efd 428 ble.onEventsToProcess(scheduleBleEventsProcessing);
DuyLionTran 0:64ca984b3efd 429 ble.init(bleInitComplete);
DuyLionTran 4:e44cd8682f1c 430
DuyLionTran 4:e44cd8682f1c 431 /* dispatch the event queue */
DuyLionTran 20:d88c0f1fd8cc 432 sensor.start();
DuyLionTran 0:64ca984b3efd 433 eventQueue.dispatch_forever();
DuyLionTran 23:720a27ce4567 434
DuyLionTran 0:64ca984b3efd 435 return 0;
DuyLionTran 23:720a27ce4567 436 }