Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Revision:
18:e844d3e6ab88
Parent:
17:51a5456a46cd
Child:
19:abf14a5ada93
--- a/main.cpp	Thu Aug 10 13:31:28 2017 +0000
+++ b/main.cpp	Fri Aug 11 12:08:24 2017 +0000
@@ -20,8 +20,8 @@
 #define DEBUG_MAC           (0)
 #define DEBUG_CONNECTION    (0)
 
-#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 SLEEP_TIME_S              (1.0)           /* Sleep time in seconds WAS 0.85 */
+#define ADV_TIMER_TIME_S          (3.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 */
@@ -35,7 +35,7 @@
 #define MSD_ID              (0xFF)
 
 #define BUZZ_TIME_S         (1)   /* Buzz time in s */
-#define ADV_INTERVAL        (100)   /* Advertising interval (in ms) */
+#define ADV_INTERVAL        (200)   /* Advertising interval (in ms) */
 #define SCAN_INTERVAL       (SCAN_TIMER_TIME_S)   /* Scan interval (in ms) */
 #define SCAN_WINDOW         (SCAN_TIMER_TIME_S)
 
@@ -58,12 +58,11 @@
 
 
 bool shushShush = false;
-const static uint16_t ACK_SERVICE_UUID = 0xCBE0;
-const static uint16_t ACK_CHARA_UUID = 0xCBE1;
+const static uint16_t ACK_CHARA_UUID = 0xA001;
 
-uint8_t sleepFlag = 0;
+uint8_t txPower = 4;
+uint8_t sleepFlag = false;
 uint8_t tempSleepTime = SLEEP_TIME_S;
-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 myMacAddress[6] = {};          
 uint8_t buzzer_flag = 0;
@@ -92,17 +91,19 @@
 BLE &ble = BLE::Instance();
 ACKService<4> *ackServicePtr;
 
-#if DEBUG
+#if DEBUG || DEBUG_MAC
     DigitalOut advLED(p22);     // Red
     DigitalOut scanLED(p23);    // Blue
     DigitalOut connectedLED(p24);  // Green
 #endif
+
 #if DEBUG_ACC
     DigitalOut int_led(p22);
 #endif
 
 
 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
+    
     #if DEBUG_CONNECTION
         scanLED = !scanLED;       // Blue
         wait_ms(100);
@@ -140,6 +141,7 @@
     #endif
     WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
     sleepFlag = true;
+    
 }
 
 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
@@ -230,33 +232,21 @@
 
 uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
     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){
+    uint8_t advLen = params->advertisingDataLen;
+    uint8_t dataLen;
+    
+    if((advLen < (MAC_SIZE_B + 2)) || advLen == 0){
         // Empty advertisement or not long enough for MAX
+        // +2 for SIZE and MSD ID
         return 0;
     }
     
     do{
-        len = params->advertisingData[i];
+        dataLen = params->advertisingData[i];
         i++;
         if(params->advertisingData[i] == MSD_ID) return i;
-        else i += (len-1);
-    }while(i<min);
+        else i += (dataLen);
+    }while(i<advLen);
     
     return 0;
 }
@@ -268,7 +258,7 @@
     BLEProtocol::AddressType_t temp_address_type;
     ble.gap().getAddress(&temp_address_type, myMacAddress);
     
-    if(msdOffset == 0){
+    if(!msdOffset){
         #if DEBUG_MAC
         for(i=0; i<10; i++){
             scanLED = !scanLED; // BLUE
@@ -295,6 +285,7 @@
             advLED = !advLED; // RED
             wait_ms(100);
         }
+        advLED = 1;
     #endif
     return 1;
 }
@@ -305,8 +296,7 @@
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
     uint8_t msdOffset, i;
                     
-    msdOffset = findMSDIndex(params);
-    //msdOffset = 1;  // 1 or 4
+    msdOffset = findMSDIndex(params);       // Should be 1 or 4
     if(msdOffset == 0){
         return;     // There's no MSD in BLE advertisement data
     }
@@ -413,6 +403,8 @@
 }
 
 void GoToSleep(){
+    buzzer.period(0.00F);
+    buzzer.write(0.0f);
     WakeSleepT.detach();
     WakeSleepT.attach(WakeMeUp, tempSleepTime);
     ble.gap().stopAdvertising();
@@ -430,14 +422,15 @@
             int_led = !int_led;
         #endif
         i2c_power.write(1.0F);
-        buzzer_flag = 1;
+        buzzer.period(0.0009F);
+        buzzer.write(0.5f);
         // Be awake some time
-        //WakeSleepT.detach();
-        //WakeSleepT.attach(goToSleep, AWAKE_TIME);
+        WakeSleepT.detach();
+        WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
 }
 
 int main(void){   
-    #if DEBUG
+    #if DEBUG || DEBUG_MAC
         advLED = 1;
         scanLED = 1;
         connectedLED = 1;
@@ -450,16 +443,16 @@
     i2c_power.period(0.01F);
     i2c_power.write(1.0F);
     
-    int i;
-    for(i=0; i<10; i++){
     #if PRINT
-        printBuffer[0] = 'B';
-        printBuffer[1] = 'o';
-        printBuffer[2] = 'k';
-        uart.send(printBuffer, 3);
+        int i;
+        for(i=0; i<10; i++){
+            printBuffer[0] = 'B';
+            printBuffer[1] = 'o';
+            printBuffer[2] = 'k';
+            uart.send(printBuffer, 3);
+        wait_ms(100);
+        }
     #endif
-    wait_ms(100);
-    }
     
     WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
     ble.init(bleInitComplete);
@@ -483,8 +476,7 @@
     /* Setup for the interrupt handler */
     //gyro_pulse.rise(&pulse_handler);                                                                                //  -------------------------------------
     //acc.set_register((char)CTRL_REG_1, (char) 0x01);        // Flow data rate and Active mode           
-    wait(1);
-    
+    //wait(1);
     __enable_irq();
     
     /* SpinWait for initialization to complete. This is necessary because the
@@ -495,11 +487,11 @@
         
     while(true){
         if(sleepFlag){
-            //if(!awake) awake = 1;
+            //if(!advLED) advLED = 1;
             __WFI();
         }
         else{
-            //if(awake) awake = 0;
+            //if(advLED) advLED = 0;
             ble.waitForEvent();
         }
     }