Bluetooth Connected TOF Sensor

Dependencies:   BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

main.cpp

Committer:
hux
Date:
2017-01-06
Revision:
24:0f08f68579bd
Parent:
23:677689000369
Child:
25:231d3f382583

File content as of revision 24:0f08f68579bd:

// T07N_Easy_GATT - Easy GATT setup using bricks

#include "bricks.h"

//==============================================================================
// Custom Service Setup
//==============================================================================

// Detection Service
   
   Service detection(0xA010,"Detection"); // Detection Service

   Characteristic<Digital>  chrPresence(detection,0xA011, "rn", "Presence"); 
   Characteristic<DateTime> chrTimestamp(detection,0xA012, "r",  "Timestamp"); 
     
// Address Service
   
   Service address(0xA020,"Adress");   // Address Service
   
   Characteristic<Digital>  chrId(address,   0xA021, "ran", "ID"); 
   Characteristic<Digital>  chrName(address,   0xA022, "ran", "Name"); 
   Characteristic<Digital>  chrLayout(address,   0xA023, "ran", "Layout"); 

// Debug Service
   
   Service debug(0xA030,"Debug");      // Debug Service
    
   Characteristic<Digital>  chrTest     (debug,0xA031, "w",  "Test"); 

// Configure all supported services

   void services(Blob &o)              // configure all supported services
   {
      address.config(o);               // configure Address Service
      detection.config(o);             // configure Detection Service
      debug.config(o);                 // configure Debug Service
   }

//==============================================================================
// Callbacks
//==============================================================================

   void onError(Blob &o)               // Error Reporting Callback
   {
      (void) o;                        // Avoid compiler warnings    
      blink(FAULT);                    // indicate advertising
   }    

   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 onWritten(Blob &o)             // Handle writes to writeCharacteristic
   {
      if (updated(o,chrTest))          // has chrTest been updated?
      {  Digital value;  
         
         get(o,chrTest,value);         // get value of chrTest
         set(o,chrPresence,value);     // and store this value to chrPresence 

         print(o,chrTest,"test");
         blink("x x x xxx xxx xxx x      ",CONNECTED);
      }
   }

   void onSetup(Blob &o)               // Immediately After Initializing BLE 
   {
      services(o);                     // config all services

      o.onConnect(onConnect);          // setup connection callback
      o.onDisconnect(onDisconnect);    // setup disconnection callback
      o.onWritten(onWritten);          // setup data written callback
   
      device(o,"nRF51-DK");            // setup device name
      data(o,"ABCDEF");                // setup advertising data
      name(o,"T07N#1.0 Easy GATT");    // setup advertising name
         
      peripheral(o,"C:ng",100);        // start advertising @ 100 msec interval
      blink(ADVERTISE);                // show that board is advertising
   }
   
//==============================================================================
// Main Program
//==============================================================================

   int main(void)
   {
      verbose(10);                      // print all verbose messages
      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 
   }