Bluetooth Connected TOF Sensor

Dependencies:   BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Revision:
28:def5e0f0fb06
Parent:
27:32267cee7cb8
Child:
29:cf61a5826426
--- a/main.cpp	Sat Jan 14 08:43:14 2017 +0000
+++ b/main.cpp	Wed Feb 01 22:08:56 2017 +0000
@@ -1,55 +1,185 @@
-// S07_Detection - Easy GATT setup using bricks for DETECTION services
+// S16_Blue_ToF - Blue button and Time-of-Flight Sensor Service over GATT
+ 
+#include "bricks/bricks.h"
+#include "shields/shields.h"
 
-#include "bricks/bricks.h"
-#include "detection.h"
+   typedef DisplayShield Dish;
+   
+   static Dish *pShield;
+   Blinker blink;
 
 //==============================================================================
-// Callbacks
+// GATT Database Setup
+//==============================================================================
+   
+// Detection Service
+
+   Service detection(0xA010,"Detection"); // Detection Service
+   Characteristic<Bool>        chrPresence(detection,0xA011, "n", "Presence"); 
+
+// Debug Service
+
+   Service debug(0xA030,"Debug");      // Debug Service
+   Characteristic<Bool>        chrTest      (debug, 0xA031, "w", "Test"); 
+
+   static void cbWritten(Blob &o)            // handle updates
+   {
+      Bool value;  
+      
+      if (updated(o,chrTest))          // has chrTest been updated?
+      {
+         get(o,chrTest,value);         // get value of chrTest
+         set(o,chrPresence,value);     // and store this value to chrPresence 
+      }
+   }
+
+   void services(O&o)
+   {
+      enroll(o,detection);             // enroll detection service
+      enroll(o,debug);                 // enroll debug service
+       
+      onWritten(o,cbWritten);          // setup 'data written' callback
+   }
+   
+//==============================================================================
+// Button Functionality
 //==============================================================================
 
-   void cbError(O&o)                   // Error Reporting Callback
+   typedef enum state { OFF, ON, IDLE } State;
+   
+   InterruptIn button(USER_BUTTON);    // declare blue user button
+   State state = IDLE; 
+   
+   static void cbRise(void)
+   {
+      O o;                             // declare a blob (BLE OBject)
+      Bool value = 0;  
+
+      if (o.hasInitialized())
+      {
+         blink.off();;
+         state = OFF;
+         set(o,chrPresence,value);     // and store this value to chrPresence 
+      }
+   }
+
+   static void cbFall(void)
    {
-       blinkError(o);                  // 'error' blink sequence
+      O o;                             // declare a blob (BLE OBject)
+      Bool value = 1;  
+
+      if (o.hasInitialized())
+      {
+         blink.on();
+         state = ON;
+         set(o,chrPresence,value);     // and store this value to chrPresence 
+      }
+   }
+
+   void setupButton()                  // enroll button functionality
+   {
+      button.rise(&cbRise);            // interrupt callback setup
+      button.fall(&cbFall);            // interrupt callback setup
+   }
+
+//==============================================================================
+// Setup BLE Functionality
+//==============================================================================
+
+   void cbError(Blob &o)               // Error Reporting Callback
+   {
+       blink.error();                  // 'error' blink sequence
    }    
 
-   void cbConnect(O&o)                 // Connection Callback
+   void cbConnect(Blob &o)             // Connection Callback
    {
-      blinkConnected(o);               // 'error' blink sequence
+      blink.connected();               // 'error' blink sequence
    }
 
-   void cbDisconnect(O&o)              // Disconnection Callback
+   void cbDisconnect(Blob &o)          // Disconnection Callback
    {
       advertise(o);                    // start advertising on client disconnect
-      blinkAdvertise(o);               // 'advertise' blink sequence
+      blink.advertise();               // 'advertise' blink sequence
    }
 
-   void cbSetup(O&o)                   // Immediately After Initializing BLE 
+   void cbInitComplete(Blob &o)        // Immediately After Initializing BLE 
    {
       services(o);                     // enroll all services & setup callbacks
 
       onConnect(o,cbConnect);          // setup connection callback
       onDisconnect(o,cbDisconnect);    // setup disconnection callback
    
-      device(o,"S07 Detector #3.47");  // setup device name
-      name(o,"Detector");              // setup advertising name
-      data(o,"My Layout");             // setup advertising data
+      device(o,"S16#2.0 Blue TOF");
+      name(o,"TOF HR");      
          
       advertise(o,"C:ng",100);         // start advertising @ 100 msec interval
-      blinkAdvertise(o);               // 'advertise' blink sequence
+      blink.advertise();               // 'advertise' blink sequence
+   }
+
+//==============================================================================
+// Simple Loop - Cannot Change Mode
+//==============================================================================
+
+   void distanceMeasurement(Dish &d)
+   {
+      uint32_t dist;
+
+      pShield->pBoard->sensor_top->GetDistance(&dist);
+
+      if (dist == 0xFF)
+         dist = 0xFFFFFFFF;
+
+      d.data.range_mm = dist;
    }
    
+   void loop(Blob &o, Dish &d)
+   {
+      Bool state = false;
+
+      for (;;)
+      {
+         distanceMeasurement(d);
+         if (d.data.range_mm != 0xFFFFFFFF)
+         {  if (state == false)
+            {  blink.on();             // switch LED on
+               state = true;
+               set(o,chrPresence,state);
+            }
+         }
+         else
+         {  if (state == true)
+            {  if (isConnected(o))
+                  blink.connected();   // switch LED off
+               else
+                  blink.advertise();   // switch LED off
+               state = false;
+               set(o,chrPresence,state);
+            }
+         }
+
+         if (d.red())                  // red slider switched to 'RANGE'?
+            d.refresh();               // refresh display
+         sleep(o);                     // 'sleep(o,10)' causes connect issues
+      }
+   }    
+
 //==============================================================================
 // Main Program
 //==============================================================================
 
    int main(void)
    {
-      O o;                              // declare a blob (BLE OBject)
-      verbose(o);                       // enable all trace messages
-      blinkIdle(o);                     // idle blinking - just started!
+      Blob o;
+      verbose(o);                      // enable all trace messages
+      trace(o,1,"\nS16_Blue_ToF\n");
+      blink.idle();
+      
+      pShield = new Dish("Init");      // init shield
+      pShield->setup(InterruptTOF);    // setup fot ToF measurement only
+         
+      init(o,cbInitComplete);          // init BLE system
+      setupButton();                   // enroll button functionality
+            
+      loop(o,*pShield);                // run measurement loop
+   }
 
-      init(o,cbSetup,cbError);          // init BLE base layer, always do first
-      
-      while (true)                      // Infinite loop waiting for BLE events
-         sleep(o);                      // low power waiting for BLE events 
-   }
\ No newline at end of file