prod

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_EddystoneBeacon_Service by Bluetooth Low Energy

Revision:
34:f6d4a699a1ea
Parent:
33:862e6e0831ea
Child:
37:acd9b4823178
--- a/main.cpp	Thu Oct 01 18:30:37 2015 +0000
+++ b/main.cpp	Thu Nov 26 16:36:04 2015 +0000
@@ -15,59 +15,80 @@
  */
 
 #include "mbed.h"
-#include "BLE.h"
-#include "ble/services/EddystoneService.h"
+#include "ble/BLE.h"
+#include "EddystoneService.h"
+
+EddystoneService *eddyServicePtr;
 
-BLE ble;
-uint8_t UIDnamespace[] = {0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA}; // 10Bytes for Namespace UUID
-uint8_t UIDinstance[]  = {0xbb,0xcc,0xdd,0xee,0xff,0x00}; // 6Bytes for Instance UUID
-char Url[] = "http://www.mbed.org";
-int8_t radioTxPower = 20;
-int8_t advTxPower = -20;
-uint16_t beaconPeriodus = 1000;
-uint8_t tlmVersion = 0x00;
-
+void onBleInitError(BLE::InitializationCompleteCallbackContext* initContext)
+{
+    /* Initialization error handling goes here... */
+    (void) initContext;
+}
 
-//Callbacks for temperature / battery updates
-Ticker tlmBattery;
-Ticker tlmTemperature;
-int battery = 0;
-int temp = 0;
+/*
+ * Update TLM frame battery voltage value.
+ */
+uint16_t tlmBatteryVoltageCallback(uint16_t prevBatteryVoltage)
+{
+    prevBatteryVoltage++;
+    return prevBatteryVoltage;
+}
 
-// Setup Eddystone Service 
-EddystoneService eddyBeacon(ble,beaconPeriodus,radioTxPower);
-
-
-// Function to update beacon battery voltage
-void tlmBatteryCallback(void){
-    eddyBeacon.updateTlmBatteryVoltage(battery++);
+/*
+ * Update TLM frame beacon temperature value.
+ */
+uint16_t tlmBeaconTemperatureCallback(uint16_t prevBeaconTemperature)
+{
+    prevBeaconTemperature++;
+    return prevBeaconTemperature;
 }
 
-// Function to update Beacon Temperature
-void tlmTemperatureCallback(void){
-    eddyBeacon.updateTlmBeaconTemp(temp++);
+void bleInitComplete(BLE::InitializationCompleteCallbackContext* initContext)
+{
+    BLE         &ble  = initContext->ble;
+    ble_error_t error = initContext->error;
+
+    if (error != BLE_ERROR_NONE) {
+        onBleInitError(initContext);
+        return;
+    }
+
+    /* Set UID and TLM frame data */
+    const UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
+    const UIDInstanceID_t  uidInstanceID  = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
+    uint8_t tlmVersion = 0x00;
+
+    /* Initialize a EddystoneBeaconConfig service providing config params, default URI, and power levels. */
+    static const PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m
+    static const PowerLevels_t radioPowerLevels      = {-30, -16, -4, 4};    // Values for radio power levels, provided by manufacturer.
+
+    /* Set everything to defaults */
+    eddyServicePtr = new EddystoneService(ble, defaultAdvPowerLevels, radioPowerLevels);
+
+    /* Set default URL, UID and TLM frame data if not initialized through the config service */
+    eddyServicePtr->setURLData("http://mbed.org");
+    eddyServicePtr->setUIDData(&uidNamespaceID, &uidInstanceID);
+    eddyServicePtr->setTLMData(tlmVersion);
+
+    /* Set battery voltage and temperature callbacks */
+    eddyServicePtr->onTLMBatteryVoltageUpdate(tlmBatteryVoltageCallback);
+    eddyServicePtr->onTLMBeaconTemperatureUpdate(tlmBeaconTemperatureCallback);
+
+    /* Start Eddystone in config mode */
+    eddyServicePtr->startBeaconService(5, 5, 5);
 }
 
 int main(void)
 {
-    printf("Starting Example\r\n"); // To enable low power mode disable all printf's
-    ble.init();
-    
-    // Set Eddystone Frame Data (TLM,UID,URI...etc)
-    eddyBeacon.setTLMFrameData(tlmVersion,5.0);
-    eddyBeacon.setURLFrameData(advTxPower, Url, 2.0);
-    eddyBeacon.setUIDFrameData(advTxPower, UIDnamespace, UIDinstance, 3.0);
+    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+    ble.init(bleInitComplete);
+
+    /* SpinWait for initialization to complete. This is necessary because the
+     * BLE object is used in the main loop below. */
+    while (ble.hasInitialized()  == false) { /* spin loop */ }
     
-    // Attach functions to modify TLM data
-    tlmTemperature.attach(tlmTemperatureCallback,2.0f);
-    tlmBattery.attach(tlmBatteryCallback, 1.0f);
-
-    // Start Advertising the eddystone service. 
-    eddyBeacon.start(); 
-    ble.gap().startAdvertising();
-    
-    printf("Running...\r\n");
     while (true) {
-        ble.waitForEvent();
+        ble.waitForEvent(); /* this will return upon any system event (such as an interrupt or a ticker wakeup) */
     }
 }