NTUES lab 2 BLE

Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API

Committer:
whz861025
Date:
Tue May 14 12:45:44 2019 +0000
Revision:
0:088a3171bdab
heartRate ble done;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
whz861025 0:088a3171bdab 1 #include "mbed.h"
whz861025 0:088a3171bdab 2 #include "BLE_API/ble/BLE.h"
whz861025 0:088a3171bdab 3 #include "BLE_API/ble/services/HeartRateService.h"
whz861025 0:088a3171bdab 4 #include "BLE_API/ble/services/BatteryService.h"
whz861025 0:088a3171bdab 5 #include "BLE_API/ble/services/DeviceInformationService.h"
whz861025 0:088a3171bdab 6
whz861025 0:088a3171bdab 7 DigitalOut led(LED1, 1);
whz861025 0:088a3171bdab 8
whz861025 0:088a3171bdab 9 Ticker update;
whz861025 0:088a3171bdab 10 Ticker aliveness;
whz861025 0:088a3171bdab 11
whz861025 0:088a3171bdab 12 const static char DEVICE_NAME[] = "HELLO";
whz861025 0:088a3171bdab 13 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
whz861025 0:088a3171bdab 14 GattService::UUID_BATTERY_SERVICE,
whz861025 0:088a3171bdab 15 GattService::UUID_DEVICE_INFORMATION_SERVICE};
whz861025 0:088a3171bdab 16
whz861025 0:088a3171bdab 17 static volatile bool triggerSensorPolling = false;
whz861025 0:088a3171bdab 18
whz861025 0:088a3171bdab 19 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
whz861025 0:088a3171bdab 20 {
whz861025 0:088a3171bdab 21 (void)params;
whz861025 0:088a3171bdab 22 BLE::Instance().gap().startAdvertising(); // restart advertising
whz861025 0:088a3171bdab 23 }
whz861025 0:088a3171bdab 24
whz861025 0:088a3171bdab 25 void update_handler(void)
whz861025 0:088a3171bdab 26 {
whz861025 0:088a3171bdab 27 triggerSensorPolling = true;
whz861025 0:088a3171bdab 28 }
whz861025 0:088a3171bdab 29
whz861025 0:088a3171bdab 30 void aliveness_handler(void)
whz861025 0:088a3171bdab 31 {
whz861025 0:088a3171bdab 32 led = !led;
whz861025 0:088a3171bdab 33 }
whz861025 0:088a3171bdab 34
whz861025 0:088a3171bdab 35 void onBleInitError(BLE &ble, ble_error_t error)
whz861025 0:088a3171bdab 36 {
whz861025 0:088a3171bdab 37 (void)ble;
whz861025 0:088a3171bdab 38 (void)error;
whz861025 0:088a3171bdab 39 /* Initialization error handling should go here */
whz861025 0:088a3171bdab 40 }
whz861025 0:088a3171bdab 41
whz861025 0:088a3171bdab 42 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
whz861025 0:088a3171bdab 43 {
whz861025 0:088a3171bdab 44 BLE& ble = params->ble;
whz861025 0:088a3171bdab 45 ble_error_t error = params->error;
whz861025 0:088a3171bdab 46
whz861025 0:088a3171bdab 47 if (error != BLE_ERROR_NONE) {
whz861025 0:088a3171bdab 48 onBleInitError(ble, error);
whz861025 0:088a3171bdab 49 return;
whz861025 0:088a3171bdab 50 }
whz861025 0:088a3171bdab 51
whz861025 0:088a3171bdab 52
whz861025 0:088a3171bdab 53 ble.gap().onDisconnection(disconnectionCallback);
whz861025 0:088a3171bdab 54
whz861025 0:088a3171bdab 55 /* Setup primary service. */
whz861025 0:088a3171bdab 56 uint8_t hrmCounter = 80; // init HRM to 60bps
whz861025 0:088a3171bdab 57 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
whz861025 0:088a3171bdab 58
whz861025 0:088a3171bdab 59 /* Setup auxiliary service. */
whz861025 0:088a3171bdab 60 uint8_t batterylevel = 25;
whz861025 0:088a3171bdab 61 BatteryService battery(ble, batterylevel);
whz861025 0:088a3171bdab 62 DeviceInformationService deviceInfo(ble, "ST", "Nucleo", "SN1" );
whz861025 0:088a3171bdab 63 /* Setup advertising. */
whz861025 0:088a3171bdab 64 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
whz861025 0:088a3171bdab 65 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
whz861025 0:088a3171bdab 66 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
whz861025 0:088a3171bdab 67 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
whz861025 0:088a3171bdab 68 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
whz861025 0:088a3171bdab 69 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
whz861025 0:088a3171bdab 70 ble.gap().startAdvertising();
whz861025 0:088a3171bdab 71
whz861025 0:088a3171bdab 72 // infinite loop
whz861025 0:088a3171bdab 73 while (true) {
whz861025 0:088a3171bdab 74 // check for trigger from periodicCallback()
whz861025 0:088a3171bdab 75 if (triggerSensorPolling && ble.getGapState().connected) {
whz861025 0:088a3171bdab 76 triggerSensorPolling = false;
whz861025 0:088a3171bdab 77
whz861025 0:088a3171bdab 78 // Do blocking calls or whatever is necessary for sensor polling.
whz861025 0:088a3171bdab 79 // In our case, we simply update the HRM measurement.
whz861025 0:088a3171bdab 80 hrmCounter++;
whz861025 0:088a3171bdab 81
whz861025 0:088a3171bdab 82 // 60 <= HRM bps <= 120
whz861025 0:088a3171bdab 83 if (hrmCounter == 120) {
whz861025 0:088a3171bdab 84 hrmCounter = 60;
whz861025 0:088a3171bdab 85 }
whz861025 0:088a3171bdab 86
whz861025 0:088a3171bdab 87 // update bps
whz861025 0:088a3171bdab 88 hrService.updateHeartRate(hrmCounter);
whz861025 0:088a3171bdab 89 } else {
whz861025 0:088a3171bdab 90 ble.waitForEvent(); // low power wait for event
whz861025 0:088a3171bdab 91 }
whz861025 0:088a3171bdab 92 }
whz861025 0:088a3171bdab 93 }
whz861025 0:088a3171bdab 94
whz861025 0:088a3171bdab 95 int main(void)
whz861025 0:088a3171bdab 96 {
whz861025 0:088a3171bdab 97 update.attach(update_handler, 1);
whz861025 0:088a3171bdab 98 aliveness.attach(aliveness_handler,0.5);
whz861025 0:088a3171bdab 99
whz861025 0:088a3171bdab 100
whz861025 0:088a3171bdab 101 BLE::Instance().init(bleInitComplete);
whz861025 0:088a3171bdab 102 }