Geo beacon for VF.

Dependencies:   MMA8452 aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Wed Sep 13 16:28:32 2017 +0000
Revision:
27:2c67f07590fd
Parent:
26:148aa2e2460c
Child:
28:7b71c61d2160
Child:
31:caef580f5943
BLE works. OUT PWM WORKS! LOW power consumption. ACC TODO!

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 27:2c67f07590fd 28 #define SLEEP_TIME_S (3.00) /* Sleep time (in s) */
jurica238814 27:2c67f07590fd 29 #define ADV_TIMER_TIME_S (1.00) /* 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 25:8ac3ff431ab1 44 #define ADV_INTERVAL (100) /* 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);
jurica238814 27:2c67f07590fd 118 //buzzer.enable(1);
Dautor 26:148aa2e2460c 119 buzzer.enableChannel(0, BUZZER);
jurica238814 27:2c67f07590fd 120 buzzer.setDuty(0,0.5f);
Dautor 26:148aa2e2460c 121 }
Dautor 26:148aa2e2460c 122 void buzzerStop(){
jurica238814 27:2c67f07590fd 123 buzzer.enable(0);
jurica238814 27:2c67f07590fd 124 buzzer.setDuty(0, 0);
Dautor 26:148aa2e2460c 125 buzzer.disable();
jurica238814 22:8d106fd5fa84 126 }
jurica238814 16:a338d2417fd5 127
jurica238814 21:10c3b8176be0 128 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
jurica238814 17:51a5456a46cd 129 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 130 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 131 wait_ms(100);
jurica238814 17:51a5456a46cd 132 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 133 wait_ms(100);
jurica238814 17:51a5456a46cd 134 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 135 wait_ms(100);
jurica238814 17:51a5456a46cd 136 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 137 wait_ms(100);
jurica238814 17:51a5456a46cd 138 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 139 wait_ms(100);
jurica238814 17:51a5456a46cd 140 scanLED = !scanLED; // Blue
jurica238814 17:51a5456a46cd 141 wait_ms(100);
jurica238814 17:51a5456a46cd 142 scanLED = 1; // Blue
jurica238814 17:51a5456a46cd 143 #endif
jurica238814 17:51a5456a46cd 144 WakeSleepT.detach();
jurica238814 17:51a5456a46cd 145 sleepFlag = false;
jurica238814 16:a338d2417fd5 146 }
jurica238814 16:a338d2417fd5 147
jurica238814 16:a338d2417fd5 148
jurica238814 0:f8c1e0b2d473 149 /* Restart Advertising on disconnection*/
jurica238814 0:f8c1e0b2d473 150 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
Dautor 26:148aa2e2460c 151 buzzerStop();
jurica238814 17:51a5456a46cd 152 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 153 advLED = !advLED; // RED
jurica238814 17:51a5456a46cd 154 wait_ms(100);
jurica238814 17:51a5456a46cd 155 advLED = !advLED;
jurica238814 17:51a5456a46cd 156 wait_ms(100);
jurica238814 17:51a5456a46cd 157 advLED = !advLED;
jurica238814 17:51a5456a46cd 158 wait_ms(100);
jurica238814 17:51a5456a46cd 159 advLED = !advLED;
jurica238814 17:51a5456a46cd 160 wait_ms(100);
jurica238814 17:51a5456a46cd 161 advLED = 1;
jurica238814 17:51a5456a46cd 162 wait_ms(100);
jurica238814 17:51a5456a46cd 163 advLED = 1;
jurica238814 17:51a5456a46cd 164 #endif
jurica238814 17:51a5456a46cd 165 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 17:51a5456a46cd 166 sleepFlag = true;
jurica238814 18:e844d3e6ab88 167
jurica238814 0:f8c1e0b2d473 168 }
jurica238814 0:f8c1e0b2d473 169
jurica238814 16:a338d2417fd5 170 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
jurica238814 25:8ac3ff431ab1 171 if(params->handle == ackServicePtr->getACKCharacteristicHandle()){
jurica238814 16:a338d2417fd5 172 // Something is written into AckCharacteristic
jurica238814 25:8ac3ff431ab1 173 if(params->data[0] == startBuzz[0]){
jurica238814 25:8ac3ff431ab1 174 if(params->data[1] == startBuzz[1]){
jurica238814 17:51a5456a46cd 175 #if DEBUG_CONNECTION
jurica238814 17:51a5456a46cd 176 connectedLED = !connectedLED; // BLUE
jurica238814 17:51a5456a46cd 177 wait_ms(100);
jurica238814 17:51a5456a46cd 178 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 179 wait_ms(100);
jurica238814 17:51a5456a46cd 180 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 181 wait_ms(100);
jurica238814 17:51a5456a46cd 182 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 183 wait_ms(100);
jurica238814 17:51a5456a46cd 184 connectedLED = !connectedLED;
jurica238814 17:51a5456a46cd 185 wait_ms(100);
jurica238814 17:51a5456a46cd 186 connectedLED = 1;
jurica238814 17:51a5456a46cd 187 wait_ms(100);
jurica238814 17:51a5456a46cd 188 #endif
Dautor 26:148aa2e2460c 189 buzzerStart();
jurica238814 16:a338d2417fd5 190 return;
jurica238814 16:a338d2417fd5 191 }
jurica238814 25:8ac3ff431ab1 192 }
Dautor 26:148aa2e2460c 193 else if(params->data[0] == stopBuzz[0]){
Dautor 26:148aa2e2460c 194 if(params->data[1] == stopBuzz[1]){
jurica238814 27:2c67f07590fd 195 buzzerStop();
Dautor 26:148aa2e2460c 196 WakeSleepT.detach();
Dautor 26:148aa2e2460c 197 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
Dautor 26:148aa2e2460c 198 ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
jurica238814 25:8ac3ff431ab1 199 }
jurica238814 25:8ac3ff431ab1 200 }
Dautor 26:148aa2e2460c 201 else if(params->data[0] == 0x55){
jurica238814 27:2c67f07590fd 202 /* This is used after pairing */
jurica238814 27:2c67f07590fd 203 radioState = SCANNING;
jurica238814 27:2c67f07590fd 204 WakeSleepT.detach();
jurica238814 27:2c67f07590fd 205 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 27:2c67f07590fd 206 shushShush = true;
Dautor 26:148aa2e2460c 207 ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
Dautor 26:148aa2e2460c 208 }
Dautor 26:148aa2e2460c 209
jurica238814 16:a338d2417fd5 210 }
jurica238814 16:a338d2417fd5 211 else{
jurica238814 25:8ac3ff431ab1 212 // Execute this for wrong data written into characteristic
jurica238814 25:8ac3ff431ab1 213 return;
jurica238814 16:a338d2417fd5 214 }
jurica238814 16:a338d2417fd5 215 }
jurica238814 0:f8c1e0b2d473 216
jurica238814 0:f8c1e0b2d473 217 /**
jurica238814 0:f8c1e0b2d473 218 * This function is called when the ble initialization process has failed
jurica238814 0:f8c1e0b2d473 219 */
jurica238814 0:f8c1e0b2d473 220 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:f8c1e0b2d473 221 /* Avoid compiler warnings */
jurica238814 0:f8c1e0b2d473 222 (void) ble;
jurica238814 0:f8c1e0b2d473 223 (void) error;
jurica238814 0:f8c1e0b2d473 224 /* Initialization error handling should go here */
jurica238814 0:f8c1e0b2d473 225 }
jurica238814 0:f8c1e0b2d473 226
jurica238814 0:f8c1e0b2d473 227 /**
jurica238814 0:f8c1e0b2d473 228 * Callback triggered when the ble initialization process has finished
jurica238814 0:f8c1e0b2d473 229 */
jurica238814 0:f8c1e0b2d473 230 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:f8c1e0b2d473 231 BLE& ble = params->ble;
jurica238814 0:f8c1e0b2d473 232 ble_error_t error = params->error;
jurica238814 0:f8c1e0b2d473 233
jurica238814 0:f8c1e0b2d473 234 if (error != BLE_ERROR_NONE) {
jurica238814 0:f8c1e0b2d473 235 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:f8c1e0b2d473 236 onBleInitError(ble, error);
jurica238814 0:f8c1e0b2d473 237 return;
jurica238814 0:f8c1e0b2d473 238 }
jurica238814 0:f8c1e0b2d473 239
jurica238814 0:f8c1e0b2d473 240 /* Ensure that it is the default instance of BLE */
jurica238814 0:f8c1e0b2d473 241 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:f8c1e0b2d473 242 return;
jurica238814 0:f8c1e0b2d473 243 }
jurica238814 16:a338d2417fd5 244
jurica238814 16:a338d2417fd5 245 uint8_t init_values[4] = {0,0,0,0};
jurica238814 1:5f34885f5cff 246 /* Get my MAC address */
jurica238814 1:5f34885f5cff 247 BLEProtocol::AddressType_t temp_address_type;
jurica238814 14:d506c0679c0b 248 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 16:a338d2417fd5 249 ackServicePtr = new ACKService<4>(ble, init_values);
jurica238814 16:a338d2417fd5 250 ackServicePtr->updateMacAddress(myMacAddress); // Update MAC address
jurica238814 21:10c3b8176be0 251
jurica238814 16:a338d2417fd5 252 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 17:51a5456a46cd 253 ble.gap().onConnection(onConnectionCallback); // -->> Uncomment these two lines for shush-shush
jurica238814 17:51a5456a46cd 254 ble.gattServer().onDataWritten(onDataWrittenCallback);
jurica238814 1:5f34885f5cff 255
jurica238814 14:d506c0679c0b 256 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE);
jurica238814 10:fd91664032d8 257 ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms!
jurica238814 0:f8c1e0b2d473 258 }
jurica238814 0:f8c1e0b2d473 259
jurica238814 3:2a4ac5b87046 260
jurica238814 3:2a4ac5b87046 261 uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 1:5f34885f5cff 262 uint8_t i=0;
jurica238814 18:e844d3e6ab88 263 uint8_t advLen = params->advertisingDataLen;
jurica238814 18:e844d3e6ab88 264 uint8_t dataLen;
jurica238814 18:e844d3e6ab88 265
jurica238814 18:e844d3e6ab88 266 if((advLen < (MAC_SIZE_B + 2)) || advLen == 0){
jurica238814 15:934a04c958f5 267 // Empty advertisement or not long enough for MAX
jurica238814 18:e844d3e6ab88 268 // +2 for SIZE and MSD ID
jurica238814 15:934a04c958f5 269 return 0;
jurica238814 15:934a04c958f5 270 }
jurica238814 3:2a4ac5b87046 271
jurica238814 3:2a4ac5b87046 272 do{
jurica238814 18:e844d3e6ab88 273 dataLen = params->advertisingData[i];
jurica238814 3:2a4ac5b87046 274 i++;
jurica238814 3:2a4ac5b87046 275 if(params->advertisingData[i] == MSD_ID) return i;
jurica238814 18:e844d3e6ab88 276 else i += (dataLen);
jurica238814 18:e844d3e6ab88 277 }while(i<advLen);
jurica238814 3:2a4ac5b87046 278
jurica238814 3:2a4ac5b87046 279 return 0;
jurica238814 3:2a4ac5b87046 280 }
jurica238814 3:2a4ac5b87046 281
jurica238814 14:d506c0679c0b 282 uint8_t CheckMac(const Gap::AdvertisementCallbackParams_t *params, uint8_t *myMacAddress, uint8_t msdOffset){
jurica238814 14:d506c0679c0b 283 int i=0;
jurica238814 14:d506c0679c0b 284
jurica238814 16:a338d2417fd5 285 /* Get my MAC address */
jurica238814 16:a338d2417fd5 286 BLEProtocol::AddressType_t temp_address_type;
jurica238814 16:a338d2417fd5 287 ble.gap().getAddress(&temp_address_type, myMacAddress);
jurica238814 16:a338d2417fd5 288
jurica238814 18:e844d3e6ab88 289 if(!msdOffset){
jurica238814 17:51a5456a46cd 290 #if DEBUG_MAC
jurica238814 16:a338d2417fd5 291 for(i=0; i<10; i++){
jurica238814 16:a338d2417fd5 292 scanLED = !scanLED; // BLUE
jurica238814 16:a338d2417fd5 293 wait_ms(100);
jurica238814 16:a338d2417fd5 294 }
jurica238814 17:51a5456a46cd 295 #endif
jurica238814 14:d506c0679c0b 296 return 0; // There's no MSD in BLE advertisement data
jurica238814 14:d506c0679c0b 297 }
jurica238814 14:d506c0679c0b 298 for(i=0; i<6; i++){
jurica238814 16:a338d2417fd5 299 if(params->advertisingData[msdOffset + 3 + i] != myMacAddress[5-i]){ // myMacAddress[0] == 0x91
jurica238814 17:51a5456a46cd 300 #if DEBUG_MAC
jurica238814 17:51a5456a46cd 301 for(i=0; i<10; i++){
jurica238814 17:51a5456a46cd 302 connectedLED = !connectedLED; // Green
jurica238814 17:51a5456a46cd 303 wait_ms(100);
jurica238814 17:51a5456a46cd 304 }
jurica238814 17:51a5456a46cd 305 #endif
jurica238814 14:d506c0679c0b 306 return 0;
jurica238814 14:d506c0679c0b 307 }
jurica238814 14:d506c0679c0b 308 }
jurica238814 17:51a5456a46cd 309 #if DEBUG_MAC
jurica238814 17:51a5456a46cd 310 for(i=0; i<10; i++){
jurica238814 17:51a5456a46cd 311 advLED = !advLED; // RED
jurica238814 17:51a5456a46cd 312 wait_ms(100);
jurica238814 17:51a5456a46cd 313 }
jurica238814 18:e844d3e6ab88 314 advLED = 1;
jurica238814 17:51a5456a46cd 315 #endif
jurica238814 14:d506c0679c0b 316 return 1;
jurica238814 14:d506c0679c0b 317 }
jurica238814 14:d506c0679c0b 318
jurica238814 3:2a4ac5b87046 319 /**
jurica238814 3:2a4ac5b87046 320 * Function is called when BLE radio discovers any kind of advertisment
jurica238814 3:2a4ac5b87046 321 */
jurica238814 3:2a4ac5b87046 322 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
jurica238814 19:abf14a5ada93 323 uint8_t msdOffset;
jurica238814 15:934a04c958f5 324
jurica238814 18:e844d3e6ab88 325 msdOffset = findMSDIndex(params); // Should be 1 or 4
jurica238814 3:2a4ac5b87046 326 if(msdOffset == 0){
jurica238814 14:d506c0679c0b 327 return; // There's no MSD in BLE advertisement data
jurica238814 3:2a4ac5b87046 328 }
jurica238814 14:d506c0679c0b 329 if ((params->advertisingData[msdOffset]) == MSD_ID){
jurica238814 1:5f34885f5cff 330 // Follows Manufacturer Specific Data
jurica238814 3:2a4ac5b87046 331 if ((params->advertisingData[msdOffset+1]) == 0x59){
jurica238814 3:2a4ac5b87046 332 if ((params->advertisingData[msdOffset+2]) == 0x00){
jurica238814 16:a338d2417fd5 333 if(CheckMac(params, myMacAddress, msdOffset)){
jurica238814 17:51a5456a46cd 334 //ble.gap().stopScan();
jurica238814 14:d506c0679c0b 335 WakeSleepT.detach();
jurica238814 22:8d106fd5fa84 336 WakeSleepT.attach(WakeMeUp, FREE_TIME_S);
jurica238814 1:5f34885f5cff 337 }
jurica238814 1:5f34885f5cff 338 }
jurica238814 1:5f34885f5cff 339 }
jurica238814 2:5504b714c9ae 340 }
jurica238814 2:5504b714c9ae 341 }
jurica238814 2:5504b714c9ae 342
jurica238814 12:6b072c2a061c 343
jurica238814 7:89c9abaa257e 344 /* Call this function few minutes (TBD) after a last scanned advertisment */
jurica238814 7:89c9abaa257e 345 void changeSleepTime(){
jurica238814 14:d506c0679c0b 346 tempSleepTime = SLEEP_TIME_S;
jurica238814 7:89c9abaa257e 347 sleepChanger.detach();
jurica238814 7:89c9abaa257e 348 }
jurica238814 7:89c9abaa257e 349
jurica238814 10:fd91664032d8 350 void startAdvertising(){
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 27:2c67f07590fd 358 ble.gap().startAdvertising();
jurica238814 27:2c67f07590fd 359 #if DEBUG
jurica238814 27:2c67f07590fd 360 advLED = 0;
jurica238814 27:2c67f07590fd 361 scanLED = 1;
jurica238814 27:2c67f07590fd 362 #endif
jurica238814 27:2c67f07590fd 363 WakeSleepT.detach();
jurica238814 27:2c67f07590fd 364 WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function
jurica238814 16:a338d2417fd5 365 }
jurica238814 10:fd91664032d8 366 }
jurica238814 10:fd91664032d8 367
jurica238814 10:fd91664032d8 368 void startScanning(){
jurica238814 10:fd91664032d8 369 ble.gap().stopAdvertising();
jurica238814 23:729717272b31 370 ble.gap().setScanInterval(SCAN_INTERVAL);
jurica238814 23:729717272b31 371 ble.gap().setScanWindow(SCAN_WINDOW);
jurica238814 23:729717272b31 372 ble.gap().setScanParams();
jurica238814 2:5504b714c9ae 373 ble.gap().startScan(advertisementCallback);
jurica238814 10:fd91664032d8 374 #if DEBUG
jurica238814 10:fd91664032d8 375 advLED = 1;
jurica238814 10:fd91664032d8 376 scanLED = 0;
jurica238814 10:fd91664032d8 377 #endif
jurica238814 2:5504b714c9ae 378 WakeSleepT.detach();
jurica238814 14:d506c0679c0b 379 WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME_S);
jurica238814 10:fd91664032d8 380 }
jurica238814 10:fd91664032d8 381
jurica238814 10:fd91664032d8 382 void WakeMeUp(){
jurica238814 8:570eb66d50b5 383 sleepFlag = 0;
jurica238814 10:fd91664032d8 384 switch(radioState){
jurica238814 10:fd91664032d8 385 case OFF:{
jurica238814 25:8ac3ff431ab1 386 radioState = SCANNING;
jurica238814 10:fd91664032d8 387 startAdvertising();
jurica238814 10:fd91664032d8 388 break;
jurica238814 10:fd91664032d8 389 }
jurica238814 10:fd91664032d8 390 case ADVERTISING:{
jurica238814 14:d506c0679c0b 391 radioState = SCANNING;
jurica238814 10:fd91664032d8 392 startScanning();
jurica238814 10:fd91664032d8 393 break;
jurica238814 10:fd91664032d8 394 }
jurica238814 10:fd91664032d8 395 case SCANNING:{
jurica238814 10:fd91664032d8 396 radioState = OFF;
jurica238814 10:fd91664032d8 397 WakeSleepT.detach();
jurica238814 16:a338d2417fd5 398 //WakeSleepT.attach(GoToSleep, FREE_TIME_S);
jurica238814 16:a338d2417fd5 399 GoToSleep();
jurica238814 10:fd91664032d8 400 break;
jurica238814 10:fd91664032d8 401 }
jurica238814 10:fd91664032d8 402 default: return;
jurica238814 10:fd91664032d8 403 }
jurica238814 2:5504b714c9ae 404 }
jurica238814 2:5504b714c9ae 405
jurica238814 14:d506c0679c0b 406 void GoToSleep(){
jurica238814 2:5504b714c9ae 407 WakeSleepT.detach();
jurica238814 7:89c9abaa257e 408 WakeSleepT.attach(WakeMeUp, tempSleepTime);
jurica238814 2:5504b714c9ae 409 ble.gap().stopAdvertising();
jurica238814 2:5504b714c9ae 410 ble.gap().stopScan();
jurica238814 8:570eb66d50b5 411 sleepFlag = 1;
jurica238814 16:a338d2417fd5 412 #if DEBUG
jurica238814 16:a338d2417fd5 413 advLED = 1;
jurica238814 16:a338d2417fd5 414 scanLED = 1;
jurica238814 16:a338d2417fd5 415 #endif
jurica238814 1:5f34885f5cff 416 }
jurica238814 1:5f34885f5cff 417
jurica238814 19:abf14a5ada93 418 #if USE_ACC
jurica238814 27:2c67f07590fd 419 void pulse_handler(){
jurica238814 27:2c67f07590fd 420 #if DEBUG_WAKEUP_BUZZER
Dautor 26:148aa2e2460c 421 buzzerStart();
jurica238814 27:2c67f07590fd 422 wait_ms(50);
Dautor 26:148aa2e2460c 423 buzzerStop();
jurica238814 27:2c67f07590fd 424 #endif
jurica238814 27:2c67f07590fd 425 #if DEBUG_ACC
jurica238814 27:2c67f07590fd 426 int_led = !int_led;
jurica238814 27:2c67f07590fd 427 #endif
jurica238814 27:2c67f07590fd 428 shushShush = false;
jurica238814 19:abf14a5ada93 429 }
jurica238814 19:abf14a5ada93 430 #endif
jurica238814 6:d14e3df498f4 431
Dautor 26:148aa2e2460c 432 int main(){
jurica238814 18:e844d3e6ab88 433 #if DEBUG || DEBUG_MAC
jurica238814 10:fd91664032d8 434 advLED = 1;
jurica238814 10:fd91664032d8 435 scanLED = 1;
jurica238814 16:a338d2417fd5 436 connectedLED = 1;
jurica238814 10:fd91664032d8 437 #endif
jurica238814 14:d506c0679c0b 438
jurica238814 19:abf14a5ada93 439 #if USE_ACC
jurica238814 19:abf14a5ada93 440 accPower = 1;
jurica238814 19:abf14a5ada93 441 i2cPower = 1;
jurica238814 27:2c67f07590fd 442 #endif
jurica238814 16:a338d2417fd5 443
jurica238814 21:10c3b8176be0 444 //WakeSleepT.attach(GoToSleep, AWAKE_TIME_S);
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);
Dautor 26:148aa2e2460c 460 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 461 wait_ms(1);
jurica238814 19:abf14a5ada93 462
jurica238814 19:abf14a5ada93 463 // Setup for the interrupt handler
jurica238814 19:abf14a5ada93 464 accPulse.rise(&pulse_handler); // -------------------------------------
jurica238814 27:2c67f07590fd 465 //acc.set_register((char)CTRL_REG_1, (char)0x03); // Flow data rate and Active mode
jurica238814 27:2c67f07590fd 466 acc.set_register((char)CTRL_REG_1, (char)0x01); // Flow data rate and Active mode
jurica238814 27:2c67f07590fd 467 acc.set_register((char)CTRL_REG_2, (char)0x03); // Set Low power mode
jurica238814 19:abf14a5ada93 468 wait(1);
jurica238814 19:abf14a5ada93 469 #endif
jurica238814 6:d14e3df498f4 470
jurica238814 2:5504b714c9ae 471 __enable_irq();
jurica238814 2:5504b714c9ae 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 27:2c67f07590fd 476 buzzerStart();
jurica238814 27:2c67f07590fd 477 wait_ms(500);
jurica238814 27:2c67f07590fd 478
jurica238814 27:2c67f07590fd 479 // Disconnect and stop PWM2
jurica238814 27:2c67f07590fd 480 /*
jurica238814 27:2c67f07590fd 481 NRF_PWM2->PSEL.OUT[0] = 0x80000000;
jurica238814 27:2c67f07590fd 482 NRF_PWM2->PSEL.OUT[1] = 0x80000000;
jurica238814 27:2c67f07590fd 483 NRF_PWM2->PSEL.OUT[2] = 0x80000000;
jurica238814 27:2c67f07590fd 484 NRF_PWM2->PSEL.OUT[3] = 0x80000000;
jurica238814 27:2c67f07590fd 485 NRF_PWM2->TASKS_STOP = 1;
jurica238814 27:2c67f07590fd 486 */
jurica238814 27:2c67f07590fd 487
jurica238814 27:2c67f07590fd 488 buzzerStop();
jurica238814 27:2c67f07590fd 489
jurica238814 27:2c67f07590fd 490
jurica238814 27:2c67f07590fd 491 while(true){
Dautor 26:148aa2e2460c 492
jurica238814 27:2c67f07590fd 493 ble.waitForEvent();
jurica238814 0:f8c1e0b2d473 494 }
jurica238814 0:f8c1e0b2d473 495 }