Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Revision:
17:51a5456a46cd
Parent:
16:a338d2417fd5
Child:
18:e844d3e6ab88
--- a/main.cpp	Thu Aug 10 11:02:02 2017 +0000
+++ b/main.cpp	Thu Aug 10 13:31:28 2017 +0000
@@ -16,15 +16,17 @@
 
 #define DEBUG               (1)
 #define DEBUG_ACC           (0)
-#define PRINT               (1)
+#define PRINT               (0)
+#define DEBUG_MAC           (0)
+#define DEBUG_CONNECTION    (0)
 
-#define SLEEP_TIME_S              (1.0)           /* Sleep time in seconds WAS 0.85 */
-#define ADV_TIMER_TIME_S          (4.0)           /* Advertising time (in s) */
-#define SCAN_TIMER_TIME_S         (2.0)           /* Scanning time (in s) */
+#define SLEEP_TIME_S              (2.0)           /* Sleep time in seconds WAS 0.85 */
+#define ADV_TIMER_TIME_S          (2.0)           /* Advertising time (in s) */
+#define SCAN_TIMER_TIME_S         (1.0)           /* Scanning time (in s) */
 #define FREE_TIME_S               (0.1)           /* Time between end of a scanning and sleep mode */
 #define AWAKE_TIME_S              (ADV_TIMER_TIME_S+SCAN_TIMER_TIME_S+FREE_TIME_S)      /* Was 0.15 */
 #define SHORT_SLEEP_TIME_S        (0.5)       /* Shorter sleep time (s) */
-#define SHORT_SLEEP_TIME_PERIOD_S (5)        /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */
+#define SHORT_SLEEP_TIME_PERIOD_S (10)        /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */
 #define MAC_SIZE_B                (6)
 
 /* Static constants for the BLE example */
@@ -32,7 +34,7 @@
 #define MSD_SIZE            (18)
 #define MSD_ID              (0xFF)
 
-#define BUZZ_TIME_S          (0.25)   /* Buzz time in s */
+#define BUZZ_TIME_S         (1)   /* Buzz time in s */
 #define ADV_INTERVAL        (100)   /* Advertising interval (in ms) */
 #define SCAN_INTERVAL       (SCAN_TIMER_TIME_S)   /* Scan interval (in ms) */
 #define SCAN_WINDOW         (SCAN_TIMER_TIME_S)
@@ -101,38 +103,43 @@
 
 
 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
-    scanLED = !scanLED;       // Blue
-    wait_ms(100);
-    scanLED = !scanLED;       // Blue
-    wait_ms(100);
-    scanLED = !scanLED;       // Blue
-    wait_ms(100);
-    scanLED = !scanLED;       // Blue
-    wait_ms(100);
-    scanLED = !scanLED;       // Blue
-    wait_ms(100);
-    scanLED = !scanLED;       // Blue
-    wait_ms(100);
-    scanLED = 1;       // Blue
-    
-    //WakeSleepT.detach();
+    #if DEBUG_CONNECTION
+        scanLED = !scanLED;       // Blue
+        wait_ms(100);
+        scanLED = !scanLED;       // Blue
+        wait_ms(100);
+        scanLED = !scanLED;       // Blue
+        wait_ms(100);
+        scanLED = !scanLED;       // Blue
+        wait_ms(100);
+        scanLED = !scanLED;       // Blue
+        wait_ms(100);
+        scanLED = !scanLED;       // Blue
+        wait_ms(100);
+        scanLED = 1;              // Blue
+    #endif
+    WakeSleepT.detach();
+    sleepFlag = false;
 }
 
 
 /* Restart Advertising on disconnection*/
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
-    shushShush = true;
-    advLED = !advLED;       // RED
-    wait_ms(100);
-    advLED = !advLED;
-    wait_ms(100);
-    advLED = !advLED;
-    wait_ms(100);
-    advLED = !advLED;
-    wait_ms(100);
-    advLED = !advLED;
-    wait_ms(100);
-    advLED = 1;
+    #if DEBUG_CONNECTION
+        advLED = !advLED;       // RED
+        wait_ms(100);
+        advLED = !advLED;
+        wait_ms(100);
+        advLED = !advLED;
+        wait_ms(100);
+        advLED = !advLED;
+        wait_ms(100);
+        advLED = 1;
+        wait_ms(100);
+        advLED = 1;
+    #endif
+    WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
+    sleepFlag = true;
 }
 
 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
