Graduation Thesis, use Nucleo and X-Nucleo BLE
Dependencies: PulseSensor GSM Thermometer KalmanFilter
Application/main.cpp@21:fa70fabe6c49, 2018-06-05 (annotated)
- Committer:
- DuyLionTran
- Date:
- Tue Jun 05 04:17:43 2018 +0000
- Revision:
- 21:fa70fabe6c49
- Parent:
- 20:d88c0f1fd8cc
- Child:
- 23:720a27ce4567
Added Kalman filter
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:64ca984b3efd | 14 | |
DuyLionTran | 0:64ca984b3efd | 15 | /* ======================== INCLUDES ========================= */ |
DuyLionTran | 0:64ca984b3efd | 16 | #include <events/mbed_events.h> |
DuyLionTran | 0:64ca984b3efd | 17 | #include <mbed.h> |
DuyLionTran | 0:64ca984b3efd | 18 | #include "ble/BLE.h" |
DuyLionTran | 0:64ca984b3efd | 19 | #include "ble_healthcare_service.h" |
DuyLionTran | 5:c12c16c0d2ea | 20 | #include "GSM.h" |
DuyLionTran | 4:e44cd8682f1c | 21 | #include "LM35.h" |
DuyLionTran | 3:9b552b775c6e | 22 | #include "PulseSensor.h" |
DuyLionTran | 21:fa70fabe6c49 | 23 | #include "KalmanFilterPulse.h" |
DuyLionTran | 4:e44cd8682f1c | 24 | |
DuyLionTran | 4:e44cd8682f1c | 25 | |
DuyLionTran | 0:64ca984b3efd | 26 | /* ======================== DEFINES ========================== */ |
DuyLionTran | 4:e44cd8682f1c | 27 | #define PULSE_SENSOR_PIN A0 |
DuyLionTran | 4:e44cd8682f1c | 28 | #define THERM_SENSOR_PIN A1 |
DuyLionTran | 0:64ca984b3efd | 29 | |
DuyLionTran | 19:a4f3328e9d09 | 30 | #define START_SEND_INT_TEMP 13 |
DuyLionTran | 19:a4f3328e9d09 | 31 | #define START_SEND_FLOAT_TEMP 10 |
DuyLionTran | 19:a4f3328e9d09 | 32 | #define STOP_SEND_TEMP 20 |
DuyLionTran | 0:64ca984b3efd | 33 | /* ======================= VARIABLES ========================= */ |
DuyLionTran | 0:64ca984b3efd | 34 | /* GLOBAL VARIABLES */ |
DuyLionTran | 17:b7c2db3e7282 | 35 | static float currentTemperature = 39.6; |
DuyLionTran | 20:d88c0f1fd8cc | 36 | static uint16_t currentHRMCounter; |
DuyLionTran | 17:b7c2db3e7282 | 37 | static float sendCombinedTempAndHR; |
DuyLionTran | 17:b7c2db3e7282 | 38 | static uint16_t sendCombinedHRAndTemp; |
DuyLionTran | 0:64ca984b3efd | 39 | |
DuyLionTran | 0:64ca984b3efd | 40 | /* PRIVATE VARIABLES */ |
DuyLionTran | 9:be09a9bf2e2e | 41 | uint8_t cnt; |
DuyLionTran | 8:3384286a8498 | 42 | |
DuyLionTran | 17:b7c2db3e7282 | 43 | uint8_t startSendFloat = 0; |
DuyLionTran | 19:a4f3328e9d09 | 44 | |
DuyLionTran | 19:a4f3328e9d09 | 45 | bool isConnectedToDevice = false; |
DuyLionTran | 17:b7c2db3e7282 | 46 | |
DuyLionTran | 9:be09a9bf2e2e | 47 | /* No need to display the device name */ |
DuyLionTran | 9:be09a9bf2e2e | 48 | //const static char DEVICE_NAME[] = "BODY SENSOR"; |
DuyLionTran | 9:be09a9bf2e2e | 49 | |
DuyLionTran | 8:3384286a8498 | 50 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
DuyLionTran | 8:3384286a8498 | 51 | GattService::UUID_HEALTH_THERMOMETER_SERVICE, |
DuyLionTran | 9:be09a9bf2e2e | 52 | HealthCareService::USER_DATA_SERVICE_UUID, |
DuyLionTran | 9:be09a9bf2e2e | 53 | HealthCareService::DEVICE_INFO_SERVICE_UUID |
DuyLionTran | 9:be09a9bf2e2e | 54 | }; |
DuyLionTran | 17:b7c2db3e7282 | 55 | |
DuyLionTran | 17:b7c2db3e7282 | 56 | uint8_t htsPosition = HealthCareService::HealthCareService::TEMPERATURE_LOCATION_FINGER; |
DuyLionTran | 17:b7c2db3e7282 | 57 | uint8_t hrmPosition = HealthCareService::HealthCareService::HRM_LOCATION_FINGER; |
DuyLionTran | 0:64ca984b3efd | 58 | |
DuyLionTran | 0:64ca984b3efd | 59 | /* STRUCTS/CLASSESS */ |
DuyLionTran | 17:b7c2db3e7282 | 60 | HealthCareService *HealthCareServicePtr; |
DuyLionTran | 17:b7c2db3e7282 | 61 | |
DuyLionTran | 17:b7c2db3e7282 | 62 | //PulseSensor PulseSensor(); |
DuyLionTran | 0:64ca984b3efd | 63 | static EventQueue eventQueue(EVENTS_EVENT_SIZE * 20); |
DuyLionTran | 4:e44cd8682f1c | 64 | Serial serial(USBTX, USBRX); |
DuyLionTran | 21:fa70fabe6c49 | 65 | KalmanFilterPulse kalman(0.5, 10, 1); |
DuyLionTran | 21:fa70fabe6c49 | 66 | |
DuyLionTran | 0:64ca984b3efd | 67 | /* ================== FUNCTION PROTOTYPES ==================== */ |
DuyLionTran | 19:a4f3328e9d09 | 68 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *event); |
DuyLionTran | 4:e44cd8682f1c | 69 | void onBleInitError(BLE &ble, ble_error_t error); |
DuyLionTran | 4:e44cd8682f1c | 70 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params); |
DuyLionTran | 8:3384286a8498 | 71 | void onDataWrittenCallback(const GattWriteCallbackParams *params); |
DuyLionTran | 4:e44cd8682f1c | 72 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context); |
DuyLionTran | 4:e44cd8682f1c | 73 | |
DuyLionTran | 20:d88c0f1fd8cc | 74 | void sendDataToProcessing(char symbol, int data); |
DuyLionTran | 9:be09a9bf2e2e | 75 | void updatePayload(void); |
DuyLionTran | 4:e44cd8682f1c | 76 | void main_event(void); |
DuyLionTran | 4:e44cd8682f1c | 77 | void periodicCallback(void); |
DuyLionTran | 0:64ca984b3efd | 78 | |
DuyLionTran | 21:fa70fabe6c49 | 79 | PulseSensor sensor(A2, sendDataToProcessing, 10); |
DuyLionTran | 0:64ca984b3efd | 80 | /* ==================== FUNCTION DETAILS ===================== */ |
DuyLionTran | 19:a4f3328e9d09 | 81 | /* Restart Advertising on disconnection */ |
DuyLionTran | 19:a4f3328e9d09 | 82 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *event) |
DuyLionTran | 19:a4f3328e9d09 | 83 | { |
DuyLionTran | 0:64ca984b3efd | 84 | BLE::Instance().gap().startAdvertising(); |
DuyLionTran | 8:3384286a8498 | 85 | printf("Device disconnected with mobile/table\r\n"); |
DuyLionTran | 19:a4f3328e9d09 | 86 | isConnectedToDevice = false; |
DuyLionTran | 0:64ca984b3efd | 87 | } |
DuyLionTran | 0:64ca984b3efd | 88 | |
DuyLionTran | 20:d88c0f1fd8cc | 89 | void sendDataToProcessing(char symbol, int data) |
DuyLionTran | 20:d88c0f1fd8cc | 90 | { |
DuyLionTran | 21:fa70fabe6c49 | 91 | int filtedData; |
DuyLionTran | 20:d88c0f1fd8cc | 92 | if (symbol == 'B') |
DuyLionTran | 20:d88c0f1fd8cc | 93 | { |
DuyLionTran | 21:fa70fabe6c49 | 94 | filtedData = kalman.kalmanUpdate(data); |
DuyLionTran | 21:fa70fabe6c49 | 95 | filtedData = kalman.kalmanUpdate(filtedData); |
DuyLionTran | 21:fa70fabe6c49 | 96 | currentHRMCounter = filtedData/1; |
DuyLionTran | 21:fa70fabe6c49 | 97 | printf("%c%d %d\r\n", symbol, data,(uint8_t)(filtedData/1)); |
DuyLionTran | 20:d88c0f1fd8cc | 98 | } |
DuyLionTran | 20:d88c0f1fd8cc | 99 | } |
DuyLionTran | 18:a9d67c51715e | 100 | |
DuyLionTran | 19:a4f3328e9d09 | 101 | void updatePayload(void) |
DuyLionTran | 19:a4f3328e9d09 | 102 | { |
DuyLionTran | 9:be09a9bf2e2e | 103 | // Update the count in the SERVICE_DATA field of the advertising payload |
DuyLionTran | 9:be09a9bf2e2e | 104 | cnt++; |
DuyLionTran | 10:4e0f5173269e | 105 | uint8_t service_data[8]; |
DuyLionTran | 10:4e0f5173269e | 106 | /* first 2 bytes are for service UUID */ |
DuyLionTran | 9:be09a9bf2e2e | 107 | service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF; |
DuyLionTran | 9:be09a9bf2e2e | 108 | service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8; |
DuyLionTran | 10:4e0f5173269e | 109 | /* next 4 bytes are for client ID */ |
DuyLionTran | 10:4e0f5173269e | 110 | service_data[2] = 0x07; |
DuyLionTran | 10:4e0f5173269e | 111 | service_data[3] = 0x09; |
DuyLionTran | 10:4e0f5173269e | 112 | service_data[4] = 0x9A; |
DuyLionTran | 10:4e0f5173269e | 113 | service_data[5] = 0xAC; |
DuyLionTran | 10:4e0f5173269e | 114 | /* last 2 bytes are sensor data */ |
DuyLionTran | 10:4e0f5173269e | 115 | service_data[6] = cnt; |
DuyLionTran | 10:4e0f5173269e | 116 | service_data[7] = cnt; |
DuyLionTran | 9:be09a9bf2e2e | 117 | ble_error_t err = BLE::Instance().gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); |
DuyLionTran | 9:be09a9bf2e2e | 118 | |
DuyLionTran | 9:be09a9bf2e2e | 119 | } |
DuyLionTran | 9:be09a9bf2e2e | 120 | |
DuyLionTran | 19:a4f3328e9d09 | 121 | void main_event(void) |
DuyLionTran | 19:a4f3328e9d09 | 122 | { |
DuyLionTran | 19:a4f3328e9d09 | 123 | uint16_t intTemperatureValuex100; |
DuyLionTran | 19:a4f3328e9d09 | 124 | uint8_t fractionalTemperature; |
DuyLionTran | 19:a4f3328e9d09 | 125 | uint8_t decimalTemperature; |
DuyLionTran | 19:a4f3328e9d09 | 126 | |
DuyLionTran | 19:a4f3328e9d09 | 127 | if (BLE::Instance().gap().getState().connected) |
DuyLionTran | 19:a4f3328e9d09 | 128 | { |
DuyLionTran | 19:a4f3328e9d09 | 129 | isConnectedToDevice = true; |
DuyLionTran | 19:a4f3328e9d09 | 130 | } |
DuyLionTran | 19:a4f3328e9d09 | 131 | |
DuyLionTran | 0:64ca984b3efd | 132 | /* Do blocking calls or whatever is necessary for sensor polling. |
DuyLionTran | 0:64ca984b3efd | 133 | In our case, we simply update the Temperature measurement. */ |
DuyLionTran | 17:b7c2db3e7282 | 134 | /* TODO Read temperature */ |
DuyLionTran | 17:b7c2db3e7282 | 135 | currentTemperature = (currentTemperature + 0.16 > 42.79) ? 35.69 : currentTemperature + 1.18; |
DuyLionTran | 17:b7c2db3e7282 | 136 | |
DuyLionTran | 17:b7c2db3e7282 | 137 | /* TODO Read Heart Rate */ |
DuyLionTran | 20:d88c0f1fd8cc | 138 | // currentHRMCounter = (currentHRMCounter + 1 > 120) ? 75 : (currentHRMCounter + HRM_increasement); |
DuyLionTran | 20:d88c0f1fd8cc | 139 | /* Updated in callback function */ |
DuyLionTran | 17:b7c2db3e7282 | 140 | |
DuyLionTran | 19:a4f3328e9d09 | 141 | /* Some little tricks here to make the temperature decimal and fractional parts different from the send codes */ |
DuyLionTran | 19:a4f3328e9d09 | 142 | intTemperatureValuex100 = currentTemperature * 100; |
DuyLionTran | 19:a4f3328e9d09 | 143 | fractionalTemperature = intTemperatureValuex100 % 100; |
DuyLionTran | 19:a4f3328e9d09 | 144 | decimalTemperature = intTemperatureValuex100 / 100; |
DuyLionTran | 19:a4f3328e9d09 | 145 | if ((fractionalTemperature == START_SEND_INT_TEMP) || |
DuyLionTran | 19:a4f3328e9d09 | 146 | (fractionalTemperature == START_SEND_FLOAT_TEMP) || |
DuyLionTran | 19:a4f3328e9d09 | 147 | (fractionalTemperature == STOP_SEND_TEMP) || |
DuyLionTran | 19:a4f3328e9d09 | 148 | (fractionalTemperature == decimalTemperature)) |
DuyLionTran | 19:a4f3328e9d09 | 149 | { |
DuyLionTran | 19:a4f3328e9d09 | 150 | fractionalTemperature = fractionalTemperature + 1; |
DuyLionTran | 19:a4f3328e9d09 | 151 | } |
DuyLionTran | 19:a4f3328e9d09 | 152 | |
DuyLionTran | 19:a4f3328e9d09 | 153 | |
DuyLionTran | 17:b7c2db3e7282 | 154 | /* TODO Update Service data */ |
DuyLionTran | 9:be09a9bf2e2e | 155 | updatePayload(); |
DuyLionTran | 11:5a4313edf10d | 156 | |
DuyLionTran | 11:5a4313edf10d | 157 | /* sendCombinedTempAndHR = (currentTemperature * 100)* 1000 + currentHRMCounter */ |
DuyLionTran | 12:dc974f6fed97 | 158 | sendCombinedTempAndHR = currentTemperature * 1000; /* Temperature float to int conversion */ |
DuyLionTran | 12:dc974f6fed97 | 159 | sendCombinedTempAndHR = sendCombinedTempAndHR + (float)(currentHRMCounter/100.0); |
DuyLionTran | 17:b7c2db3e7282 | 160 | |
DuyLionTran | 19:a4f3328e9d09 | 161 | |
DuyLionTran | 19:a4f3328e9d09 | 162 | |
DuyLionTran | 17:b7c2db3e7282 | 163 | switch (startSendFloat) { |
DuyLionTran | 19:a4f3328e9d09 | 164 | case 0: sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 19:a4f3328e9d09 | 165 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)START_SEND_INT_TEMP; |
DuyLionTran | 19:a4f3328e9d09 | 166 | break; |
DuyLionTran | 19:a4f3328e9d09 | 167 | |
DuyLionTran | 19:a4f3328e9d09 | 168 | case 1: /* sendCombinedHRAndTemp = (currentHRMCounter * 100) + decimalTemperature */ |
DuyLionTran | 17:b7c2db3e7282 | 169 | /* Because the maximum size of HRM Value is 2 bytes */ |
DuyLionTran | 17:b7c2db3e7282 | 170 | sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 19:a4f3328e9d09 | 171 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)decimalTemperature; |
DuyLionTran | 17:b7c2db3e7282 | 172 | break; |
DuyLionTran | 19:a4f3328e9d09 | 173 | |
DuyLionTran | 17:b7c2db3e7282 | 174 | case 2: sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 19:a4f3328e9d09 | 175 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)START_SEND_FLOAT_TEMP; |
DuyLionTran | 17:b7c2db3e7282 | 176 | break; |
DuyLionTran | 17:b7c2db3e7282 | 177 | |
DuyLionTran | 17:b7c2db3e7282 | 178 | case 3: sendCombinedHRAndTemp = currentHRMCounter * 100; |
DuyLionTran | 19:a4f3328e9d09 | 179 | sendCombinedHRAndTemp = sendCombinedHRAndTemp + (uint8_t)fractionalTemperature; |
DuyLionTran | 17:b7c2db3e7282 | 180 | break; |
DuyLionTran | 19:a4f3328e9d09 | 181 | |
DuyLionTran | 17:b7c2db3e7282 | 182 | default: break; |
DuyLionTran | 17:b7c2db3e7282 | 183 | } |
DuyLionTran | 11:5a4313edf10d | 184 | // printf("sendCombinedTempAndHR %d\r\n", sendCombinedTempAndHR); |
DuyLionTran | 16:0325e647496f | 185 | // printf("sendCombinedTempAndHR %.2f\r\n\r\n", (float)sendCombinedTempAndHR); |
DuyLionTran | 17:b7c2db3e7282 | 186 | // printf("currentHRMCounter %d\r\n", currentHRMCounter); |
DuyLionTran | 17:b7c2db3e7282 | 187 | // printf("currentTemperature %d\r\n", (uint8_t)currentTemperature); |
DuyLionTran | 17:b7c2db3e7282 | 188 | // printf("sendCombinedHRAndTemp %d\r\n", sendCombinedHRAndTemp); |
DuyLionTran | 17:b7c2db3e7282 | 189 | |
DuyLionTran | 19:a4f3328e9d09 | 190 | if (isConnectedToDevice) |
DuyLionTran | 19:a4f3328e9d09 | 191 | { |
DuyLionTran | 12:dc974f6fed97 | 192 | HealthCareServicePtr->updateTemperature(sendCombinedTempAndHR); |
DuyLionTran | 19:a4f3328e9d09 | 193 | HealthCareServicePtr->updateHeartRate(sendCombinedHRAndTemp); |
DuyLionTran | 19:a4f3328e9d09 | 194 | startSendFloat = (startSendFloat + 1) % 4; |
DuyLionTran | 4:e44cd8682f1c | 195 | } |
DuyLionTran | 19:a4f3328e9d09 | 196 | else |
DuyLionTran | 19:a4f3328e9d09 | 197 | { |
DuyLionTran | 19:a4f3328e9d09 | 198 | startSendFloat = 0; |
DuyLionTran | 19:a4f3328e9d09 | 199 | } |
DuyLionTran | 0:64ca984b3efd | 200 | } |
DuyLionTran | 0:64ca984b3efd | 201 | |
DuyLionTran | 19:a4f3328e9d09 | 202 | void periodicCallback(void) |
DuyLionTran | 19:a4f3328e9d09 | 203 | { |
DuyLionTran | 4:e44cd8682f1c | 204 | /* call main_event immediately */ |
DuyLionTran | 4:e44cd8682f1c | 205 | eventQueue.call(main_event); |
DuyLionTran | 0:64ca984b3efd | 206 | } |
DuyLionTran | 0:64ca984b3efd | 207 | |
DuyLionTran | 0:64ca984b3efd | 208 | void printMacAddress() { |
DuyLionTran | 0:64ca984b3efd | 209 | /* Print out device MAC address to the console*/ |
DuyLionTran | 0:64ca984b3efd | 210 | Gap::AddressType_t addr_type; |
DuyLionTran | 0:64ca984b3efd | 211 | Gap::Address_t address; |
DuyLionTran | 0:64ca984b3efd | 212 | BLE::Instance().gap().getAddress(&addr_type, address); |
DuyLionTran | 0:64ca984b3efd | 213 | printf("DEVICE MAC ADDRESS: "); |
DuyLionTran | 0:64ca984b3efd | 214 | for (int i = 5; i >= 1; i--) { |
DuyLionTran | 0:64ca984b3efd | 215 | printf("%02x:", address[i]); |
DuyLionTran | 0:64ca984b3efd | 216 | } |
DuyLionTran | 0:64ca984b3efd | 217 | printf("%02x\r\n", address[0]); |
DuyLionTran | 0:64ca984b3efd | 218 | } |
DuyLionTran | 0:64ca984b3efd | 219 | |
DuyLionTran | 19:a4f3328e9d09 | 220 | void onBleInitError(BLE &ble, ble_error_t error) |
DuyLionTran | 19:a4f3328e9d09 | 221 | { |
DuyLionTran | 0:64ca984b3efd | 222 | /* Initialization error handling should go here */ |
DuyLionTran | 8:3384286a8498 | 223 | printf("BLE init error!\r\n"); |
DuyLionTran | 8:3384286a8498 | 224 | } |
DuyLionTran | 8:3384286a8498 | 225 | |
DuyLionTran | 8:3384286a8498 | 226 | /** |
DuyLionTran | 8:3384286a8498 | 227 | * This callback allows the HealthCareService to receive updates to the controlState Characteristic. |
DuyLionTran | 8:3384286a8498 | 228 | * |
DuyLionTran | 8:3384286a8498 | 229 | * @param[in] params |
DuyLionTran | 8:3384286a8498 | 230 | * Information about the characterisitc being updated. |
DuyLionTran | 8:3384286a8498 | 231 | */ |
DuyLionTran | 19:a4f3328e9d09 | 232 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
DuyLionTran | 19:a4f3328e9d09 | 233 | { |
DuyLionTran | 16:0325e647496f | 234 | // printf("Write callback, value %d\r\n", *(params->data)); |
DuyLionTran | 19:a4f3328e9d09 | 235 | if ((params->handle == HealthCareServicePtr->getTypeHandle()) && (params->len >= 1)) |
DuyLionTran | 19:a4f3328e9d09 | 236 | { |
DuyLionTran | 14:e6029b780879 | 237 | uint8_t oldPosition = htsPosition; |
DuyLionTran | 14:e6029b780879 | 238 | htsPosition = *(params->data); |
DuyLionTran | 16:0325e647496f | 239 | printf("Old type: %d, New type %d\r\n", oldPosition, htsPosition); |
DuyLionTran | 16:0325e647496f | 240 | HealthCareServicePtr->updateType(htsPosition); |
DuyLionTran | 8:3384286a8498 | 241 | } |
DuyLionTran | 19:a4f3328e9d09 | 242 | if ((params->handle == HealthCareServicePtr->getLocationHandle()) && (params->len >= 1)) |
DuyLionTran | 19:a4f3328e9d09 | 243 | { |
DuyLionTran | 16:0325e647496f | 244 | uint8_t oldPosition = hrmPosition; |
DuyLionTran | 16:0325e647496f | 245 | hrmPosition = *(params->data); |
DuyLionTran | 16:0325e647496f | 246 | printf("Old location: %d, New location %d\r\n", oldPosition, hrmPosition); |
DuyLionTran | 16:0325e647496f | 247 | HealthCareServicePtr->updateLocation(hrmPosition); |
DuyLionTran | 16:0325e647496f | 248 | } |
DuyLionTran | 0:64ca984b3efd | 249 | } |
DuyLionTran | 0:64ca984b3efd | 250 | |
DuyLionTran | 0:64ca984b3efd | 251 | /** |
DuyLionTran | 0:64ca984b3efd | 252 | * @brief Callback triggered when the ble initialization process has finished |
DuyLionTran | 0:64ca984b3efd | 253 | */ |
DuyLionTran | 19:a4f3328e9d09 | 254 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
DuyLionTran | 19:a4f3328e9d09 | 255 | { |
DuyLionTran | 0:64ca984b3efd | 256 | BLE& ble = params->ble; |
DuyLionTran | 0:64ca984b3efd | 257 | ble_error_t error = params->error; |
DuyLionTran | 19:a4f3328e9d09 | 258 | uint8_t service_data[8]; |
DuyLionTran | 0:64ca984b3efd | 259 | |
DuyLionTran | 19:a4f3328e9d09 | 260 | if (error != BLE_ERROR_NONE) |
DuyLionTran | 19:a4f3328e9d09 | 261 | { |
DuyLionTran | 0:64ca984b3efd | 262 | onBleInitError(ble, error); |
DuyLionTran | 0:64ca984b3efd | 263 | return; |
DuyLionTran | 0:64ca984b3efd | 264 | } |
DuyLionTran | 0:64ca984b3efd | 265 | |
DuyLionTran | 0:64ca984b3efd | 266 | /* Ensure that it is the default instance of BLE */ |
DuyLionTran | 19:a4f3328e9d09 | 267 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) |
DuyLionTran | 19:a4f3328e9d09 | 268 | { |
DuyLionTran | 0:64ca984b3efd | 269 | return; |
DuyLionTran | 0:64ca984b3efd | 270 | } |
DuyLionTran | 8:3384286a8498 | 271 | uint8_t initial_HRMIncreasement = 1; |
DuyLionTran | 19:a4f3328e9d09 | 272 | ble.gap().onDisconnection(&disconnectionCallback); |
DuyLionTran | 8:3384286a8498 | 273 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
DuyLionTran | 8:3384286a8498 | 274 | |
DuyLionTran | 14:e6029b780879 | 275 | HealthCareServicePtr = new HealthCareService(ble, currentTemperature, htsPosition, |
DuyLionTran | 15:00a1c0ea570c | 276 | currentHRMCounter, hrmPosition, |
DuyLionTran | 8:3384286a8498 | 277 | initial_HRMIncreasement); |
DuyLionTran | 0:64ca984b3efd | 278 | |
DuyLionTran | 0:64ca984b3efd | 279 | /* setup advertising */ |
DuyLionTran | 0:64ca984b3efd | 280 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
DuyLionTran | 0:64ca984b3efd | 281 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
DuyLionTran | 2:16f6cfcd7505 | 282 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER ); |
DuyLionTran | 0:64ca984b3efd | 283 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
DuyLionTran | 9:be09a9bf2e2e | 284 | |
DuyLionTran | 9:be09a9bf2e2e | 285 | /* No need to display the device name */ |
DuyLionTran | 9:be09a9bf2e2e | 286 | // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME , (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
DuyLionTran | 19:a4f3328e9d09 | 287 | /* uint8_t service_data[8]; */ |
DuyLionTran | 9:be09a9bf2e2e | 288 | /* first 2 bytes are for service UUID */ |
DuyLionTran | 9:be09a9bf2e2e | 289 | service_data[0] = HealthCareService::USER_DATA_SERVICE_UUID & 0xFF; |
DuyLionTran | 9:be09a9bf2e2e | 290 | service_data[1] = HealthCareService::USER_DATA_SERVICE_UUID >> 8; |
DuyLionTran | 10:4e0f5173269e | 291 | /* next 4 bytes are for client ID */ |
DuyLionTran | 10:4e0f5173269e | 292 | service_data[2] = 0x07; |
DuyLionTran | 10:4e0f5173269e | 293 | service_data[3] = 0x09; |
DuyLionTran | 11:5a4313edf10d | 294 | service_data[4] = 0x89; |
DuyLionTran | 11:5a4313edf10d | 295 | service_data[5] = 0xAB; |
DuyLionTran | 9:be09a9bf2e2e | 296 | /* last 2 bytes are sensor data */ |
DuyLionTran | 10:4e0f5173269e | 297 | service_data[6] = cnt; |
DuyLionTran | 10:4e0f5173269e | 298 | service_data[7] = cnt; |
DuyLionTran | 9:be09a9bf2e2e | 299 | |
DuyLionTran | 9:be09a9bf2e2e | 300 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA , (uint8_t *)service_data, sizeof(service_data)); |
DuyLionTran | 9:be09a9bf2e2e | 301 | |
DuyLionTran | 0:64ca984b3efd | 302 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
DuyLionTran | 13:e6ddc458904e | 303 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
DuyLionTran | 0:64ca984b3efd | 304 | ble.gap().startAdvertising(); |
DuyLionTran | 0:64ca984b3efd | 305 | |
DuyLionTran | 0:64ca984b3efd | 306 | printMacAddress(); |
DuyLionTran | 8:3384286a8498 | 307 | printf("BLE init successfully\r\n"); |
DuyLionTran | 0:64ca984b3efd | 308 | } |
DuyLionTran | 0:64ca984b3efd | 309 | |
DuyLionTran | 19:a4f3328e9d09 | 310 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) |
DuyLionTran | 19:a4f3328e9d09 | 311 | { |
DuyLionTran | 0:64ca984b3efd | 312 | BLE &ble = BLE::Instance(); |
DuyLionTran | 0:64ca984b3efd | 313 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
DuyLionTran | 0:64ca984b3efd | 314 | } |
DuyLionTran | 0:64ca984b3efd | 315 | |
DuyLionTran | 0:64ca984b3efd | 316 | /* MAIN FUNCTION */ |
DuyLionTran | 19:a4f3328e9d09 | 317 | int main() |
DuyLionTran | 19:a4f3328e9d09 | 318 | { |
DuyLionTran | 4:e44cd8682f1c | 319 | serial.baud(115200); |
DuyLionTran | 4:e44cd8682f1c | 320 | printf("\r\n BODY WIRELESS SENSOR NETWORK\r\n"); |
DuyLionTran | 4:e44cd8682f1c | 321 | /* call periodicCallback every 500ms */ |
DuyLionTran | 13:e6ddc458904e | 322 | eventQueue.call_every(1000, periodicCallback); |
DuyLionTran | 4:e44cd8682f1c | 323 | |
DuyLionTran | 4:e44cd8682f1c | 324 | /* init BLE */ |
DuyLionTran | 0:64ca984b3efd | 325 | BLE &ble = BLE::Instance(); |
DuyLionTran | 0:64ca984b3efd | 326 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
DuyLionTran | 0:64ca984b3efd | 327 | ble.init(bleInitComplete); |
DuyLionTran | 4:e44cd8682f1c | 328 | |
DuyLionTran | 4:e44cd8682f1c | 329 | /* dispatch the event queue */ |
DuyLionTran | 20:d88c0f1fd8cc | 330 | sensor.start(); |
DuyLionTran | 0:64ca984b3efd | 331 | eventQueue.dispatch_forever(); |
DuyLionTran | 0:64ca984b3efd | 332 | |
DuyLionTran | 0:64ca984b3efd | 333 | return 0; |
DuyLionTran | 0:64ca984b3efd | 334 | } |