Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Wed Aug 30 08:10:53 2017 +0000
Revision:
23:729717272b31
Parent:
22:8d106fd5fa84
Child:
24:201b9d8b6c5a
Child:
25:8ac3ff431ab1
Stable version. Power consumption: sleep:1.8uA, Adv: 200uA, Scan: 10mA (!!)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 1:5f34885f5cff 1 /*
jurica238814 0:f8c1e0b2d473 2 *
jurica238814 1:5f34885f5cff 3 * Made by Jurica Resetar @ aconno
jurica238814 1:5f34885f5cff 4 * aconno.de
jurica238814 4:331dddea780e 5 * All rights reserved.
jurica238814 0:f8c1e0b2d473 6 *
jurica238814 0:f8c1e0b2d473 7 */
jurica238814 0:f8c1e0b2d473 8
jurica238814 0:f8c1e0b2d473 9 #include "mbed.h"
jurica238814 0:f8c1e0b2d473 10 #include "ble/BLE.h"
jurica238814 1:5f34885f5cff 11 #include "GapAdvertisingData.h"
jurica238814 0:f8c1e0b2d473 12 #include "acd52832_bsp.h"
jurica238814 6:d14e3df498f4 13 #include "mma8452.h"
jurica238814 16:a338d2417fd5 14 #include "AckService.h"
jurica238814 16:a338d2417fd5 15 #include "nrf52_uart.h"
jurica238814 22:8d106fd5fa84 16 #include "nrf52_digital.h"
jurica238814 1:5f34885f5cff 17
jurica238814 23:729717272b31 18 #define DEBUG (1)
jurica238814 10:fd91664032d8 19 #define DEBUG_ACC (0)
jurica238814 17:51a5456a46cd 20 #define PRINT (0)
jurica238814 17:51a5456a46cd 21 #define DEBUG_MAC (0)
jurica238814 17:51a5456a46cd 22 #define DEBUG_CONNECTION (0)
jurica238814 19:abf14a5ada93 23 #define USE_ACC (0)
jurica238814 8:570eb66d50b5 24
jurica238814 23:729717272b31 25 #define SLEEP_TIME_S (6.0) /* Sleep time (in s) */
jurica238814 23:729717272b31 26 #define ADV_TIMER_TIME_S (0.25) /* Advertising time (in s) */
jurica238814 23:729717272b31 27 #define SCAN_TIMER_TIME_S (0.25) /* Scanning time (in s) */
jurica238814 14:d506c0679c0b 28 #define FREE_TIME_S (0.1) /* Time between end of a scanning and sleep mode */
jurica238814 14:d506c0679c0b 29 #define AWAKE_TIME_S (ADV_TIMER_TIME_S+SCAN_TIMER_TIME_S+FREE_TIME_S) /* Was 0.15 */
jurica238814 19:abf14a5ada93 30 #define SHORT_SLEEP_TIME_S (0.5) /* Shorter sleep time (s) */
jurica238814 19:abf14a5ada93 31 #define SHORT_SLEEP_TIME_PERIOD_S (10) /* Time after a last scanned advertisment. In the period, sleep time is SHORT_SLEEP_TIME */
jurica238814 23:729717272b31 32 #define BUZZ_PERIOD_US (250)
jurica238814 23:729717272b31 33 #define BUZZ_DURATION_MS (1000)
jurica238814 15:934a04c958f5 34 #define MAC_SIZE_B (6)
jurica238814 12:6b072c2a061c 35
jurica238814 1:5f34885f5cff 36 /* Static constants for the BLE example */
jurica238814 3:2a4ac5b87046 37 #define MAX_BLE_PACKET_SIZE (31)
jurica238814 3:2a4ac5b87046 38 #define MSD_SIZE (18)
jurica238814 3:2a4ac5b87046 39 #define MSD_ID (0xFF)
jurica238814 12:6b072c2a061c 40
jurica238814 17:51a5456a46cd 41 #define BUZZ_TIME_S (1) /* Buzz time in s */
jurica238814 18:e844d3e6ab88 42 #define ADV_INTERVAL (200) /* Advertising interval (in ms) */
jurica238814 14:d506c0679c0b 43 #define SCAN_INTERVAL (SCAN_TIMER_TIME_S) /* Scan interval (in ms) */
jurica238814 14:d506c0679c0b 44 #define SCAN_WINDOW (SCAN_TIMER_TIME_S)
jurica238814 1:5f34885f5cff 45
jurica238814 6:d14e3df498f4 46 /* Static constants for the accelerometer */
jurica238814 13:d51127eed926 47 #define WHO_AM_I 0x0D /* Type 'read' : This should return the device id of 0x2A */
jurica238814 13:d51127eed926 48 #define OUT_Z_MSB 0x05 /* Type 'read' : z axis - 8 most significatn bit of a 12 bit sample */
jurica238814 6:d14e3df498f4 49 #define I2C_DATA (p29)
jurica238814 6:d14e3df498f4 50 #define I2C_CLK (p2)
jurica238814 6:d14e3df498f4 51 #define INT2_PIN (p4)
jurica238814 10:fd91664032d8 52 #define BUZZER (p31)
jurica238814 0:f8c1e0b2d473 53
jurica238814 16:a338d2417fd5 54 #if PRINT
jurica238814 16:a338d2417fd5 55 /* Defines for debugging over uart */
jurica238814 16:a338d2417fd5 56 #define TX (p25)
jurica238814 16:a338d2417fd5 57 #define RX (p26)
jurica238814 16:a338d2417fd5 58 NRF52_UART uart(TX,RX, Baud9600);
jurica238814 16:a338d2417fd5 59 char printBuffer[30] = {};
jurica238814 16:a338d2417fd5 60 #endif
jurica238814 16:a338d2417fd5 61
jurica238814 16:a338d2417fd5 62
jurica238814 16:a338d2417fd5 63
jurica238814 16:a338d2417fd5 64 bool shushShush = false;
jurica238814 18:e844d3e6ab88 65 const static uint16_t ACK_CHARA_UUID = 0xA001;
jurica238814 16:a338d2417fd5 66
jurica238814 18:e844d3e6ab88 67 uint8_t txPower = 4;
jurica238814 18:e844d3e6ab88 68 uint8_t sleepFlag = false;
jurica238814 14:d506c0679c0b 69 uint8_t tempSleepTime = SLEEP_TIME_S;
jurica238814 14:d506c0679c0b 70 uint8_t msd[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
jurica238814 23:729717272b31 71 uint8_t myMacAddress[6] = {};
jurica238814 6:d14e3df498f4 72 uint8_t buzzer_flag = 0;
jurica238814 0:f8c1e0b2d473 73
jurica238814 14:d506c0679c0b 74 enum RadioState{
jurica238814 10:fd91664032d8 75 OFF,
jurica238814 10:fd91664032d8 76 ADVERTISING,
jurica238814 10:fd91664032d8 77 SCANNING
jurica238814 10:fd91664032d8 78 };
jurica238814 14:d506c0679c0b 79 enum RadioState radioState = OFF;
jurica238814 0:f8c1e0b2d473 80
jurica238814 14:d506c0679c0b 81 void TurnBuzzOff(void);
jurica238814 14:d506c0679c0b 82 void GoToSleep();
jurica238814 14:d506c0679c0b 83 void StartAdvertising();
jurica238814 10:fd91664032d8 84 void startScanning();
jurica238814 10:fd91664032d8 85 void WakeMeUp();
jurica238814 0:f8c1e0b2d473 86
jurica238814 2:5504b714c9ae 87 Ticker WakeSleepT;
jurica238814 2:5504b714c9ae 88 Ticker turnBuzzOffT;
jurica238814 7:89c9abaa257e 89 Ticker sleepChanger;
jurica238814 21:10c3b8176be0 90 NRF52_DigitalOut buzzer(BUZZER);
jurica238814 19:abf14a5ada93 91 #if USE_ACC
jurica238814 19:abf14a5ada93 92 DigitalOut accPower(p7);
jurica238814 19:abf14a5ada93 93 DigitalOut i2cPower(p5);
jurica238814 19:abf14a5ada93 94 InterruptIn accPulse(INT2_PIN);
jurica238814 21:10c3b8176be0 95 Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS);
jurica238814 19:abf14a5ada93 96 #endif
jurica238814 1:5f34885f5cff 97 BLE &ble = BLE::Instance();
jurica238814 16:a338d2417fd5 98 ACKService<4> *ackServicePtr;
jurica238814 10:fd91664032d8 99
jurica238814 22:8d106fd5fa84 100 #if DEBUG || DEBUG_MAC || DEBUG_CONNECTION
jurica238814 19:abf14a5ada93 101 DigitalOut advLED(p22); // Red
jurica238814 19:abf14a5ada93 102 DigitalOut scanLED(p23); // Blue
jurica238814 19:abf14a5ada93 103 DigitalOut connectedLED(p24); // Green
jurica238814 10:fd91664032d8 104 #endif
jurica238814 18:e844d3e6ab88 105
jurica238814 10:fd91664032d8 106 #if DEBUG_ACC
jurica238814 8:570eb66d50b5 107 DigitalOut int_led(p22);
jurica238814 8:570eb66d50b5 108 #endif
jurica238814 8:570eb66d50b5 109
jurica238814 22:8d106fd5fa84 110 void buzz(uint16_t period_us, uint32_t duration_ms){
jurica238814 22:8d106fd5fa84 111 int32_t counter;
jurica238814 22:8d106fd5fa84 112 for(counter=0; counter<((duration_ms*1000)/(period_us*1.0)); counter++){
jurica238814 22:8d106fd5fa84 113 buzzer.toggle();
jurica238814 22:8d106fd5fa84 114 wait_us(period_us/2);
jurica238814 22:8d106fd5fa84 115 buzzer.toggle();
jurica238814 22:8d106fd5fa84 116 wait_us(period_us/2);
jurica238814 22:8d106fd5fa84 117 }
jurica238814 22:8d106fd5fa84 118 buzzer = 0;
jurica238814 22:8d106fd5fa84 119 }
jurica238814 16:a338d2417fd5 120
jurica238814 21:10c3b8176be0 121 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
jurica238814 17:51a5456a46cd 122 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 123 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 124 wait_ms(100);
jurica238814 17:51a5456a46cd 125 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 126 wait_ms(100);
jurica238814 17:51a5456a46cd 127 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 128 wait_ms(100);
jurica238814 17:51a5456a46cd 129 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 130 wait_ms(100);
jurica238814 17:51a5456a46cd 131 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 132 wait_ms(100);
jurica238814 17:51a5456a46cd 133 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 134 wait_ms(100);
jurica238814 17:51a5456a46cd 135 scanLED = 1; // Blue
jurica238814 17:51a5456a46cd 136 #endif
jurica238814 17:51a5456a46cd 137 WakeSleepT.detach();
jurica238814 17:51a5456a46cd 138 sleepFlag = false;
jurica238814 16:a338d2417fd5 139 }
jurica238814 16:a338d2417fd5 140
jurica238814 16:a338d2417fd5 141
jurica238814 0:f8c1e0b2d473 142 /* Restart Advertising on disconnection*/
jurica238814 0:f8c1e0b2d473 143 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
jurica238814 17:51a5456a46cd 144 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 145 advLED = !advLED; // RED
jurica238814 17:51a5456a46cd 146 wait_ms(100);
jurica238814 17:51a5456a46cd 147 advLED = !advLED;
jurica238814 17:51a5456a46cd 148 wait_ms(100);
jurica238814 17:51a5456a46cd 149 advLED = !advLED;
jurica238814 17:51a5456a46cd 150 wait_ms(100);
jurica238814 17:51a5456a46cd 151 advLED = !advLED;
jurica238814 17:51a5456a46cd 152 wait_ms(100);
jurica238814 17:51a5456a46cd 153 advLED = 1;
jurica238814 17:51a5456a46cd 154 wait_ms(100);
jurica238814 17:51a5456a46cd 155 advLED = 1;
jurica238814 17:51a5456a46cd 156 #endif
jurica238814 17:51a5456a46cd 157 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 17:51a5456a46cd 158 sleepFlag = true;
jurica238814 18:e844d3e6ab88 159
jurica238814 0:f8c1e0b2d473 160 }
jurica238814 0:f8c1e0b2d473 161
jurica238814 16:a338d2417fd5 162 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
jurica238814 16:a338d2417fd5 163 if(params->handle == ACK_CHARA_UUID || 1){
jurica238814 16:a338d2417fd5 164 // Something is written into AckCharacteristic
jurica238814 16:a338d2417fd5 165 if(params->data[0] == 0xBA)
jurica238814 16:a338d2417fd5 166 if(params->data[1] == 0xBE){
jurica238814 17:51a5456a46cd 167 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 168 connectedLED = !connectedLED; // BLUE
jurica238814 17:51a5456a46cd 169 wait_ms(100);
jurica238814 17:51a5456a46cd 170 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 171 wait_ms(100);
jurica238814 17:51a5456a46cd 172 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 173 wait_ms(100);
jurica238814 17:51a5456a46cd 174 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 175 wait_ms(100);
jurica238814 17:51a5456a46cd 176 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 177 wait_ms(100);
jurica238814 17:51a5456a46cd 178 connectedLED = 1;
jurica238814 17:51a5456a46cd 179 wait_ms(100);
jurica238814 17:51a5456a46cd 180 #endif
jurica238814 23:729717272b31 181 buzz(BUZZ_PERIOD_US,BUZZ_DURATION_MS);
jurica238814 17:51a5456a46cd 182 WakeSleepT.detach();
jurica238814 22:8d106fd5fa84 183 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 16:a338d2417fd5 184 /*
jurica238814 16:a338d2417fd5 185 This function should make advertiser stop
jurica238814 16:a338d2417fd5 186 */
jurica238814 16:a338d2417fd5 187 ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
jurica238814 16:a338d2417fd5 188 return;
jurica238814 16:a338d2417fd5 189 }
jurica238814 16:a338d2417fd5 190 }
jurica238814 16:a338d2417fd5 191 else{
jurica238814 17:51a5456a46cd 192 // Execute this for wrong data written into characteristic
jurica238814 16:a338d2417fd5 193 }
jurica238814 16:a338d2417fd5 194 }
jurica238814 0:f8c1e0b2d473 195
jurica238814 0:f8c1e0b2d473 196 /**
jurica238814 0:f8c1e0b2d473 197 * This function is called when the ble initialization process has failed
jurica238814 0:f8c1e0b2d473 198 */
jurica238814 0:f8c1e0b2d473 199 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:f8c1e0b2d473 200 /* Avoid compiler warnings */
jurica238814 0:f8c1e0b2d473 201 (void) ble;
jurica238814 0:f8c1e0b2d473 202 (void) error;
jurica238814 0:f8c1e0b2d473 203 /* Initialization error handling should go here */
jurica238814 0:f8c1e0b2d473 204 }
jurica238814 0:f8c1e0b2d473 205
jurica238814 0:f8c1e0b2d473 206 /**
jurica238814 0:f8c1e0b2d473 207 * Callback triggered when the ble initialization process has finished
jurica238814 0:f8c1e0b2d473 208 */
jurica238814 0:f8c1e0b2d473 209 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:f8c1e0b2d473 210 BLE& ble = params->ble;
jurica238814 0:f8c1e0b2d473 211 ble_error_t error = params->error;
jurica238814 0:f8c1e0b2d473 212
jurica238814 0:f8c1e0b2d473 213 if (error != BLE_ERROR_NONE) {
jurica238814 0:f8c1e0b2d473 214 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:f8c1e0b2d473 215 onBleInitError(ble, error);
jurica238814 0:f8c1e0b2d473 216 return;
jurica238814 0:f8c1e0b2d473 217 }
jurica238814 0:f8c1e0b2d473 218
jurica238814 0:f8c1e0b2d473 219 /* Ensure that it is the default instance of BLE */
jurica238814 0:f8c1e0b2d473 220 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:f8c1e0b2d473 221 return;
jurica238814 0:f8c1e0b2d473 222 }
jurica238814 16:a338d2417fd5 223
jurica238814 16:a338d2417fd5 224 uint8_t init_values[4] = {0,0,0,0};
jurica238814 1:5f34885f5cff 225 /* Get my MAC address */
jurica238814 1:5f34885f5cff 226 BLEProtocol::AddressType_t temp_address_type;
jurica238814 14:d506c0679c0b 227 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 16:a338d2417fd5 228 ackServicePtr = new ACKService<4>(ble, init_values);
jurica238814 16:a338d2417fd5 229 ackServicePtr->updateMacAddress(myMacAddress); // Update MAC address
jurica238814 21:10c3b8176be0 230
jurica238814 16:a338d2417fd5 231 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 17:51a5456a46cd 232 ble.gap().onConnection(onConnectionCallback); // -->> Uncomment these two lines for shush-shush
jurica238814 17:51a5456a46cd 233 ble.gattServer().onDataWritten(onDataWrittenCallback);
jurica238814 1:5f34885f5cff 234
jurica238814 14:d506c0679c0b 235 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE);
jurica238814 10:fd91664032d8 236 ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms!
jurica238814 23:729717272b31 237
jurica238814 16:a338d2417fd5 238
jurica238814 0:f8c1e0b2d473 239 }
jurica238814 0:f8c1e0b2d473 240
jurica238814 3:2a4ac5b87046 241
jurica238814 3:2a4ac5b87046 242 uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 1:5f34885f5cff 243 uint8_t i=0;
jurica238814 18:e844d3e6ab88 244 uint8_t advLen = params->advertisingDataLen;
jurica238814 18:e844d3e6ab88 245 uint8_t dataLen;
jurica238814 18:e844d3e6ab88 246
jurica238814 18:e844d3e6ab88 247 if((advLen < (MAC_SIZE_B + 2)) || advLen == 0){
jurica238814 15:934a04c958f5 248 // Empty advertisement or not long enough for MAX
jurica238814 18:e844d3e6ab88 249 // +2 for SIZE and MSD ID
jurica238814 15:934a04c958f5 250 return 0;
jurica238814 15:934a04c958f5 251 }
jurica238814 3:2a4ac5b87046 252
jurica238814 3:2a4ac5b87046 253 do{
jurica238814 18:e844d3e6ab88 254 dataLen = params->advertisingData[i];
jurica238814 3:2a4ac5b87046 255 i++;
jurica238814 3:2a4ac5b87046 256 if(params->advertisingData[i] == MSD_ID) return i;
jurica238814 18:e844d3e6ab88 257 else i += (dataLen);
jurica238814 18:e844d3e6ab88 258 }while(i<advLen);
jurica238814 3:2a4ac5b87046 259
jurica238814 3:2a4ac5b87046 260 return 0;
jurica238814 3:2a4ac5b87046 261 }
jurica238814 3:2a4ac5b87046 262
jurica238814 14:d506c0679c0b 263 uint8_t CheckMac(const Gap::AdvertisementCallbackParams_t *params, uint8_t *myMacAddress, uint8_t msdOffset){
jurica238814 14:d506c0679c0b 264 int i=0;
jurica238814 14:d506c0679c0b 265
jurica238814 16:a338d2417fd5 266 /* Get my MAC address */
jurica238814 16:a338d2417fd5 267 BLEProtocol::AddressType_t temp_address_type;
jurica238814 16:a338d2417fd5 268 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 16:a338d2417fd5 269
jurica238814 18:e844d3e6ab88 270 if(!msdOffset){
jurica238814 17:51a5456a46cd 271 #if DEBUG_MAC
jurica238814 16:a338d2417fd5 272 for(i=0; i<10; i++){
jurica238814 16:a338d2417fd5 273 scanLED = !scanLED; // BLUE
jurica238814 16:a338d2417fd5 274 wait_ms(100);
jurica238814 16:a338d2417fd5 275 }
jurica238814 17:51a5456a46cd 276 #endif
jurica238814 14:d506c0679c0b 277 return 0; // There's no MSD in BLE advertisement data
jurica238814 14:d506c0679c0b 278 }
jurica238814 14:d506c0679c0b 279 for(i=0; i<6; i++){
jurica238814 16:a338d2417fd5 280 if(params->advertisingData[msdOffset + 3 + i] != myMacAddress[5-i]){ // myMacAddress[0] == 0x91
jurica238814 17:51a5456a46cd 281 #if DEBUG_MAC
jurica238814 17:51a5456a46cd 282 for(i=0; i<10; i++){
jurica238814 17:51a5456a46cd 283 connectedLED = !connectedLED; // Green
jurica238814 17:51a5456a46cd 284 wait_ms(100);
jurica238814 17:51a5456a46cd 285 }
jurica238814 17:51a5456a46cd 286 #endif
jurica238814 14:d506c0679c0b 287 return 0;
jurica238814 14:d506c0679c0b 288 }
jurica238814 14:d506c0679c0b 289 }
jurica238814 17:51a5456a46cd 290 #if DEBUG_MAC
jurica238814 17:51a5456a46cd 291 for(i=0; i<10; i++){
jurica238814 17:51a5456a46cd 292 advLED = !advLED; // RED
jurica238814 17:51a5456a46cd 293 wait_ms(100);
jurica238814 17:51a5456a46cd 294 }
jurica238814 18:e844d3e6ab88 295 advLED = 1;
jurica238814 17:51a5456a46cd 296 #endif
jurica238814 14:d506c0679c0b 297 return 1;
jurica238814 14:d506c0679c0b 298 }
jurica238814 14:d506c0679c0b 299
jurica238814 3:2a4ac5b87046 300 /**
jurica238814 3:2a4ac5b87046 301 * Function is called when BLE radio discovers any kind of advertisment
jurica238814 3:2a4ac5b87046 302 */
jurica238814 3:2a4ac5b87046 303 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 19:abf14a5ada93 304 uint8_t msdOffset;
jurica238814 15:934a04c958f5 305
jurica238814 18:e844d3e6ab88 306 msdOffset = findMSDIndex(params); // Should be 1 or 4
jurica238814 3:2a4ac5b87046 307 if(msdOffset == 0){
jurica238814 14:d506c0679c0b 308 return; // There's no MSD in BLE advertisement data
jurica238814 3:2a4ac5b87046 309 }
jurica238814 14:d506c0679c0b 310 if ((params->advertisingData[msdOffset]) == MSD_ID){
jurica238814 1:5f34885f5cff 311 // Follows Manufacturer Specific Data
jurica238814 3:2a4ac5b87046 312 if ((params->advertisingData[msdOffset+1]) == 0x59){
jurica238814 3:2a4ac5b87046 313 if ((params->advertisingData[msdOffset+2]) == 0x00){
jurica238814 16:a338d2417fd5 314 if(CheckMac(params, myMacAddress, msdOffset)){
jurica238814 17:51a5456a46cd 315 //ble.gap().stopScan();
jurica238814 23:729717272b31 316 buzz(BUZZ_PERIOD_US,BUZZ_DURATION_MS);
jurica238814 14:d506c0679c0b 317 WakeSleepT.detach();
jurica238814 22:8d106fd5fa84 318 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 1:5f34885f5cff 319 }
jurica238814 1:5f34885f5cff 320 }
jurica238814 1:5f34885f5cff 321 }
jurica238814 2:5504b714c9ae 322 }
jurica238814 2:5504b714c9ae 323 }
jurica238814 2:5504b714c9ae 324
jurica238814 12:6b072c2a061c 325
jurica238814 7:89c9abaa257e 326 /* Call this function few minutes (TBD) after a last scanned advertisment */
jurica238814 7:89c9abaa257e 327 void changeSleepTime(){
jurica238814 14:d506c0679c0b 328 tempSleepTime = SLEEP_TIME_S;
jurica238814 7:89c9abaa257e 329 sleepChanger.detach();
jurica238814 7:89c9abaa257e 330 }
jurica238814 7:89c9abaa257e 331
jurica238814 12:6b072c2a061c 332
jurica238814 7:89c9abaa257e 333 /**
jurica238814 7:89c9abaa257e 334 * The function is called when ticker generates interrupt
jurica238814 7:89c9abaa257e 335 */
jurica238814 14:d506c0679c0b 336 void TurnBuzzOff(void){
jurica238814 14:d506c0679c0b 337 tempSleepTime = SHORT_SLEEP_TIME_S;
jurica238814 7:89c9abaa257e 338 turnBuzzOffT.detach();
jurica238814 13:d51127eed926 339 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 340 sleepChanger.attach(changeSleepTime, SHORT_SLEEP_TIME_PERIOD_S);
jurica238814 14:d506c0679c0b 341 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 7:89c9abaa257e 342 }
jurica238814 7:89c9abaa257e 343
jurica238814 10:fd91664032d8 344 void startAdvertising(){
jurica238814 19:abf14a5ada93 345 #if USE_ACC
jurica238814 19:abf14a5ada93 346 i2cPower = 1;
jurica238814 19:abf14a5ada93 347 #endif
jurica238814 19:abf14a5ada93 348 wait_ms(10);
jurica238814 19:abf14a5ada93 349
jurica238814 16:a338d2417fd5 350 if(shushShush){
jurica238814 16:a338d2417fd5 351 // Do not advertise! Go to sleep
jurica238814 16:a338d2417fd5 352 WakeSleepT.detach();
jurica238814 16:a338d2417fd5 353 ble.gap().stopAdvertising();
jurica238814 16:a338d2417fd5 354 WakeMeUp();
jurica238814 16:a338d2417fd5 355 }
jurica238814 16:a338d2417fd5 356 else{
jurica238814 16:a338d2417fd5 357 ble.gap().startAdvertising();
jurica238814 16:a338d2417fd5 358 #if DEBUG
jurica238814 16:a338d2417fd5 359 advLED = 0;
jurica238814 16:a338d2417fd5 360 scanLED = 1;
jurica238814 16:a338d2417fd5 361 #endif
jurica238814 16:a338d2417fd5 362 WakeSleepT.detach();
jurica238814 16:a338d2417fd5 363 WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function
jurica238814 16:a338d2417fd5 364 }
jurica238814 10:fd91664032d8 365 }
jurica238814 10:fd91664032d8 366
jurica238814 10:fd91664032d8 367 void startScanning(){
jurica238814 10:fd91664032d8 368 ble.gap().stopAdvertising();
jurica238814 23:729717272b31 369 ble.gap().setScanInterval(SCAN_INTERVAL);
jurica238814 23:729717272b31 370 ble.gap().setScanWindow(SCAN_WINDOW);
jurica238814 23:729717272b31 371 ble.gap().setScanParams();
jurica238814 2:5504b714c9ae 372 ble.gap().startScan(advertisementCallback);
jurica238814 10:fd91664032d8 373 #if DEBUG
jurica238814 10:fd91664032d8 374 advLED = 1;
jurica238814 10:fd91664032d8 375 scanLED = 0;
jurica238814 10:fd91664032d8 376 #endif
jurica238814 2:5504b714c9ae 377 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 378 WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME_S);
jurica238814 10:fd91664032d8 379 }
jurica238814 10:fd91664032d8 380
jurica238814 10:fd91664032d8 381 void WakeMeUp(){
jurica238814 8:570eb66d50b5 382 sleepFlag = 0;
jurica238814 10:fd91664032d8 383 switch(radioState){
jurica238814 10:fd91664032d8 384 case OFF:{
jurica238814 14:d506c0679c0b 385 radioState = ADVERTISING;
jurica238814 10:fd91664032d8 386 startAdvertising();
jurica238814 10:fd91664032d8 387 break;
jurica238814 10:fd91664032d8 388 }
jurica238814 10:fd91664032d8 389 case ADVERTISING:{
jurica238814 14:d506c0679c0b 390 radioState = SCANNING;
jurica238814 10:fd91664032d8 391 startScanning();
jurica238814 10:fd91664032d8 392 break;
jurica238814 10:fd91664032d8 393 }
jurica238814 10:fd91664032d8 394 case SCANNING:{
jurica238814 10:fd91664032d8 395 radioState = OFF;
jurica238814 10:fd91664032d8 396 WakeSleepT.detach();
jurica238814 16:a338d2417fd5 397 //WakeSleepT.attach(GoToSleep, FREE_TIME_S);
jurica238814 16:a338d2417fd5 398 GoToSleep();
jurica238814 10:fd91664032d8 399 break;
jurica238814 10:fd91664032d8 400 }
jurica238814 10:fd91664032d8 401 default: return;
jurica238814 10:fd91664032d8 402 }
jurica238814 2:5504b714c9ae 403 }
jurica238814 2:5504b714c9ae 404
jurica238814 14:d506c0679c0b 405 void GoToSleep(){
jurica238814 2:5504b714c9ae 406 WakeSleepT.detach();
jurica238814 7:89c9abaa257e 407 WakeSleepT.attach(WakeMeUp, tempSleepTime);
jurica238814 2:5504b714c9ae 408 ble.gap().stopAdvertising();
jurica238814 2:5504b714c9ae 409 ble.gap().stopScan();
jurica238814 8:570eb66d50b5 410 sleepFlag = 1;
jurica238814 16:a338d2417fd5 411 #if DEBUG
jurica238814 16:a338d2417fd5 412 advLED = 1;
jurica238814 16:a338d2417fd5 413 scanLED = 1;
jurica238814 16:a338d2417fd5 414 #endif
jurica238814 1:5f34885f5cff 415 }
jurica238814 1:5f34885f5cff 416
jurica238814 19:abf14a5ada93 417 #if USE_ACC
jurica238814 19:abf14a5ada93 418 void pulse_handler(void){
jurica238814 10:fd91664032d8 419 #if DEBUG_ACC
jurica238814 6:d14e3df498f4 420 int_led = !int_led;
jurica238814 6:d14e3df498f4 421 #endif
jurica238814 19:abf14a5ada93 422 }
jurica238814 19:abf14a5ada93 423 #endif
jurica238814 6:d14e3df498f4 424
jurica238814 10:fd91664032d8 425 int main(void){
jurica238814 18:e844d3e6ab88 426 #if DEBUG || DEBUG_MAC
jurica238814 10:fd91664032d8 427 advLED = 1;
jurica238814 10:fd91664032d8 428 scanLED = 1;
jurica238814 16:a338d2417fd5 429 connectedLED = 1;
jurica238814 10:fd91664032d8 430 #endif
jurica238814 14:d506c0679c0b 431
jurica238814 19:abf14a5ada93 432 #if USE_ACC
jurica238814 19:abf14a5ada93 433 accPower = 1;
jurica238814 19:abf14a5ada93 434 i2cPower = 1;
jurica238814 19:abf14a5ada93 435 #endif
jurica238814 16:a338d2417fd5 436
jurica238814 16:a338d2417fd5 437 #if PRINT
jurica238814 18:e844d3e6ab88 438 int i;
jurica238814 18:e844d3e6ab88 439 for(i=0; i<10; i++){
jurica238814 18:e844d3e6ab88 440 printBuffer[0] = 'B';
jurica238814 18:e844d3e6ab88 441 printBuffer[1] = 'o';
jurica238814 18:e844d3e6ab88 442 printBuffer[2] = 'k';
jurica238814 18:e844d3e6ab88 443 uart.send(printBuffer, 3);
jurica238814 18:e844d3e6ab88 444 wait_ms(100);
jurica238814 18:e844d3e6ab88 445 }
jurica238814 16:a338d2417fd5 446 #endif
jurica238814 16:a338d2417fd5 447
jurica238814 21:10c3b8176be0 448 //WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
jurica238814 21:10c3b8176be0 449 GoToSleep();
jurica238814 16:a338d2417fd5 450 ble.init(bleInitComplete);
jurica238814 16:a338d2417fd5 451 ble.gap().setTxPower(txPower);
jurica238814 16:a338d2417fd5 452 GapAdvertisingData postavke = GapAdvertisingData();
jurica238814 16:a338d2417fd5 453
jurica238814 19:abf14a5ada93 454 #if USE_ACC
jurica238814 19:abf14a5ada93 455 // Pulse interrupt detection
jurica238814 19:abf14a5ada93 456 acc.set_register((char)CTRL_REG_4, (char) 0x04); // INT_EN_FF_MT Freefall/motion interrupt enabled
jurica238814 19:abf14a5ada93 457 wait_ms(1);
jurica238814 19:abf14a5ada93 458 acc.set_register((char)FF_MT_CFG, (char) 0b01011000); //ELE, Motion Flag ON, YEFE, X Event Flag Enable
jurica238814 19:abf14a5ada93 459 wait_ms(1);
jurica238814 19:abf14a5ada93 460 acc.set_register((char)CTRL_REG_5, (char) 0x00); // INT_EN_FF_MT interrupt is router t0 INT2
jurica238814 19:abf14a5ada93 461 wait_ms(1);
jurica238814 19:abf14a5ada93 462 acc.set_register((char)FF_COUNT, (char) 0x08); // Set Counter degister value (10ms)
jurica238814 19:abf14a5ada93 463 wait_ms(1);
jurica238814 19:abf14a5ada93 464 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)
jurica238814 19:abf14a5ada93 465 wait_ms(1);
jurica238814 19:abf14a5ada93 466
jurica238814 19:abf14a5ada93 467 // Setup for the interrupt handler
jurica238814 19:abf14a5ada93 468 accPulse.rise(&pulse_handler); // -------------------------------------
jurica238814 19:abf14a5ada93 469 acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode
jurica238814 19:abf14a5ada93 470 wait(1);
jurica238814 19:abf14a5ada93 471 #endif
jurica238814 6:d14e3df498f4 472
jurica238814 2:5504b714c9ae 473 __enable_irq();
jurica238814 2:5504b714c9ae 474
jurica238814 19:abf14a5ada93 475 /* SpinWait for initialization to complete. This is necessary because the BLE object is used in the main loop below. */
jurica238814 3:2a4ac5b87046 476 while (ble.hasInitialized() == false){
jurica238814 3:2a4ac5b87046 477 /* spin loop */
jurica238814 3:2a4ac5b87046 478 }
jurica238814 9:2ab2be19add9 479
jurica238814 1:5f34885f5cff 480 while(true){
jurica238814 9:2ab2be19add9 481 if(sleepFlag){
jurica238814 21:10c3b8176be0 482 //NRF_GPIO->PIN_CNF[31] = 0x00000002;
jurica238814 19:abf14a5ada93 483 #if USE_ACC
jurica238814 19:abf14a5ada93 484 i2cPower = 0;
jurica238814 19:abf14a5ada93 485 #endif
jurica238814 21:10c3b8176be0 486 ble.waitForEvent();
jurica238814 9:2ab2be19add9 487 __WFI();
jurica238814 9:2ab2be19add9 488 }
jurica238814 9:2ab2be19add9 489 else{
jurica238814 9:2ab2be19add9 490 ble.waitForEvent();
jurica238814 9:2ab2be19add9 491 }
jurica238814 0:f8c1e0b2d473 492 }
jurica238814 0:f8c1e0b2d473 493 }