Bluetooth enabled control of a BLDC via the Allegro MicroSystems A4960.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_ModularRobot by Annie Mao

Revision:
10:af76616e4d75
Parent:
9:35a5a5796286
Child:
11:4251b62991ac
diff -r 35a5a5796286 -r af76616e4d75 main.cpp
--- a/main.cpp	Tue Sep 29 11:50:28 2015 +0000
+++ b/main.cpp	Tue Jan 12 10:34:34 2016 +0000
@@ -15,26 +15,27 @@
  */
 
 #include "mbed.h"
-#include "BLE.h"
+#include "ble/BLE.h"
 #include "LEDService.h"
 
-BLE        ble;
-DigitalOut alivenessLED(LED1);
-DigitalOut actuatedLED(LED2);
+DigitalOut alivenessLED(LED1, 0);
+DigitalOut actuatedLED(LED2, 0);
 
 const static char     DEVICE_NAME[] = "LED";
 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
 
 LEDService *ledServicePtr;
 
+Ticker ticker;
+
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    ble.gap().startAdvertising();
+    BLE::Instance().gap().startAdvertising();
 }
 
 void periodicCallback(void)
 {
-    //alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
+    alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
 }
 
 /**
@@ -49,21 +50,38 @@
     }
 }
 
-int main(void)
+/**
+ * This function is called when the ble initialization process has failed
+ */
+void onBleInitError(BLE &ble, ble_error_t error)
 {
-    alivenessLED = 0;
-    actuatedLED  = 0;
+    /* Initialization error handling should go here */
+}
 
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1);
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE&        ble   = params->ble;
+    ble_error_t error = params->error;
 
-    ble.init();
+    if (error != BLE_ERROR_NONE) {
+        /* In case of error, forward the error handling to onBleInitError */
+        onBleInitError(ble, error);
+        return;
+    }
+
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+        return;
+    }
+ 
     ble.gap().onDisconnection(disconnectionCallback);
     ble.gattServer().onDataWritten(onDataWrittenCallback);
 
     bool initialValueForLEDCharacteristic = false;
-    LEDService ledService(ble, initialValueForLEDCharacteristic);
-    ledServicePtr = &ledService;
+    ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
 
     /* setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
@@ -72,6 +90,18 @@
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
     ble.gap().startAdvertising();
+}
+
+int main(void)
+{
+    ticker.attach(periodicCallback, 1); /* Blink LED every second */
+
+    BLE &ble = BLE::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 */ }
 
     while (true) {
         ble.waitForEvent();