Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Revision:
3:2a4ac5b87046
Parent:
2:5504b714c9ae
Child:
4:331dddea780e
Child:
5:0f42dcae4cdf
--- a/main.cpp	Sun Jul 16 15:10:18 2017 +0000
+++ b/main.cpp	Mon Jul 17 11:35:44 2017 +0000
@@ -11,15 +11,15 @@
 #include "GapAdvertisingData.h"
 #include "acd52832_bsp.h"
 
-#define SLEEP_TIME      (0.85)            // Sleep time in seconds
-#define AWAKE_TIME      (0.15)
+#define SLEEP_TIME      (0.85)      // Sleep time in seconds WAS 0.85
+#define AWAKE_TIME      (0.15)      // Was 0.15
 #define BUZZER          (p31)
 
 /* Static constants for the BLE example */
-#define MSD_SIZE        (18)
-#define MSD_ID          (0xFF)
-#define MSD_OFFSET      (4)
-#define BUZZ_TIME       (0.4)   // Buzz time in s
+#define MAX_BLE_PACKET_SIZE (31)
+#define MSD_SIZE            (18)
+#define MSD_ID              (0xFF)
+#define BUZZ_TIME           (1.0)   // Buzz time in s
 
 
 bool SLEEP = true;
@@ -40,6 +40,9 @@
     BLE::Instance().gap().startAdvertising();
 }
 
+/**
+ *  The function is called when ticker generates interrupt
+ */
 void turnBuzzOff(void){
     buzzer.write(0.0F);
     turnBuzzOffT.detach();
@@ -89,14 +92,40 @@
     ble.gap().startAdvertising();
 }
 
-void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){    
+
+uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
     uint8_t i=0;
-    if ((params->advertisingData[MSD_OFFSET]) == MSD_ID ){ 
+    uint8_t len;
+    
+    do{
+        len = params->advertisingData[i];
+        i++;
+        if(params->advertisingData[i] == MSD_ID) return i;
+        else i += (len-1);
+    }while(i<MAX_BLE_PACKET_SIZE);
+    
+    return 0;
+}
+
+/**
+ *  Function is called when BLE radio discovers any kind of advertisment 
+ */
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
+    uint8_t i=0;
+    uint8_t msdOffset;
+    
+    msdOffset = findMSDIndex(params);
+    if(msdOffset == 0){
+        // There's no MSD in BLE advertisement data
+        return;
+    }
+    
+    if ((params->advertisingData[msdOffset]) == MSD_ID){ 
         // Follows Manufacturer Specific Data
-        if ((params->advertisingData[MSD_OFFSET+1]) == 0x59){
-            if ((params->advertisingData[MSD_OFFSET+2]) == 0x00){
+        if ((params->advertisingData[msdOffset+1]) == 0x59){
+            if ((params->advertisingData[msdOffset+2]) == 0x00){
                 for(i=0; i<6; i++){
-                    if((params->advertisingData[MSD_OFFSET+i+3]) == my_mac_address[5-i]){
+                    if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i]){
                         continue;
                     }
                     else{
@@ -146,15 +175,12 @@
     
     /* SpinWait for initialization to complete. This is necessary because the
      * BLE object is used in the main loop below. */
-    while (ble.hasInitialized()  == false) { /* spin loop */ }
+    while (ble.hasInitialized()  == false){
+         /* spin loop */ 
+    }
     
     while(true){
-        if(SLEEP){
-            __WFI();
-            }
-        else{
-            ble.waitForEvent();
-            }
+        if(SLEEP) __WFI();
+        else ble.waitForEvent();
     }
-    
 }