Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Thu Sep 14 12:43:51 2017 +0000
Revision:
28:7b71c61d2160
Parent:
27:2c67f07590fd
Child:
29:7b794a8633b4
With OUT acc. # Sleep time: 7s # Awake (adv) time: 0.3s. # Battery Life: 2.82year.

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