Heart Rate Monitor example for the BLE

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of Program3_BLEHeartRate by Make NTU Hackathon

Revision:
13:227a0149b677
Parent:
3:a51ca6313ad2
Child:
14:f715c13eb84f
diff -r fc21349c19c1 -r 227a0149b677 main.cpp
--- a/main.cpp	Fri Feb 19 15:14:19 2016 +0000
+++ b/main.cpp	Thu Feb 25 10:05:05 2016 +0000
@@ -17,20 +17,18 @@
 #include "mbed.h"
 #include "ble/BLE.h"
 #include "ble/services/HeartRateService.h"
-#include "ble/services/BatteryService.h"
-#include "ble/services/DeviceInformationService.h"
 
-BLE  ble;
-DigitalOut led1(LED1);
+DigitalOut led1(LED1, 1);
 
 const static char     DEVICE_NAME[]        = "HRM1";
-static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE,
-                                              GattService::UUID_DEVICE_INFORMATION_SERVICE};
+static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE};
+
 static volatile bool  triggerSensorPolling = false;
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    ble.gap().startAdvertising(); // restart advertising
+    (void)params;
+    BLE::Instance().gap().startAdvertising(); // restart advertising
 }
 
 void periodicCallback(void)
@@ -41,22 +39,33 @@
     triggerSensorPolling = true;
 }
 
-int main(void)
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+    (void)ble;
+    (void)error;
+   /* Initialization error handling should go here */
+}
+
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
-    led1 = 1;
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1); // blink LED every second
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
 
-    ble.init();
+    if (error != BLE_ERROR_NONE) {
+        onBleInitError(ble, error);
+        return;
+    }
+
+    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+
     ble.gap().onDisconnection(disconnectionCallback);
 
     /* Setup primary service. */
     uint8_t hrmCounter = 100; // init HRM to 100bps
     HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
 
-    /* Setup auxiliary service. */
-    DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
-
     /* Setup advertising. */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
@@ -67,7 +76,7 @@
     ble.gap().startAdvertising();
 
     // infinite loop
-    while (1) {
+    while (true) {
         // check for trigger from periodicCallback()
         if (triggerSensorPolling && ble.getGapState().connected) {
             triggerSensorPolling = false;
@@ -88,3 +97,11 @@
         }
     }
 }
+
+int main(void)
+{
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1); // blink LED every second
+
+    BLE::Instance().init(bleInitComplete);
+}