Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Revision:
13:d51127eed926
Parent:
12:6b072c2a061c
Child:
14:d506c0679c0b
--- a/main.cpp	Tue Jul 25 12:19:39 2017 +0000
+++ b/main.cpp	Wed Jul 26 20:11:45 2017 +0000
@@ -15,13 +15,13 @@
 #define DEBUG               (1)
 #define DEBUG_ACC           (0)
 
-#define SLEEP_TIME          (2.0)      // Sleep time in seconds WAS 0.85
-#define ADV_TIMER_TIME      (0.1)      // Advertising time (in s)
-#define SCAN_TIMER_TIME     (0.5)      // Scanning time (in s)
-#define FREE_TIME           (0.01)     // Time between end of a scanning and sleep mode
-#define AWAKE_TIME          (ADV_TIMER_TIME+SCAN_TIMER_TIME+FREE_TIME)      // Was 0.15
-#define SHORT_SLEEP_TIME        (0.5)      // Shorter sleep time (s) 
-#define SHORT_SLEEP_TIME_PERIOD (60)       // Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME
+#define SLEEP_TIME          (2.0)           /* Sleep time in seconds WAS 0.85 */
+#define ADV_TIMER_TIME      (1.1)           /* Advertising time (in s) */
+#define SCAN_TIMER_TIME     (1.5)           /* Scanning time (in s) */
+#define FREE_TIME           (1.01)          /* Time between end of a scanning and sleep mode */
+#define AWAKE_TIME          (ADV_TIMER_TIME+SCAN_TIMER_TIME+FREE_TIME)      /* Was 0.15 */
+#define SHORT_SLEEP_TIME        (2.5)      /* Shorter sleep time (s) */
+#define SHORT_SLEEP_TIME_PERIOD (60)       /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */
 
 
 /* Static constants for the BLE example */
@@ -29,15 +29,15 @@
 #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)
+#define BUZZ_TIME           (2.0)   /* Buzz time in s */
+#define ADV_INTERVAL        (100)   /* Advertising interval (in ms) */
+#define SCAN_INTERVAL       (SCAN_TIMER_TIME)   /* Scan interval (in ms) */
+#define SCAN_WINDOW         (SCAN_TIMER_TIME)
 
 
 /* Static constants for the accelerometer */
-#define WHO_AM_I            0x0D           // Type 'read' : This should return the device id of 0x2A
-#define OUT_Z_MSB           0x05           // Type 'read' : z axis - 8 most significatn bit of a 12 bit sample
+#define WHO_AM_I            0x0D           /* Type 'read' : This should return the device id of 0x2A */
+#define OUT_Z_MSB           0x05           /* Type 'read' : z axis - 8 most significatn bit of a 12 bit sample */
 #define I2C_DATA            (p29)
 #define I2C_CLK             (p2)
 #define INT2_PIN            (p4)
@@ -155,7 +155,6 @@
     uint8_t i=0;
     uint8_t msdOffset;
     
-    ble.gap().stopScan();
     msdOffset = findMSDIndex(params);
     if(msdOffset == 0){
         // There's no MSD in BLE advertisement data
@@ -174,10 +173,18 @@
                         return;
                     }
                 }
+                /*
+                ble.gap().stopScan();
                 turnBuzzOffT.detach();
                 WakeSleepT.detach();
                 buzzer.write(0.5F);
                 turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME);
+                */
+                buzzer.write(0.5F);
+                WakeSleepT.detach();
+                turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME);
+                //wait_ms(100);
+                //buzzer.write(0.0F);
             }
         }
     }
@@ -198,14 +205,29 @@
     buzzer.write(0.0F);
     tempSleepTime = SHORT_SLEEP_TIME;
     turnBuzzOffT.detach();
-    ble.gap().startScan(advertisementCallback);
-    WakeSleepT.attach(goToSleep, AWAKE_TIME);
+    //ble.gap().startScan(advertisementCallback);
+    //WakeSleepT.attach(WakeMeUp, AWAKE_TIME);
+    WakeSleepT.detach();
+    radioState = SCANNING;
+    //WakeMeUp();
+    
     sleepChanger.attach(changeSleepTime, SHORT_SLEEP_TIME_PERIOD);
+    
+    radioState = OFF;
+    sleepFlag = 1;
+    ble.gap().stopAdvertising();    // Just in case
+    ble.gap().stopScan();
+    WakeSleepT.detach();
+    //WakeSleepT.attach(goToSleep, FREE_TIME);
+    WakeSleepT.attach(WakeMeUp, tempSleepTime);
+    #if DEBUG
+        advLED = 1;
+        scanLED = 1;
+    #endif
 }
 
 void startAdvertising(){
     ble.gap().startAdvertising();
-    radioState = ADVERTISING;
     #if DEBUG
         advLED = 0;
         scanLED = 1;
@@ -219,7 +241,6 @@
     ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW);
     ble.gap().setScanTimeout(SCAN_TIMER_TIME);
     ble.gap().startScan(advertisementCallback);
-    radioState = SCANNING;
     #if DEBUG
         advLED = 1;
         scanLED = 0;
@@ -233,14 +254,17 @@
     switch(radioState){
         case OFF:{
                 startAdvertising();
+                radioState = ADVERTISING;
                 break;
             }
         case ADVERTISING:{
                 startScanning();
+                radioState = SCANNING;
                 break;
             }
         case SCANNING:{
                 radioState = OFF;
+                sleepFlag = 1;
                 ble.gap().stopAdvertising();    // Just in case
                 ble.gap().stopScan();
                 WakeSleepT.detach();
@@ -292,10 +316,6 @@
     ble.gap().setTxPower(txPower);
     GapAdvertisingData postavke = GapAdvertisingData();
     
-    //ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW);
-    //ble.gap().setScanTimeout(0.5);
-    //ble.gap().startScan(advertisementCallback);
-    
     buzzer.period(0.0009F);
     buzzer.write(0.0F);
     gyro_power.period(0.01F);
@@ -317,7 +337,7 @@
     wait_ms(1);
             
     /* Setup for the interrupt handler */
-    //gyro_pulse.rise(&pulse_handler);                                                                                //  -------------------------------------
+    gyro_pulse.rise(&pulse_handler);                                                                                //  -------------------------------------
     acc.set_register((char)CTRL_REG_1, (char) 0x01);        // Flow data rate and Active mode           
     wait(1);