Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Revision:
27:2c67f07590fd
Parent:
26:148aa2e2460c
Child:
28:7b71c61d2160
Child:
31:caef580f5943
--- a/main.cpp	Fri Sep 08 10:49:50 2017 +0000
+++ b/main.cpp	Wed Sep 13 16:28:32 2017 +0000
@@ -16,24 +16,23 @@
 #include "nrf52_digital.h"
 #include "acn_nrf52_pwm.h"
 
-#define DEBUG               (1)
-#define DEBUG_ACC           (1)
+#define DEBUG               (0)
+#define DEBUG_ACC           (0)
 #define DEBUG_PRINT_UART    (0)
 #define DEBUG_MAC           (0)
 #define DEBUG_CONNECTION    (0)
 #define DEBUG_WAKEUP_BUZZER (0)
 
-#define USE_ACC             (1)
+#define USE_ACC             (0)
 
-#define SLEEP_TIME_S              (8.00)           /* Sleep time (in s)          */
-#define ADV_TIMER_TIME_S          (0.50)           /* Advertising time (in s)    */
+#define SLEEP_TIME_S              (3.00)           /* Sleep time (in s)          */
+#define ADV_TIMER_TIME_S          (1.00)           /* Advertising time (in s)    */
 #define SCAN_TIMER_TIME_S         (0.25)           /* 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 (10)             /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */
-#define BUZZ_PERIOD_US            (250)
-#define BUZZ_DURATION_MS          (1000)
+#define BUZZER_FREQUENCY_Hz       (3000)
 #define MAC_SIZE_B                (6)
 
 /* Static constants for the BLE example */
@@ -54,24 +53,21 @@
 #define INT2_PIN            (p4)
 #define BUZZER              (p31)
 
-#define BUZZER_FREQUENCY_Hz (1500)
-
 #if DEBUG_PRINT_UART
-#include "nrf52_uart.h"
-NRF52_UART uart(p25, p26, Baud9600);
-char buffer[255];
-#define SEND(...) {uint8_t len = sprintf(buffer, __VA_ARGS__); uart.send(buffer, len);}
+    #include "nrf52_uart.h"
+    NRF52_UART uart(p25, p26, Baud9600);
+    char buffer[255];
+    #define SEND(...) {uint8_t len = sprintf(buffer, __VA_ARGS__); uart.send(buffer, len);}
 #else
-#define SEND(...)
+    #define SEND(...)
 #endif
 
 bool beaconStateActive = 1;
 
 bool shushShush = false;
-const static uint16_t ACK_CHARA_UUID = 0xA001;
 
 uint8_t txPower = 4;
-uint8_t sleepFlag = false;
+uint8_t sleepFlag = true;
 uint8_t tempSleepTime = SLEEP_TIME_S;
 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 startBuzz[2] = {0xBA, 0xBE};
@@ -91,14 +87,16 @@
 void startScanning();
 void WakeMeUp();
 
-    
 Ticker WakeSleepT;
 Ticker sleepChanger;
+NRF52_PWM buzzer(NRF_PWM2);
+
 #if USE_ACC
     DigitalOut accPower(p7);
     DigitalOut i2cPower(p5);
     InterruptIn accPulse(INT2_PIN);
     Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
+
 #endif
 BLE &ble = BLE::Instance();
 ACKService<4> *ackServicePtr;
@@ -114,14 +112,16 @@
     NRF52_DigitalOut act_led(p22);
 #endif
 
-NRF52_PWM buzzer(NRF_PWM0);
 
 void buzzerStart(){
-    buzzer.enable(3000);
+    buzzer.enable(BUZZER_FREQUENCY_Hz);
+    //buzzer.enable(1);
     buzzer.enableChannel(0, BUZZER);
-    buzzer.setDuty(0, 0.5f);
+    buzzer.setDuty(0,0.5f);
 }
 void buzzerStop(){
+    buzzer.enable(0);
+    buzzer.setDuty(0, 0);
     buzzer.disable();
 }
 
@@ -192,14 +192,18 @@
         }
         else if(params->data[0] == stopBuzz[0]){
             if(params->data[1] == stopBuzz[1]){
+                buzzerStop();
                 WakeSleepT.detach();
                 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
                 ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
-                buzzerStop();
             }
         }
         else if(params->data[0] == 0x55){
-            beaconStateActive = 0;
+            /* This is used after pairing */
+            radioState = SCANNING;
+            WakeSleepT.detach();
+            WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
+            shushShush = true;
             ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
         }
         
