Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Revision:
10:fd91664032d8
Parent:
9:2ab2be19add9
Child:
11:92a9fffd5015
--- a/main.cpp	Thu Jul 20 13:44:49 2017 +0000
+++ b/main.cpp	Tue Jul 25 09:22:32 2017 +0000
@@ -12,17 +12,23 @@
 #include "acd52832_bsp.h"
 #include "mma8452.h"
 
-#define DEBUG 1
+#define DEBUG               (1)
+#define DEBUG_ACC           (0)
 
-#define SLEEP_TIME      (2.0)      // Sleep time in seconds WAS 0.85
-#define AWAKE_TIME      (2.0)      // Was 0.15
-#define BUZZER          (p31)
+#define SLEEP_TIME          (2.0)      // Sleep time in seconds WAS 0.85
+#define AWAKE_TIME          (3.0)      // Was 0.15
+#define ADV_TIMER_TIME      (1.0)      // Advertising time (in s)
+#define SCAN_TIMER_TIME     (2.0)      // Scanning time (in s)
+#define FREE_TIME           (0.01)     // Time between end of a scanning and sleep mode
 
 /* Static constants for the BLE example */
 #define MAX_BLE_PACKET_SIZE (31)
 #define MSD_SIZE            (18)
 #define MSD_ID              (0xFF)
 #define BUZZ_TIME           (1.0)   // Buzz time in s
+#define ADV_INTERVAL        (100)   // Advertising interval (in ms)
+#define SCAN_INTERVAL       (100)   // Scan interval (in ms)
+#define SCAN_WINDOW         (50)
 
 /* Static constants for the accelerometer */
 #define WHO_AM_I            0x0D           // Type 'read' : This should return the device id of 0x2A
@@ -30,15 +36,26 @@
 #define I2C_DATA            (p29)
 #define I2C_CLK             (p2)
 #define INT2_PIN            (p4)
+#define BUZZER              (p31)
 
-uint8_t sleepFlag = 1;
+uint8_t sleepFlag = 0;
 int8_t txPower = 4;
 uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
 uint8_t my_mac_address[6] = {};          
 uint8_t buzzer_flag = 0;
 
+enum _radioState{
+    OFF,
+    ADVERTISING,
+    SCANNING
+    };
+enum _radioState radioState = OFF;
+
 void turnBuzzOff(void);
 void goToSleep();
+void startAdvertising();
+void startScanning();
+void WakeMeUp();
 
 Ticker WakeSleepT;
 Ticker turnBuzzOffT;
@@ -49,23 +66,19 @@
 Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
 BLE &ble = BLE::Instance();
 
+
 #if DEBUG
+    DigitalOut advLED(p22);
+    DigitalOut scanLED(p23);
+    DigitalOut awake(p24);
+#endif
+#if DEBUG_ACC
     DigitalOut int_led(p22);
-    DigitalOut awake(p24);
 #endif
 
 /* Restart Advertising on disconnection*/
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
-    BLE::Instance().gap().startAdvertising();
-}
-
-/**
- *  The function is called when ticker generates interrupt
- */
-void turnBuzzOff(void){
-    buzzer.write(0.0F);
-    turnBuzzOffT.detach();
-    WakeSleepT.attach(goToSleep, AWAKE_TIME);
+    //BLE::Instance().gap().startAdvertising();
 }
 
 /**
@@ -104,11 +117,12 @@
     
     
     /* setup advertising */
+    
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);    
-    ble.gap().setAdvertisingInterval(100);  // --> Has to be at least 100ms!
-    ble.gap().startAdvertising();
+    ble.gap().setAdvertisingInterval(ADV_INTERVAL);  // --> Has to be at least 100ms!
+    //ble.gap().startAdvertising();   
 }
 
 
@@ -133,6 +147,7 @@
     uint8_t i=0;
     uint8_t msdOffset;
     
