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:
23:2e73c391bb12
Parent:
22:d467526abc4a
Child:
25:339931243be4
--- a/main.cpp	Sun Dec 18 05:33:58 2016 +0000
+++ b/main.cpp	Sun Jan 08 23:15:53 2017 +0000
@@ -1,111 +1,46 @@
-#include <mbed.h>
-#include "ble/BLE.h"
-#include "bricks.h"
-
-//==============================================================================
-// Custom Service Definition
-//==============================================================================
+// N06_NAND: demonstration of 'NAND' GATT service
+// Program has been tested on nRF51822-DK
+ 
+#include "bricks/bricks.h"
 
-   typedef union Buffer
-           {
-              uint8_t ubuf[10];
-              char    cbuf[10];
-              uint8_t byte;
-              uint16_t word16;
-              uint32_t word32;
-              int     i;
-              double  d;
-              float   f;
-           } Buffer;
-           
-//   Service svc(0xA000);                    // setup GATT service,   UUID 0xA000
-//   Characteristic  a(svc,0xA001,"r");      // characteristic A with UUID 0xA001
-//   Characteristic  x(svc,0xA002,"w");      // characteristic X with UUID 0xA002
-//   Characteristic  y(svc,0xA003,"w");      // characteristic Y with UUID 0xA003
-  
-   static Buffer adata[1];
-   ReadOnlyArrayGattCharacteristic<Buffer,1> a(0xA001, adata);
+   Service tiny(0xA000,"Tiny");        // Tiny Service
+   Characteristic<Digital>  chrInput1(tiny, 0xA001, "w", "Input1"); 
+   Characteristic<Digital>  chrInput2(tiny, 0xA002, "w", "Input2"); 
+   Characteristic<Digital>  chrOutput(tiny, 0xA003, "n", "Output"); 
 
-   static Buffer xdata[1];
-   WriteOnlyArrayGattCharacteristic<Buffer,1> x(0xA002, xdata);  
-
-   static Buffer ydata[1];
-   WriteOnlyArrayGattCharacteristic<Buffer,1> y(0xA002, ydata);  
-   
-   GattCharacteristic *characteristics[] = {&a,&x,&y};
-   GattService service(0xA000, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
-    
-//==============================================================================
-// Some Callbacks
-//==============================================================================
-
-   void onWritten(Blob &blue)          // Handle writes to writeCharacteristic
+   void cbWritten(O&o)                 // data written callback
    {
-      const GattWriteCallbackParams *p = blue.pWritten;
-    
-         // Check to see what characteristic was written, by handle
-    
-      if(p->handle == x.getValueHandle())
-         blink("x x x xxx x      ",CONNECTED);
-      else if(p->handle == y.getValueHandle())
-         blink("x x x xxx xxx x      ",CONNECTED);
-
-        // Update the readChar with the value of writeChar
-
-      //blue.pble->gattServer().write(a.getValueHandle(), p->data, p->len);
-      blue.gatt().write(a.getValueHandle(), p->data, p->len);
+      Digital in1, in2, out;           // need some local variables
+      
+      if (updated(o,chrInput1) || updated(o,chrInput2))  // changed inputs?
+      {
+         get(o,chrInput1,in1);         // get value of Input1 characteristics
+         get(o,chrInput2,in2);         // get value of Input2 characteristics
+         out = !(in1 && in2);          // perform NAND (Not AND) operation
+         set(o,chrOutput,out);         // store result to Output characteristics
+      }
    }
 
-
-   void onConnect(Blob &blue)          // Connection Callback
+   void cbDisconnect(O&o)              // disconnection callback
    {
-      blink("x x x   ",CONNECTED);                // indicate advertising
+      advertise(o);                    // resume advertising on disconnection
    }
-
-
-   void onDisconnect(Blob &blue)       // Disconnection Callback
+   
+   void cbSetup(O&o)                   // setup calback (after BLE init)
    {
-      blue.start();                    // start advertising on client disconnect
-      blink(ADVERTISE);                // indicate advertising
-   }
-
-
-   void onError(Blob &blue)            // Error Reporting Callback
-   {
-      (void) blue;                     // Avoid compiler warnings    
-      blink(FAULT);                    // indicate advertising
-   }    
-
+      onDisconnect(o,cbDisconnect);    // setup disconnection callback
+      onWritten(o,cbWritten);          // setup data written callback
+      
+      device(o,"NAND Gate");           // setup device name
+      name(o,"N04#1.0 NAND");          // add name to device
+      enroll(o,tiny);                  // enroll (register) TINY service
 
-   void onSetup(Blob &blue)            // Immediately After Initializing BLE 
-   {
-      blue.device("nRF51-DK");         // setup device name
-      blue.data("ABCDEF");             // setup advertising data
-      blue.name("T06.0 Custom GATT");  // setup advertising name
-
-      blue.onConnect(onConnect);       // setup connection callback
-      blue.onDisconnect(onDisconnect); // setup disconnection callback
-      blue.onWritten(onWritten);       // setup data written callback
-      
-      //blue.service(svc);               // setup custom service
-      blue.service(service);           // setup custom service
-      
-      blue.advertise("C:ng",100);      // start advertising @ 100 msec interval
-      blink(ADVERTISE);                // show that board is advertising
+      advertise(o,"C:ng",100);         // start advertising @ 100 msec interval
    }
 
-//==============================================================================
-// Main Program
-//==============================================================================
-
    int main(void)
    {
-      blink(IDLE);                      // idle blinking - just started!
-
-      Blob blue;                        // declare a blob (BLe OBject)
-      blue.init(onSetup,onError);       // init BLE base layer, always do first
-      
-      while (true)                      // Infinite loop waiting for BLE events
-         blue.sleep();                  // low power waiting for BLE events 
-   }
-
+      O o;                             // Oh-ohh - our Blob (BLuetooth OBject)
+      init(o,cbSetup);                 // init BLE base layer, always do first
+      for(;;) sleep(o);                // low power waiting for BLE events 
+   }
\ No newline at end of file