@@ -344,11 +348,6 @@
 }
 
 void startAdvertising(){
-    #if USE_ACC
-        i2cPower = 1;
-    #endif
-    wait_ms(10);
-    
     if(shushShush){
         // Do not advertise! Go to sleep
         WakeSleepT.detach();
@@ -356,13 +355,13 @@
         WakeMeUp();
     }
     else{
-        ble.gap().startAdvertising();
-        #if DEBUG
-            advLED = 0;
-            scanLED = 1;
-        #endif
-        WakeSleepT.detach();
-        WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S);    // Call the wakeMeUp function
+    ble.gap().startAdvertising();
+    #if DEBUG
+        advLED = 0;
+        scanLED = 1;
+    #endif
+    WakeSleepT.detach();
+    WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S);    // Call the wakeMeUp function
     }
 }
 
@@ -417,20 +416,18 @@
 }
 
 #if USE_ACC
-void pulse_handler(){
-#if DEBUG_WAKEUP_BUZZER
-    if(beaconStateActive == 0){
+    void pulse_handler(){
+    #if DEBUG_WAKEUP_BUZZER
         buzzerStart();
-        wait_ms(500);
+        wait_ms(50);
         buzzerStop();
-        beaconStateActive = 1;
+    #endif
+    #if DEBUG_ACC
+        int_led = !int_led;
+    #endif
+    shushShush = false;
     }
 #endif
-#if DEBUG_ACC
-    int_led = !int_led;
-#endif
-}
-#endif
 
 int main(){
     #if DEBUG || DEBUG_MAC
@@ -442,14 +439,13 @@
     #if USE_ACC
         accPower = 1;
         i2cPower = 1;
-    #endif
-    
-    int_led = 0;
+    #endif 
     
     //WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
     GoToSleep();
     ble.init(bleInitComplete);
     ble.gap().setTxPower(txPower);
+    //WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
     
     #if USE_ACC
         // Pulse interrupt detection
@@ -459,14 +455,16 @@
         wait_ms(1);        
         acc.set_register((char)CTRL_REG_5, (char)0x00);        // INT_EN_FF_MT interrupt is router t0 INT2
         wait_ms(1);
-        acc.set_register((char)FF_COUNT, (char)0x08);          // Set Counter degister value (10ms)
+        acc.set_register((char)FF_COUNT, (char)0x01);  //WAS 0x08        // Set Counter degister value (10ms)
         wait_ms(1);
         acc.set_register((char)FF_MT_THS, (char)0x90);         // Set TH value for motion detection on 1 G (1/0.063) and DBCNTM = 1 (Increments or clears counter)
         wait_ms(1);
                 
         // Setup for the interrupt handler
         accPulse.rise(&pulse_handler);                                                                                //  -------------------------------------
-        acc.set_register((char)CTRL_REG_1, (char)0x01);        // Flow data rate and Active mode           
+        //acc.set_register((char)CTRL_REG_1, (char)0x03);       // Flow data rate and Active mode           
+        acc.set_register((char)CTRL_REG_1, (char)0x01);         // Flow data rate and Active mode           
+        acc.set_register((char)CTRL_REG_2, (char)0x03);         // Set Low power mode
         wait(1);
     #endif
     
@@ -475,37 +473,23 @@
     /* 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(true){
-        if(beaconStateActive){
-            // wake up
-            
-            startAdvertising();
-            while(beaconStateActive){
-                if(sleepFlag){
-                    #if USE_ACC
-                        i2cPower = 0;
-                    #endif
-                    ble.waitForEvent();
-                    __WFI();
-                }
-                else{
-                    ble.waitForEvent();
-                }
-            }
-        }
+    buzzerStart();
+    wait_ms(500);
+    
+    // Disconnect and stop PWM2
+    /*
+    NRF_PWM2->PSEL.OUT[0] = 0x80000000;
+    NRF_PWM2->PSEL.OUT[1] = 0x80000000;
+    NRF_PWM2->PSEL.OUT[2] = 0x80000000;
+    NRF_PWM2->PSEL.OUT[3] = 0x80000000;
+    NRF_PWM2->TASKS_STOP = 1;
+    */
+    
+    buzzerStop();
+    
+    
+    while(true){   
         
-        scanLED = 0;
-        // prepare for sleep
-            
-        // disable everything
-        i2cPower = 0;
-        WakeSleepT.detach();
-        // set accelerometer refresh rate to lowest
-        
-        // do not wake up until beaconStateActive is set by the accelerometer
-        while(!beaconStateActive){
-            __WFI();
-        }
-        scanLED = 1;
+        ble.waitForEvent();
     }
 }