Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Mon Aug 28 19:58:11 2017 +0000
Revision:
22:8d106fd5fa84
Parent:
21:10c3b8176be0
Child:
23:729717272b31
pwm->gpio buzzer. This version works stable. Problems: beeping is not loud enough. Scanner power is huge (>7mA).

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