A blue button is always a nice toy ...

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Revision:
28:307f58df778a
Parent:
27:32267cee7cb8
--- a/main.cpp	Sat Jan 14 08:43:14 2017 +0000
+++ b/main.cpp	Sun Oct 01 12:48:58 2017 +0000
@@ -1,26 +1,93 @@
-// S07_Detection - Easy GATT setup using bricks for DETECTION services
+// S15_Blue_Button - GATT Detector setup with
+//                   Blue Button connected to Presence characteristic
 
 #include "bricks/bricks.h"
-#include "detection.h"
+
+   Blinker blink;
+   
+//==============================================================================
+// 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
+//==============================================================================
+
+   typedef enum state { OFF, ON, IDLE } State;
+   
+   State state = IDLE; 
+   
+   static void cbRise(void)
+   {
+      O o;                             // declare a blob (BLE OBject)
+      Bool value = 0;  
+
+      if (o.hasInitialized())
+      {
+         state = OFF;
+         set(o,chrPresence,value);     // and store this value to chrPresence 
+      }
+   }
+
+   static void cbFall(void)
+   {
+      O o;                             // declare a blob (BLE OBject)
+      Bool value = 1;  
+
+      if (o.hasInitialized())
+      {
+         state = ON;
+         set(o,chrPresence,value);     // and store this value to chrPresence 
+      }
+   }
+   
 //==============================================================================
 // Callbacks
 //==============================================================================
 
    void cbError(O&o)                   // Error Reporting Callback
    {
-       blinkError(o);                  // 'error' blink sequence
+       blink.error();                  // 'error' blink sequence
    }    
 
    void cbConnect(O&o)                 // Connection Callback
    {
-      blinkConnected(o);               // 'error' blink sequence
+      blink.connected();               // 'error' blink sequence
    }
 
    void cbDisconnect(O&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 
@@ -30,12 +97,12 @@
       onConnect(o,cbConnect);          // setup connection callback
       onDisconnect(o,cbDisconnect);    // setup disconnection callback
    
-      device(o,"S07 Detector #3.47");  // setup device name
+      device(o,"S15#2.0 Blue");        // setup device name
       name(o,"Detector");              // setup advertising name
       data(o,"My Layout");             // setup advertising data
          
       advertise(o,"C:ng",100);         // start advertising @ 100 msec interval
-      blinkAdvertise(o);               // 'advertise' blink sequence
+      blink.advertise();               // 'advertise' blink sequence
    }
    
 //==============================================================================
@@ -46,10 +113,24 @@
    {
       O o;                              // declare a blob (BLE OBject)
       verbose(o);                       // enable all trace messages
-      blinkIdle(o);                     // idle blinking - just started!
+      blink.idle();                     // idle blinking - just started!
+
+      InterruptIn button(USER_BUTTON);  // declare blue user button
+      button.rise(&cbRise);             // interrupt callback setup
+      button.fall(&cbFall);             // interrupt callback setup
 
       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 
+         
+         if (state == ON)               // detection active ?
+            blink.blink("xxx ");
+            
+         if (state == OFF)              // detection inactive ?
+            blink.blink("x ");
+            
+         state = IDLE;                  // reset to IDLE state
+      }
    }
\ No newline at end of file