Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Revision:
31:caef580f5943
Parent:
27:2c67f07590fd
Child:
32:61d2328ec1a7
--- a/main.cpp	Wed Sep 13 16:28:32 2017 +0000
+++ b/main.cpp	Thu Nov 23 15:49:10 2017 +0000
@@ -53,6 +53,18 @@
 #define INT2_PIN            (p4)
 #define BUZZER              (p31)
 
+typedef struct __attribute__((packed, aligned(1))){
+  uint16_t appleID;
+  uint8_t  secondID;
+  uint8_t  DataSize;
+  uint8_t  UUID[18];
+  uint8_t  major;
+  uint8_t  minor;
+  int8_t   RSSI;
+}IBeaconMSD;
+
+IBeaconMSD ibeaconMSD;
+
 #if DEBUG_PRINT_UART
     #include "nrf52_uart.h"
     NRF52_UART uart(p25, p26, Baud9600);
@@ -91,6 +103,11 @@
 Ticker sleepChanger;
 NRF52_PWM buzzer(NRF_PWM2);
 
+enum advPayloadType{
+    iBEACON = 0,
+    BEACON
+};
+
 #if USE_ACC
     DigitalOut accPower(p7);
     DigitalOut i2cPower(p5);
@@ -254,9 +271,28 @@
     ble.gattServer().onDataWritten(onDataWrittenCallback);
     
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE);
+    
+    /*
+     *  iBeacon
+     */
+    //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t*)&ibeaconMSD, sizeof(ibeaconMSD));
+    
     ble.gap().setAdvertisingInterval(ADV_INTERVAL);  // --> Has to be at least 100ms!
 }
 
+void changeAdvPaylod(advPayloadType newState){
+    if(newState == iBEACON){
+        /*
+         *  iBeacon
+         */
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED);
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t*)&ibeaconMSD, sizeof(ibeaconMSD));
+    }
+    else{
+        ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE);
+    }
+}
 
 uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
     uint8_t i=0;
@@ -348,6 +384,8 @@
 }
 
 void startAdvertising(){
+    static uint8_t cnt = 0;
+    advPayloadType nextState;
     if(shushShush){
         // Do not advertise! Go to sleep
         WakeSleepT.detach();
@@ -355,13 +393,23 @@
         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
+        if(!cnt){
+            nextState = iBEACON;
+            changeAdvPaylod(nextState);
+        }
+        if(cnt==1){
+            nextState = BEACON;
+            changeAdvPaylod(nextState);
+        }
+        cnt = (cnt+1)%5;    // Everyfifth advertising change to iBeacon
+        
+        ble.gap().startAdvertising();
+        #if DEBUG
+            advLED = 0;
+            scanLED = 1;
+        #endif
+        WakeSleepT.detach();
+        WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S);    // Call the wakeMeUp function
     }
 }
 
@@ -445,51 +493,25 @@
     GoToSleep();
     ble.init(bleInitComplete);
     ble.gap().setTxPower(txPower);
-    //WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
-    
-    #if USE_ACC
-        // Pulse interrupt detection
-        acc.set_register((char)CTRL_REG_4, (char)0x04);        //  INT_EN_FF_MT Freefall/motion interrupt enabled
-        wait_ms(1);
-        acc.set_register((char)FF_MT_CFG, (char)0b01011000);   //ELE, Motion Flag ON, YEFE, X Event Flag Enable
-        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)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)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
-    
-    __enable_irq();
-    
     /* 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 */ }
-    
+    __enable_irq();
+            
     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();
     
+    ibeaconMSD.appleID = 0x004C;
+    ibeaconMSD.secondID = 0x02;
+    ibeaconMSD.DataSize = 0x15;
+    ibeaconMSD.UUID[0] = 0x11;
+    ibeaconMSD.UUID[1] = 0x22;
+    ibeaconMSD.UUID[2] = 0x33;
+    ibeaconMSD.RSSI = -4;
+    
+    WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
     
     while(true){   
-        
         ble.waitForEvent();
     }
 }