A cute tiny piece of code implementing an IoT NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service. The code has been tested on a Nordic nRF51822-DK.

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate_IDB0XA1 by ST

Revision:
26:dce30a5341bb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bricks/init.cpp	Sat May 19 14:10:17 2018 +0000
@@ -0,0 +1,71 @@
+// init.cpp - initialize BLE system
+
+#include "bricks/init.h"
+#include "bricks/blinker.h"
+#include "bricks/trace.h"
+
+   static void (*cbSetup)(O&o) = 0;    // setup callback 
+   static void (*cbError)(O&o) = 0;    // error callback 
+
+   static void cbDefaultError(O&o)     // default error handler
+   {
+      Blinker blink;
+      blink.error();                   // 'error' blink sequence
+   }
+
+   static void cbDefaultComplete(O&o)  // default error handler
+   {
+       // empty
+   }
+   
+   static void initComplete(BLE::InitializationCompleteCallbackContext *params)
+   {
+      Blob o;                          // setup a blob
+      o.pComplete = params;            // store to provide access
+ 
+      // params->error = (ble_error_t)((int)BLE_ERROR_NONE+1);  // uncomment for debug
+      
+      trace(o,2,"<initialized>\n");
+
+      if (params->error != BLE_ERROR_NONE)
+      {
+         trace(o,0,"error\n");
+         if (cbError)
+            (*cbError)(o);             // handle error in follow-up
+            
+         return;
+      }
+
+         // Ensure that it is the default instance of BLE
+      
+      if(o.getInstanceID() != BLE::DEFAULT_INSTANCE)
+      {
+         return;
+      }
+    
+      (*cbSetup)(o);                   // followup with setup
+   }
+
+
+   void init(O&o, void (*scb)(O&o),void (*ecb)(O&o))   // init BLE system
+   {
+      cbSetup = scb;                   // store setup callback
+      cbError = ecb;                   // store error callback
+      o.init(initComplete);
+   }
+
+
+   void init(O&o, void (*scb)(O&o))    // initialize BLE system
+   {
+      init(o,scb,cbDefaultError);      // continue with default error callback
+   }
+
+   void init(O&o)                      // initialize BLE system
+   {
+      init(o,cbDefaultComplete,cbDefaultError); 
+      while (!o.hasInitialized())
+      {
+         trace(o,1,"waiting for initialized BLE!\n");
+         wait_ms(200);
+      }
+   }