A blue button is always a nice toy ...

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Revision:
23:677689000369
Parent:
22:d467526abc4a
Child:
24:0f08f68579bd
--- a/main.cpp	Sun Dec 18 05:33:58 2016 +0000
+++ b/main.cpp	Fri Jan 06 15:28:18 2017 +0000
@@ -2,64 +2,66 @@
 #include "ble/BLE.h"
 #include "bricks.h"
 
+   Serial out(USBTX, USBRX);                 // serial port to PC terminal
+
 //==============================================================================
 // 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);
+// occupation service
+   
+   Collection detection;
+   Characteristic<Digital>  presence (detection,0xA011, "rn", "Presence"); 
+   Characteristic<DateTime> timestamp(detection,0xA012, "r",  "Timestamp"); 
+   Characteristic<Digital>  test     (detection,0xA013, "w",  "Test"); 
 
-   static Buffer xdata[1];
-   WriteOnlyArrayGattCharacteristic<Buffer,1> x(0xA002, xdata);  
+   Service service1(detection,0xA010); // our occupation service
+     
+// address service
+   
+   Collection address;
+   Characteristic<Digital>  id       (address,   0xA021, "ran", "ID"); 
+   Characteristic<Digital>  name     (address,   0xA022, "ran", "Name"); 
+   Characteristic<Digital>  layout   (address,   0xA023, "ran", "Layout"); 
 
-   static Buffer ydata[1];
-   WriteOnlyArrayGattCharacteristic<Buffer,1> y(0xA002, ydata);  
-   
-   GattCharacteristic *characteristics[] = {&a,&x,&y};
-   GattService service(0xA000, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
-    
+   Service service2(address,0xA020); // our occupation service
+     
+     
 //==============================================================================
 // Some Callbacks
 //==============================================================================
 
-   void onWritten(Blob &blue)          // Handle writes to writeCharacteristic
+   void print(Blob &blue, GattCharacteristic &chr, const char *name)
    {
-      const GattWriteCallbackParams *p = blue.pWritten;
+       Buffer data;  uint8_t *p = data;
+       blue.get(chr,data);
+
+       out.printf("%s: %02x-%02x-%02x-%02x-%02x-",name,(int)(p[0]),(int)(p[1]),(int)(p[2]),(int)(p[3]),(int)(p[4]));
+       out.printf("%02x-%02x-%02x-%02x-%02x\r\n",(int)(p[5]),(int)(p[6]),(int)(p[7]),(int)(p[8]),(int)(p[9]));
+   }    
+       
+
+   void onWritten(Blob &o)             // Handle writes to writeCharacteristic
+   {
+      const GattWriteCallbackParams *p = o.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);
+      if(p->handle == test.getValueHandle())
+      {  Digital value;  
+         
+         get(o,test,value);
+         set(o,presence,value);
 
-        // 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);
+         print(o,test,"test");
+         blink("x x x xxx xxx xxx x      ",CONNECTED);
+      }
    }
 
 
    void onConnect(Blob &blue)          // Connection Callback
    {
-      blink("x x x   ",CONNECTED);                // indicate advertising
+      blink("x x x   ",CONNECTED);     // indicate advertising
    }
 
 
@@ -79,17 +81,17 @@
 
    void onSetup(Blob &blue)            // Immediately After Initializing BLE 
    {
+      blue.config(service1);           // configure custom service 1
+      blue.config(service1);           // configure custom service 2
+      
+      blue.name("T07N#8 Easy GATT");   // setup advertising name
       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
    }
@@ -100,9 +102,12 @@
 
    int main(void)
    {
+      out.printf(" hello\r\n");
+      
+      verbose(10);                      // print all verbose messages
       blink(IDLE);                      // idle blinking - just started!
 
-      Blob blue;                        // declare a blob (BLe OBject)
+      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