Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
Diff: main.cpp
- Revision:
- 14:d506c0679c0b
- Parent:
- 13:d51127eed926
- Child:
- 15:934a04c958f5
--- a/main.cpp Wed Jul 26 20:11:45 2017 +0000 +++ b/main.cpp Thu Jul 27 15:04:50 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 (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 */ +#define SLEEP_TIME_S (2.0) /* Sleep time in seconds WAS 0.85 */ +#define ADV_TIMER_TIME_S (1.0) /* Advertising time (in s) */ +#define SCAN_TIMER_TIME_S (2.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 */ +#define SHORT_SLEEP_TIME_S (0.5) /* Shorter sleep time (s) */ +#define SHORT_SLEEP_TIME_PERIOD_S (5) /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */ /* Static constants for the BLE example */ @@ -29,10 +29,10 @@ #define MSD_SIZE (18) #define MSD_ID (0xFF) -#define BUZZ_TIME (2.0) /* Buzz time in s */ +#define BUZZ_TIME_S (0.25) /* 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) +#define SCAN_INTERVAL (SCAN_TIMER_TIME_S) /* Scan interval (in ms) */ +#define SCAN_WINDOW (SCAN_TIMER_TIME_S) /* Static constants for the accelerometer */ @@ -44,22 +44,22 @@ #define BUZZER (p31) uint8_t sleepFlag = 0; -uint8_t tempSleepTime = SLEEP_TIME; +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 my_mac_address[6] = {}; +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; -enum _radioState{ +enum RadioState{ OFF, ADVERTISING, SCANNING }; -enum _radioState radioState = OFF; +enum RadioState radioState = OFF; -void turnBuzzOff(void); -void goToSleep(); -void startAdvertising(); +void TurnBuzzOff(void); +void GoToSleep(); +void StartAdvertising(); void startScanning(); void WakeMeUp(); @@ -68,7 +68,7 @@ Ticker sleepChanger; PwmOut buzzer(BUZZER); PwmOut gyro_power(p7); -PwmOut i2c_power(p5); // I2C Pull-ups power pin +PwmOut i2c_power(p5); /* I2C Pull-ups power pin */ InterruptIn gyro_pulse(INT2_PIN); Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS); BLE &ble = BLE::Instance(); @@ -121,14 +121,14 @@ /* Get my MAC address */ BLEProtocol::AddressType_t temp_address_type; - ble.gap().getAddress(&temp_address_type, my_mac_address); + ble.gap().getAddress(&temp_address_type, myMacAddress); /* setup advertising */ - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); - ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE); - ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); + //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); + //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE); ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms! //ble.gap().startAdvertising(); } @@ -148,43 +148,41 @@ return 0; } +uint8_t CheckMac(const Gap::AdvertisementCallbackParams_t *params, uint8_t *myMacAddress, uint8_t msdOffset){ + int i=0; + + if(msdOffset == 0){ + return 0; // There's no MSD in BLE advertisement data + } + for(i=0; i<6; i++){ + if(!(params->advertisingData[msdOffset+i+3]) == *(myMacAddress+5-i)){ + return 0; + } + } + return 1; +} + /** * Function is called when BLE radio discovers any kind of advertisment */ void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){ - uint8_t i=0; uint8_t msdOffset; msdOffset = findMSDIndex(params); if(msdOffset == 0){ - // There's no MSD in BLE advertisement data - return; + return; // There's no MSD in BLE advertisement data } - - if ((params->advertisingData[msdOffset]) == MSD_ID){ + if ((params->advertisingData[msdOffset]) == MSD_ID){ // Follows Manufacturer Specific Data if ((params->advertisingData[msdOffset+1]) == 0x59){ if ((params->advertisingData[msdOffset+2]) == 0x00){ - for(i=0; i<6; i++){ - if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i]){ - continue; - } - else{ - return; - } + if(CheckMac(params, myMacAddress, msdOffset)){ + ble.gap().stopScan(); + buzzer.write(0.5F); + WakeSleepT.detach(); + turnBuzzOffT.detach(); + turnBuzzOffT.attach(TurnBuzzOff, BUZZ_TIME_S); } - /* - 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); } } } @@ -193,7 +191,7 @@ /* Call this function few minutes (TBD) after a last scanned advertisment */ void changeSleepTime(){ - tempSleepTime = SLEEP_TIME; + tempSleepTime = SLEEP_TIME_S; sleepChanger.detach(); } @@ -201,29 +199,13 @@ /** * The function is called when ticker generates interrupt */ -void turnBuzzOff(void){ +void TurnBuzzOff(void){ buzzer.write(0.0F); - tempSleepTime = SHORT_SLEEP_TIME; + tempSleepTime = SHORT_SLEEP_TIME_S; turnBuzzOffT.detach(); - //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 + sleepChanger.attach(changeSleepTime, SHORT_SLEEP_TIME_PERIOD_S); + WakeSleepT.attach(WakeMeUp, FREE_TIME_S); } void startAdvertising(){ @@ -233,42 +215,39 @@ scanLED = 1; #endif WakeSleepT.detach(); - WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME); // Call the wakeMeUp function + WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function } void startScanning(){ ble.gap().stopAdvertising(); ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW); - ble.gap().setScanTimeout(SCAN_TIMER_TIME); + ble.gap().setScanTimeout(SCAN_TIMER_TIME_S); ble.gap().startScan(advertisementCallback); #if DEBUG advLED = 1; scanLED = 0; #endif WakeSleepT.detach(); - WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME); + WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME_S); } void WakeMeUp(){ sleepFlag = 0; switch(radioState){ case OFF:{ + radioState = ADVERTISING; startAdvertising(); - radioState = ADVERTISING; break; } case ADVERTISING:{ + radioState = SCANNING; startScanning(); - radioState = SCANNING; break; } case SCANNING:{ radioState = OFF; - sleepFlag = 1; - ble.gap().stopAdvertising(); // Just in case - ble.gap().stopScan(); WakeSleepT.detach(); - WakeSleepT.attach(goToSleep, FREE_TIME); + WakeSleepT.attach(GoToSleep, FREE_TIME_S); #if DEBUG advLED = 1; scanLED = 1; @@ -279,7 +258,7 @@ } } -void goToSleep(){ +void GoToSleep(){ WakeSleepT.detach(); WakeSleepT.attach(WakeMeUp, tempSleepTime); ble.gap().stopAdvertising(); @@ -311,7 +290,8 @@ advLED = 1; scanLED = 1; #endif - WakeSleepT.attach(goToSleep, AWAKE_TIME); + + WakeSleepT.attach(GoToSleep, AWAKE_TIME_S); ble.init(bleInitComplete); ble.gap().setTxPower(txPower); GapAdvertisingData postavke = GapAdvertisingData();