Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
Diff: main.cpp
- Revision:
- 19:abf14a5ada93
- Parent:
- 18:e844d3e6ab88
- Child:
- 20:d534b9c18482
- Child:
- 21:10c3b8176be0
--- a/main.cpp Fri Aug 11 12:08:24 2017 +0000 +++ b/main.cpp Tue Aug 22 07:35:03 2017 +0000 @@ -14,19 +14,20 @@ #include "AckService.h" #include "nrf52_uart.h" -#define DEBUG (1) +#define DEBUG (0) #define DEBUG_ACC (0) #define PRINT (0) #define DEBUG_MAC (0) #define DEBUG_CONNECTION (0) +#define USE_ACC (0) -#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 SLEEP_TIME_S (3.0) /* Sleep time (in s) */ +#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 */ -#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 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 MAC_SIZE_B (6) /* Static constants for the BLE example */ @@ -84,17 +85,19 @@ Ticker turnBuzzOffT; Ticker sleepChanger; PwmOut buzzer(BUZZER); -PwmOut gyro_power(p7); -PwmOut i2c_power(p5); /* I2C Pull-ups power pin */ -InterruptIn gyro_pulse(INT2_PIN); +#if USE_ACC + DigitalOut accPower(p7); + DigitalOut i2cPower(p5); + InterruptIn accPulse(INT2_PIN); +#endif Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS); BLE &ble = BLE::Instance(); ACKService<4> *ackServicePtr; #if DEBUG || DEBUG_MAC - DigitalOut advLED(p22); // Red - DigitalOut scanLED(p23); // Blue - DigitalOut connectedLED(p24); // Green + DigitalOut advLED(p22); // Red + DigitalOut scanLED(p23); // Blue + DigitalOut connectedLED(p24); // Green #endif #if DEBUG_ACC @@ -221,8 +224,6 @@ /* setup advertising */ - //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(); @@ -294,7 +295,7 @@ * Function is called when BLE radio discovers any kind of advertisment */ void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){ - uint8_t msdOffset, i; + uint8_t msdOffset; msdOffset = findMSDIndex(params); // Should be 1 or 4 if(msdOffset == 0){ @@ -304,13 +305,6 @@ // 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]) != myMacAddress[5-i]){ - return; - } - } - */ if(CheckMac(params, myMacAddress, msdOffset)){ //ble.gap().stopScan(); buzzer.period(0.0009F); @@ -338,6 +332,9 @@ void TurnBuzzOff(void){ buzzer.period(0.00F); buzzer.write(0.0F); + NRF_PWM0->TASKS_STOP = 1; + NRF_PWM1->TASKS_STOP = 1; + NRF_PWM2->TASKS_STOP = 1; tempSleepTime = SHORT_SLEEP_TIME_S; turnBuzzOffT.detach(); WakeSleepT.detach(); @@ -346,6 +343,12 @@ } void startAdvertising(){ + NRF_PWM0->TASKS_SEQSTART[0] = 1; + #if USE_ACC + i2cPower = 1; + #endif + wait_ms(10); + if(shushShush){ // Do not advertise! Go to sleep WakeSleepT.detach(); @@ -356,6 +359,9 @@ ble.gap().startAdvertising(); buzzer.period(0.00F); buzzer.write(0.0F); + NRF_PWM0->TASKS_STOP = 1; + NRF_PWM1->TASKS_STOP = 1; + NRF_PWM2->TASKS_STOP = 1; #if DEBUG advLED = 0; scanLED = 1; @@ -370,6 +376,11 @@ ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW); ble.gap().setScanTimeout(SCAN_TIMER_TIME_S); ble.gap().startScan(advertisementCallback); + buzzer.period(0.00F); + buzzer.write(0.0F); + NRF_PWM0->TASKS_STOP = 1; + NRF_PWM1->TASKS_STOP = 1; + NRF_PWM2->TASKS_STOP = 1; #if DEBUG advLED = 1; scanLED = 0; @@ -405,6 +416,9 @@ void GoToSleep(){ buzzer.period(0.00F); buzzer.write(0.0f); + NRF_PWM0->TASKS_STOP = 1; + NRF_PWM1->TASKS_STOP = 1; + NRF_PWM2->TASKS_STOP = 1; WakeSleepT.detach(); WakeSleepT.attach(WakeMeUp, tempSleepTime); ble.gap().stopAdvertising(); @@ -416,18 +430,15 @@ #endif } - -void pulse_handler(void){ +#if USE_ACC + void pulse_handler(void){ #if DEBUG_ACC int_led = !int_led; #endif - i2c_power.write(1.0F); buzzer.period(0.0009F); buzzer.write(0.5f); - // Be awake some time - WakeSleepT.detach(); - WakeSleepT.attach(GoToSleep, AWAKE_TIME_S); -} + } +#endif int main(void){ #if DEBUG || DEBUG_MAC @@ -438,10 +449,10 @@ buzzer.period(0.0009F); buzzer.write(0.0F); - gyro_power.period(0.01F); - gyro_power.write(1.0F); - i2c_power.period(0.01F); - i2c_power.write(1.0F); + #if USE_ACC + accPower = 1; + i2cPower = 1; + #endif #if PRINT int i; @@ -459,39 +470,57 @@ ble.gap().setTxPower(txPower); GapAdvertisingData postavke = GapAdvertisingData(); - wait_ms(1000); + #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) 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 + wait(1); + #endif - /* 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) 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 */ - //gyro_pulse.rise(&pulse_handler); // ------------------------------------- - //acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode - //wait(1); __enable_irq(); - /* SpinWait for initialization to complete. This is necessary because the - * BLE object is used in the main loop below. */ + /* 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(sleepFlag){ - //if(!advLED) advLED = 1; + NRF_GPIO->PIN_CNF[31] = 0x00000003; + NRF_PWM0->TASKS_STOP = 1; + NRF_PWM1->TASKS_STOP = 1; + NRF_PWM2->TASKS_STOP = 1; + NRF_PPI->TASKS_CHG[0].DIS = 1; + NRF_PPI->TASKS_CHG[1].DIS = 1; + NRF_PPI->TASKS_CHG[2].DIS = 1; + NRF_PPI->TASKS_CHG[3].DIS = 1; + NRF_PPI->TASKS_CHG[4].DIS = 1; + NRF_GPIOTE->TASKS_CLR[0] = 1; + NRF_GPIOTE->TASKS_CLR[1] = 1; + NRF_GPIOTE->TASKS_CLR[2] = 1; + NRF_GPIOTE->TASKS_CLR[3] = 1; + NRF_GPIOTE->TASKS_CLR[4] = 1; + NRF_GPIOTE->TASKS_CLR[5] = 1; + NRF_GPIOTE->TASKS_CLR[6] = 1; + NRF_GPIOTE->TASKS_CLR[7] = 1; + #if USE_ACC + i2cPower = 0; + #endif __WFI(); } else{ - //if(advLED) advLED = 0; ble.waitForEvent(); } }