+    ble.gap().stopScan();
     msdOffset = findMSDIndex(params);
     if(msdOffset == 0){
         // There's no MSD in BLE advertisement data
@@ -144,35 +159,89 @@
         if ((params->advertisingData[msdOffset+1]) == 0x59){
             if ((params->advertisingData[msdOffset+2]) == 0x00){
                 for(i=0; i<6; i++){
-                    if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i]){
+                    if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i] || 1){
                         continue;
                     }
                     else{
-                        //return;
+                        return;
                     }
                 }
                 turnBuzzOffT.detach();
                 WakeSleepT.detach();
                 buzzer.write(0.5F);
                 turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME);
+                //wait_ms(40);
+                //buzzer.write(0.0F);
             }
         }
     }
 }
 
-void WakeMeUp(){
-    WakeSleepT.detach();
+/**
+ *  The function is called when ticker generates interrupt
+ */
+void turnBuzzOff(void){
+    buzzer.write(0.0F);
+    turnBuzzOffT.detach();
+    ble.gap().startScan(advertisementCallback);
     WakeSleepT.attach(goToSleep, AWAKE_TIME);
-    
+}
+
+void startAdvertising(){
+    ble.gap().startAdvertising();
+    radioState = ADVERTISING;
+    #if DEBUG
+        advLED = 0;
+        scanLED = 1;
+    #endif
+    WakeSleepT.detach();
+    WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME);    // Call the wakeMeUp function
+}
+
+void startScanning(){
+    ble.gap().stopAdvertising();
+    ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW);
+    ble.gap().setScanTimeout(SCAN_TIMER_TIME);
     ble.gap().startScan(advertisementCallback);
-    ble.gap().startAdvertising();
+    radioState = SCANNING;
+    #if DEBUG
+        advLED = 1;
+        scanLED = 0;
+    #endif
+    WakeSleepT.detach();
+    WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME);
+}
+
+void WakeMeUp(){
     sleepFlag = 0;
+    switch(radioState){
+        case OFF:{
+                startAdvertising();
+                break;
+            }
+        case ADVERTISING:{
+                startScanning();
+                break;
+            }
+        case SCANNING:{
+                radioState = OFF;
+                ble.gap().stopAdvertising();    // Just in case
+                ble.gap().stopScan();
+                WakeSleepT.detach();
+                WakeSleepT.attach(goToSleep, FREE_TIME);
+                #if DEBUG
+                    advLED = 1;
+                    scanLED = 1;
+                #endif
+                break;
+            }
+        default: return;
+    }
 }
 
 void goToSleep(){
     WakeSleepT.detach();
     WakeSleepT.attach(WakeMeUp, SLEEP_TIME);
-    
     ble.gap().stopAdvertising();
     ble.gap().stopScan();
     sleepFlag = 1;
@@ -186,7 +255,7 @@
 }
 
 void pulse_handler(void){
-        #if DEBUG
+        #if DEBUG_ACC
             int_led = !int_led;
         #endif
         i2c_power.write(1.0F);
@@ -196,14 +265,20 @@
         //WakeSleepT.attach(goToSleep, AWAKE_TIME);
 }
 
-int main(void){
+int main(void){   
+    #if DEBUG
+        awake = 1;
+        advLED = 1;
+        scanLED = 1;
+    #endif
     WakeSleepT.attach(goToSleep, AWAKE_TIME);
     ble.init(bleInitComplete);
     ble.gap().setTxPower(txPower);
     GapAdvertisingData postavke = GapAdvertisingData();
     
-    ble.gap().setScanParams(100, 100);
-    ble.gap().startScan(advertisementCallback);
+    //ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW);
+    //ble.gap().setScanTimeout(0.5);
+    //ble.gap().startScan(advertisementCallback);
     
     buzzer.period(0.001F);
     buzzer.write(0.0F);
@@ -237,12 +312,6 @@
     while (ble.hasInitialized()  == false){
          /* spin loop */ 
     }
-    
-    while(1){
-        ble.waitForEvent();
-        awake != awake;
-        wait_ms(100);
-        }
         
     while(true){
         if(sleepFlag){