@@ -140,18 +147,25 @@
         // Something is written into AckCharacteristic
         if(params->data[0] == 0xBA)
             if(params->data[1] == 0xBE){
-                connectedLED = !connectedLED;       // BLUE
-                wait_ms(100);
-                connectedLED = !connectedLED;
-                wait_ms(100);
-                connectedLED = !connectedLED;
-                wait_ms(100);
-                connectedLED = !connectedLED;
-                wait_ms(100);
-                connectedLED = !connectedLED;
-                wait_ms(100);
-                connectedLED = 1;
-                wait_ms(100);
+                #if DEBUG_CONNECTION
+                    connectedLED = !connectedLED;       // BLUE
+                    wait_ms(100);
+                    connectedLED = !connectedLED;
+                    wait_ms(100);
+                    connectedLED = !connectedLED;
+                    wait_ms(100);
+                    connectedLED = !connectedLED;
+                    wait_ms(100);
+                    connectedLED = !connectedLED;
+                    wait_ms(100);
+                    connectedLED = 1;
+                    wait_ms(100);
+                #endif    
+                buzzer.period(0.0009F);            
+                buzzer.write(0.5F);
+                WakeSleepT.detach();
+                turnBuzzOffT.detach();
+                turnBuzzOffT.attach(TurnBuzzOff, BUZZ_TIME_S);
                 /*
                     This function should make advertiser stop
                 */
@@ -160,9 +174,7 @@
             }
     }
     else{
-        // Execute this for wrong data written into characteristic
-        wait_ms(500);
-        
+        // Execute this for wrong data written into characteristic   
     }
 }
 
@@ -202,8 +214,8 @@
     ackServicePtr->updateMacAddress(myMacAddress);    // Update MAC address
     ble.gap().onDisconnection(disconnectionCallback);
     
-    //ble.gap().onConnection(onConnectionCallback);         // -->> Uncomment these two lines for shush-shush 
-    //ble.gattServer().onDataWritten(onDataWrittenCallback);
+    ble.gap().onConnection(onConnectionCallback);         // -->> Uncomment these two lines for shush-shush 
+    ble.gattServer().onDataWritten(onDataWrittenCallback);
     
     
     /* setup advertising */
@@ -220,6 +232,20 @@
     uint8_t i=0;
     uint8_t len;
     uint8_t min = params->advertisingDataLen;
+    if(min == 13){
+        advLED = 0;
+        wait_ms(50);
+        advLED = !advLED;
+        wait_ms(50);
+        advLED = !advLED;
+        wait_ms(50);
+        advLED = !advLED;
+        wait_ms(50);
+        advLED = !advLED;
+        wait_ms(50);
+        advLED = 1;
+        wait_ms(50);
+    }
     if((params->advertisingDataLen < (MAC_SIZE_B + 2)) || params->advertisingDataLen == 0){
         // Empty advertisement or not long enough for MAX
         return 0;
@@ -243,27 +269,33 @@
     ble.gap().getAddress(&temp_address_type, myMacAddress);
     
     if(msdOffset == 0){
+        #if DEBUG_MAC
         for(i=0; i<10; i++){
             scanLED = !scanLED; // BLUE
             wait_ms(100);
         }
+        #endif
         return 0;     // There's no MSD in BLE advertisement data
     }
     for(i=0; i<6; i++){
         //if((params->advertisingData[0+i+4]) != myMacAddress[i]){
         //if(params->advertisingData[4] != myMacAddress[4]){
         if(params->advertisingData[msdOffset + 3 + i] != myMacAddress[5-i]){      // myMacAddress[0] == 0x91
-            for(i=0; i<10; i++){
-                connectedLED = !connectedLED; // Green
-                wait_ms(100);
-            }
+            #if DEBUG_MAC
+                for(i=0; i<10; i++){
+                    connectedLED = !connectedLED; // Green
+                    wait_ms(100);
+                }
+            #endif
             return 0;
         }
     }       
-    for(i=0; i<10; i++){
-        advLED = !advLED; // RED
-        wait_ms(100);
-    }
+    #if DEBUG_MAC
+        for(i=0; i<10; i++){
+            advLED = !advLED; // RED
+            wait_ms(100);
+        }
+    #endif
     return 1;
 }
 
@@ -273,8 +305,8 @@
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
     uint8_t msdOffset, i;
                     
-    //msdOffset = findMSDIndex(params);
-    msdOffset = 1;  // 1 or 4
+    msdOffset = findMSDIndex(params);
+    //msdOffset = 1;  // 1 or 4
     if(msdOffset == 0){
         return;     // There's no MSD in BLE advertisement data
     }
@@ -290,7 +322,8 @@
                 }
                 */
                 if(CheckMac(params, myMacAddress, msdOffset)){
-                    ble.gap().stopScan();
+                    //ble.gap().stopScan();
+                    buzzer.period(0.0009F);
                     buzzer.write(0.5F);
                     WakeSleepT.detach();
                     turnBuzzOffT.detach();
@@ -313,6 +346,7 @@
  *  The function is called when ticker generates interrupt
  */
 void TurnBuzzOff(void){
+    buzzer.period(0.00F);
     buzzer.write(0.0F);
     tempSleepTime = SHORT_SLEEP_TIME_S;
     turnBuzzOffT.detach();
@@ -330,6 +364,8 @@
     }
     else{
         ble.gap().startAdvertising();
+        buzzer.period(0.00F);
+        buzzer.write(0.0F);
         #if DEBUG
             advLED = 0;
             scanLED = 1;
@@ -388,12 +424,6 @@
     #endif
 }
 
-void buzz(void){
-        buzzer.write(0.5f);
-        wait_ms(100);
-        buzzer.write(0.0f);
-        sleepFlag = 0;
-}
 
 void pulse_handler(void){
         #if DEBUG_ACC