BLE NAND for ST Boards

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of N06_NAND by Hugo Pristauz

main.cpp

Committer:
hux
Date:
2016-12-18
Revision:
22:d467526abc4a
Parent:
21:0e7c08f5386f
Child:
23:2e73c391bb12

File content as of revision 22:d467526abc4a:

#include <mbed.h>
#include "ble/BLE.h"
#include "bricks.h"

//==============================================================================
// Custom Service Definition
//==============================================================================

   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);

   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
   {
      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);
   }


   void onConnect(Blob &blue)          // Connection Callback
   {
      blink("x x x   ",CONNECTED);                // indicate advertising
   }


   void onDisconnect(Blob &blue)       // Disconnection Callback
   {
      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
   }    


   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
   }

//==============================================================================
// 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 
   }