NTUES lab 2 BLE

Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API

Revision:
0:088a3171bdab
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue May 14 12:45:44 2019 +0000
@@ -0,0 +1,102 @@
+#include "mbed.h"
+#include "BLE_API/ble/BLE.h"
+#include "BLE_API/ble/services/HeartRateService.h"
+#include "BLE_API/ble/services/BatteryService.h"
+#include "BLE_API/ble/services/DeviceInformationService.h"
+
+DigitalOut led(LED1, 1);   
+
+Ticker update;
+Ticker aliveness;
+
+const static char     DEVICE_NAME[]        = "HELLO";
+static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE,
+                                              GattService::UUID_BATTERY_SERVICE,
+                                              GattService::UUID_DEVICE_INFORMATION_SERVICE};
+
+static volatile bool  triggerSensorPolling = false;
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    (void)params;
+    BLE::Instance().gap().startAdvertising(); // restart advertising
+}
+
+void update_handler(void)
+{
+    triggerSensorPolling = true;
+}
+
+void aliveness_handler(void)
+{
+    led = !led;    
+}
+
+void onBleInitError(BLE &ble, ble_error_t error)
+{
+    (void)ble;
+    (void)error;
+   /* Initialization error handling should go here */
+}
+
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
+
+    if (error != BLE_ERROR_NONE) {
+        onBleInitError(ble, error);
+        return;
+    }
+
+
+    ble.gap().onDisconnection(disconnectionCallback);
+
+    /* Setup primary service. */
+    uint8_t hrmCounter = 80; // init HRM to 60bps
+    HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+
+    /* Setup auxiliary service. */
+    uint8_t batterylevel = 25;
+    BatteryService battery(ble, batterylevel);
+    DeviceInformationService deviceInfo(ble, "ST", "Nucleo", "SN1" );
+    /* 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));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
+    ble.gap().startAdvertising();
+
+    // infinite loop
+    while (true) {
+        // check for trigger from periodicCallback()
+        if (triggerSensorPolling && ble.getGapState().connected) {
+            triggerSensorPolling = false;
+
+            // Do blocking calls or whatever is necessary for sensor polling.
+            // In our case, we simply update the HRM measurement.
+            hrmCounter++;
+
+            //  60 <= HRM bps <= 120
+            if (hrmCounter == 120) {
+                hrmCounter = 60;
+            }
+
+            // update bps
+            hrService.updateHeartRate(hrmCounter);
+        } else {
+            ble.waitForEvent(); // low power wait for event
+        }
+    }
+}
+
+int main(void)
+{
+    update.attach(update_handler, 1);
+    aliveness.attach(aliveness_handler,0.5);
+
+
+    BLE::Instance().init(bleInitComplete);
+}