with 2 advertisers

Dependencies:   ADXL345_nRF51 BLE_API advertiser_data mbed nRF51822

Fork of BLE_GAP_Acceleration_Observer by Ames HCI IoT

Revision:
7:d78080148084
Parent:
6:460ee10d9930
Child:
8:122d77e4fc6a
Child:
9:7360ae6b9b1e
--- a/main.cpp	Thu Jul 16 23:08:49 2015 +0000
+++ b/main.cpp	Sat Jul 18 06:56:25 2015 +0000
@@ -19,16 +19,53 @@
 
 BLE        ble;
 
+typedef union _accleration_data {
+  float f;
+  uint8_t  b[4];
+} acceleration_data;
+
+typedef enum _parse_state {
+    PARSE_SIZE,
+    PARSE_TYPE,
+    PARSE_DATA
+} ParseState;
+
+Serial pc(USBTX, USBRX);
+
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
 {
     if (params->peerAddr[5] == 0xe5 && params->peerAddr[4] == 0x4f) {
-        printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
+        
+        pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
                 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
                 params->rssi, params->isScanResponse, params->type);
         for (unsigned index = 0; index < params->advertisingDataLen; index++) {
-           printf("%02x ", params->advertisingData[index]);
+           pc.printf("%02x ", params->advertisingData[index]);
+        }
+        pc.printf("\r\n");
+        
+        /* Quick and dirty naive way */
+        acceleration_data acceleration;
+        acceleration.f = 4.5;
+        if (params->advertisingDataLen != 6) return;
+        for (unsigned i = 0; i < 4; i++) {
+            acceleration.b[i] = params->advertisingData[i+2];
         }
-        printf("\r\n");
+        pc.printf("Acceleration: %.2f", acceleration.f);
+        pc.printf("\r\n");
+        /*
+        unsigned index = 0;
+        uint8_t data_size = 0;
+        unsigned data_index = 0;
+        while (index < params->advertisingDataLen) {
+            data_size = params->advertisingData[index];
+            data_index = index;
+            // TODO: CHECK for size > dataLen
+            for (index; index < data_index + data_size; index++) { 
+                // do something with the data 
+            }
+        }
+        */
     }
 }
 
@@ -39,6 +76,11 @@
     // Set scan to be constant by interval == window
     ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */);
     ble.gap().startScan(advertisementCallback);
+    
+    pc.baud(9600);
+    wait(8);
+    pc.printf("Started scanning...\n\r");
+    
 
     while (true) {
         ble.waitForEvent();