Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
Diff: main.cpp
- Revision:
- 34:9856c51ec646
- Parent:
- 33:449fcc43b287
- Child:
- 35:7917a7f951c7
--- a/main.cpp Mon Nov 27 15:24:13 2017 +0000
+++ b/main.cpp Tue Jan 09 09:34:13 2018 +0000
@@ -16,34 +16,26 @@
#include "nrf52_digital.h"
#include "acn_nrf52_pwm.h"
-#define DEBUG (1)
+#define DEBUG (0)
#define DEBUG_ACC (0)
#define DEBUG_PRINT_UART (0)
#define DEBUG_MAC (0)
-#define DEBUG_CONNECTION (1)
+#define DEBUG_CONNECTION (0)
#define DEBUG_WAKEUP_BUZZER (0)
#define USE_ACC (0)
-#define SLEEP_TIME_S (3.00) /* Sleep time (in s) */
+#define SLEEP_TIME_S (4.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 AWAKE_TIME_S (ADV_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 BUZZER_FREQUENCY_Hz (3000)
-#define MAC_SIZE_B (6)
-
-/* Static constants for the BLE example */
-#define MAX_BLE_PACKET_SIZE (31)
-#define MSD_SIZE (16)
-#define MSD_ID (0xFF)
+#define BUZZER_FREQUENCY_Hz (4000)
#define BUZZ_TIME_S (1) /* Buzz time in s */
#define ADV_INTERVAL (100) /* Advertising interval (in ms) */
-#define SCAN_INTERVAL (SCAN_TIMER_TIME_S) /* Scan interval (in ms) */
-#define SCAN_WINDOW (SCAN_TIMER_TIME_S)
+#define UUID_SIZE_B (16)
/* Static constants for the accelerometer */
#define WHO_AM_I 0x0D /* Type 'read' : This should return the device id of 0x2A */
@@ -52,19 +44,6 @@
#define I2C_CLK (p2)
#define INT2_PIN (p4)
#define BUZZER (p31)
-#define USE_IBEACON_ONLY (1)
-
-typedef struct __attribute__((packed, aligned(1))){
- uint16_t appleID;
- uint8_t secondID;
- uint8_t DataSize;
- uint8_t UUID[10]; // was 18
- uint8_t major;
- uint8_t minor;
- int8_t RSSI;
-}IBeaconMSD;
-
-IBeaconMSD ibeaconMSD;
#if DEBUG_PRINT_UART
#include "nrf52_uart.h"
@@ -75,16 +54,9 @@
#define SEND(...)
#endif
-bool beaconStateActive = 1;
-bool shushShush = false;
-
-uint8_t txPower = 4;
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 msd[MSD_SIZE] = {0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
-uint8_t tempUUID[2] = {0x00, 0x59};
+uint8_t UUID[UUID_SIZE_B] = {0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
uint8_t startBuzz[2] = {0xBA, 0xBE};
uint8_t stopBuzz[2] = {0xDE, 0xAD};
uint8_t myMacAddress[6] = {};
@@ -95,6 +67,7 @@
ADVERTISING,
SCANNING,
};
+
enum RadioState radioState = OFF;
void GoToSleep();
@@ -106,11 +79,6 @@
Ticker sleepChanger;
NRF52_PWM buzzer(NRF_PWM2);
-enum advPayloadType{
- iBEACON = 0,
- BEACON
-};
-
#if USE_ACC
DigitalOut accPower(p7);
DigitalOut i2cPower(p5);
@@ -135,7 +103,6 @@
void buzzerStart(){
buzzer.enable(BUZZER_FREQUENCY_Hz);
- //buzzer.enable(1);
buzzer.enableChannel(0, BUZZER);
buzzer.setDuty(0,0.5f);
}
@@ -218,15 +185,6 @@
ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
}
}
- else if(params->data[0] == 0x55){
- /* This is used after pairing */
- radioState = SCANNING;
- WakeSleepT.detach();
- WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
- shushShush = true;
- ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
- }
-
}
else{
// Execute this for wrong data written into characteristic
@@ -270,190 +228,37 @@
ackServicePtr->updateMacAddress(myMacAddress); // Update MAC address
ble.gap().onDisconnection(disconnectionCallback);
- ble.gap().onConnection(onConnectionCallback); // -->> Uncomment these two lines for shush-shush
+ ble.gap().onConnection(onConnectionCallback);
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, 2);
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS , (uint8_t*)msd, MSD_SIZE);
-
+ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS , (uint8_t*)UUID, sizeof(UUID));
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));
- //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS , (uint8_t*)msd, MSD_SIZE);
- //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::LE_GENERAL_DISCOVERABLE | GapAdvertisingData::BREDR_NOT_SUPPORTED);
- //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t*)&ibeaconMSD, 2);
- ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS , (uint8_t*)msd, MSD_SIZE);
-
- }
- 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;
- 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{
- dataLen = params->advertisingData[i];
- i++;
- if(params->advertisingData[i] == MSD_ID) return i;
- else i += (dataLen);
- }while(i<advLen);
-
- return 0;
-}
-
-uint8_t CheckMac(const Gap::AdvertisementCallbackParams_t *params, uint8_t *myMacAddress, uint8_t msdOffset){
- int i=0;
-
- /* Get my MAC address */
- BLEProtocol::AddressType_t temp_address_type;
- ble.gap().getAddress(&temp_address_type, myMacAddress);
-
- if(!msdOffset){
- #if DEBUG_MAC
- for(i=0; i<10; i++){
- scanLED = !scanLED; // BLUE
- wait_ms(100);
- }
- #endif
- return 0; // There's no MSD in BLE advertisement data
- }
- for(i=0; i<6; i++){
- if(params->advertisingData[msdOffset + 3 + i] != myMacAddress[5-i]){ // myMacAddress[0] == 0x91
- #if DEBUG_MAC
- for(i=0; i<10; i++){
- connectedLED = !connectedLED; // Green
- wait_ms(100);
- }
- #endif
- return 0;
- }
- }
- #if DEBUG_MAC
- for(i=0; i<10; i++){
- advLED = !advLED; // RED
- wait_ms(100);
- }
- advLED = 1;
- #endif
- return 1;
-}
-
-/**
- * Function is called when BLE radio discovers any kind of advertisment
- */
-void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
- uint8_t msdOffset;
-
- msdOffset = findMSDIndex(params); // Should be 1 or 4
- if(msdOffset == 0){
- return; // There's no MSD in BLE advertisement data
- }
- if ((params->advertisingData[msdOffset]) == MSD_ID){
- // Follows Manufacturer Specific Data
- if ((params->advertisingData[msdOffset+1]) == 0x59){
- if ((params->advertisingData[msdOffset+2]) == 0x00){
- if(CheckMac(params, myMacAddress, msdOffset)){
- //ble.gap().stopScan();
- WakeSleepT.detach();
- WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
- }
- }
- }
- }
-}
-/* Call this function few minutes (TBD) after a last scanned advertisment */
-void changeSleepTime(){
- tempSleepTime = SLEEP_TIME_S;
- sleepChanger.detach();
-}
-
-void startAdvertising(){
- static uint8_t cnt = 0;
- advPayloadType nextState;
- if(shushShush){
- // Do not advertise! Go to sleep
- WakeSleepT.detach();
- ble.gap().stopAdvertising();
- WakeMeUp();
- }
- else{
- if(USE_IBEACON_ONLY || !cnt){
- nextState = iBEACON;
- changeAdvPaylod(nextState);
- }
- else if(cnt==1){
- nextState = BEACON;
- changeAdvPaylod(nextState);
- }
- cnt = (cnt+1)%5; // Every fifth 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
- }
-}
-
-void startScanning(){
- ble.gap().stopAdvertising();
- ble.gap().setScanInterval(SCAN_INTERVAL);
- ble.gap().setScanWindow(SCAN_WINDOW);
- ble.gap().setScanParams();
- ble.gap().startScan(advertisementCallback);
+void startAdvertising(){
+ ble.gap().startAdvertising();
#if DEBUG
- advLED = 1;
- scanLED = 0;
+ advLED = 0;
+ scanLED = 1;
#endif
WakeSleepT.detach();
- WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME_S);
+ WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function
}
void WakeMeUp(){
- sleepFlag = 0;
+ sleepFlag = false;
switch(radioState){
case OFF:{
- radioState = SCANNING;
+ radioState = ADVERTISING;
startAdvertising();
break;
}
case ADVERTISING:{
- radioState = SCANNING;
- startScanning();
- break;
- }
- case SCANNING:{
radioState = OFF;
WakeSleepT.detach();
- //WakeSleepT.attach(GoToSleep, FREE_TIME_S);
- GoToSleep();
+ WakeSleepT.attach(GoToSleep, FREE_TIME_S);
break;
}
default: return;
@@ -462,10 +267,9 @@
void GoToSleep(){
WakeSleepT.detach();
- WakeSleepT.attach(WakeMeUp, tempSleepTime);
+ WakeSleepT.attach(WakeMeUp, SLEEP_TIME_S);
ble.gap().stopAdvertising();
- ble.gap().stopScan();
- sleepFlag = 1;
+ sleepFlag = true;
#if DEBUG
advLED = 1;
scanLED = 1;
@@ -482,7 +286,6 @@
#if DEBUG_ACC
int_led = !int_led;
#endif
- shushShush = false;
}
#endif
@@ -498,10 +301,7 @@
i2cPower = 1;
#endif
- //WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
- GoToSleep();
ble.init(bleInitComplete);
- ble.gap().setTxPower(txPower);
/* 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();
@@ -509,15 +309,7 @@
buzzerStart();
wait_ms(500);
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){

