Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
main.cpp@26:148aa2e2460c, 2017-09-08 (annotated)
- Committer:
- Dautor
- Date:
- Fri Sep 08 10:49:50 2017 +0000
- Revision:
- 26:148aa2e2460c
- Parent:
- 25:8ac3ff431ab1
- Child:
- 27:2c67f07590fd
Accelerometer and shush protocol.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
Dautor | 26:148aa2e2460c | 19 | #define DEBUG (1) |
Dautor | 26:148aa2e2460c | 20 | #define DEBUG_ACC (1) |
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 | |
Dautor | 26:148aa2e2460c | 26 | #define USE_ACC (1) |
jurica238814 | 8:570eb66d50b5 | 27 | |
jurica238814 | 25:8ac3ff431ab1 | 28 | #define SLEEP_TIME_S (8.00) /* Sleep time (in s) */ |
jurica238814 | 25:8ac3ff431ab1 | 29 | #define ADV_TIMER_TIME_S (0.50) /* 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 | 23:729717272b31 | 35 | #define BUZZ_PERIOD_US (250) |
jurica238814 | 23:729717272b31 | 36 | #define BUZZ_DURATION_MS (1000) |
jurica238814 | 15:934a04c958f5 | 37 | #define MAC_SIZE_B (6) |
jurica238814 | 12:6b072c2a061c | 38 | |
jurica238814 | 1:5f34885f5cff | 39 | /* Static constants for the BLE example */ |
jurica238814 | 3:2a4ac5b87046 | 40 | #define MAX_BLE_PACKET_SIZE (31) |
jurica238814 | 3:2a4ac5b87046 | 41 | #define MSD_SIZE (18) |
jurica238814 | 3:2a4ac5b87046 | 42 | #define MSD_ID (0xFF) |
jurica238814 | 12:6b072c2a061c | 43 | |
Dautor | 26:148aa2e2460c | 44 | #define BUZZ_TIME_S (1) /* Buzz time in s */ |
jurica238814 | 25:8ac3ff431ab1 | 45 | #define ADV_INTERVAL (100) /* Advertising interval (in ms) */ |
jurica238814 | 14:d506c0679c0b | 46 | #define SCAN_INTERVAL (SCAN_TIMER_TIME_S) /* Scan interval (in ms) */ |
jurica238814 | 14:d506c0679c0b | 47 | #define SCAN_WINDOW (SCAN_TIMER_TIME_S) |
jurica238814 | 1:5f34885f5cff | 48 | |
jurica238814 | 6:d14e3df498f4 | 49 | /* Static constants for the accelerometer */ |
jurica238814 | 13:d51127eed926 | 50 | #define WHO_AM_I 0x0D /* Type 'read' : This should return the device id of 0x2A */ |
jurica238814 | 13:d51127eed926 | 51 | #define OUT_Z_MSB 0x05 /* Type 'read' : z axis - 8 most significatn bit of a 12 bit sample */ |
jurica238814 | 6:d14e3df498f4 | 52 | #define I2C_DATA (p29) |
jurica238814 | 6:d14e3df498f4 | 53 | #define I2C_CLK (p2) |
jurica238814 | 6:d14e3df498f4 | 54 | #define INT2_PIN (p4) |
jurica238814 | 10:fd91664032d8 | 55 | #define BUZZER (p31) |
jurica238814 | 0:f8c1e0b2d473 | 56 | |
Dautor | 26:148aa2e2460c | 57 | #define BUZZER_FREQUENCY_Hz (1500) |
Dautor | 26:148aa2e2460c | 58 | |
Dautor | 26:148aa2e2460c | 59 | #if DEBUG_PRINT_UART |
Dautor | 26:148aa2e2460c | 60 | #include "nrf52_uart.h" |
Dautor | 26:148aa2e2460c | 61 | NRF52_UART uart(p25, p26, Baud9600); |
Dautor | 26:148aa2e2460c | 62 | char buffer[255]; |
Dautor | 26:148aa2e2460c | 63 | #define SEND(...) {uint8_t len = sprintf(buffer, __VA_ARGS__); uart.send(buffer, len);} |
Dautor | 26:148aa2e2460c | 64 | #else |
Dautor | 26:148aa2e2460c | 65 | #define SEND(...) |
jurica238814 | 16:a338d2417fd5 | 66 | #endif |
jurica238814 | 16:a338d2417fd5 | 67 | |
Dautor | 26:148aa2e2460c | 68 | bool beaconStateActive = 1; |
jurica238814 | 16:a338d2417fd5 | 69 | |
jurica238814 | 16:a338d2417fd5 | 70 | bool shushShush = false; |
jurica238814 | 18:e844d3e6ab88 | 71 | const static uint16_t ACK_CHARA_UUID = 0xA001; |
jurica238814 | 16:a338d2417fd5 | 72 | |
jurica238814 | 18:e844d3e6ab88 | 73 | uint8_t txPower = 4; |
jurica238814 | 18:e844d3e6ab88 | 74 | uint8_t sleepFlag = false; |
jurica238814 | 14:d506c0679c0b | 75 | uint8_t tempSleepTime = SLEEP_TIME_S; |
jurica238814 | 14:d506c0679c0b | 76 | 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 | 77 | uint8_t startBuzz[2] = {0xBA, 0xBE}; |
jurica238814 | 25:8ac3ff431ab1 | 78 | uint8_t stopBuzz[2] = {0xDE, 0xAD}; |
jurica238814 | 23:729717272b31 | 79 | uint8_t myMacAddress[6] = {}; |
jurica238814 | 6:d14e3df498f4 | 80 | uint8_t buzzer_flag = 0; |
jurica238814 | 0:f8c1e0b2d473 | 81 | |
jurica238814 | 14:d506c0679c0b | 82 | enum RadioState{ |
jurica238814 | 10:fd91664032d8 | 83 | OFF, |
jurica238814 | 10:fd91664032d8 | 84 | ADVERTISING, |
Dautor | 26:148aa2e2460c | 85 | SCANNING, |
Dautor | 26:148aa2e2460c | 86 | }; |
jurica238814 | 14:d506c0679c0b | 87 | enum RadioState radioState = OFF; |
jurica238814 | 0:f8c1e0b2d473 | 88 | |
jurica238814 | 14:d506c0679c0b | 89 | void GoToSleep(); |
jurica238814 | 14:d506c0679c0b | 90 | void StartAdvertising(); |
jurica238814 | 10:fd91664032d8 | 91 | void startScanning(); |
jurica238814 | 10:fd91664032d8 | 92 | void WakeMeUp(); |
jurica238814 | 0:f8c1e0b2d473 | 93 | |
Dautor | 26:148aa2e2460c | 94 | |
jurica238814 | 2:5504b714c9ae | 95 | Ticker WakeSleepT; |
jurica238814 | 7:89c9abaa257e | 96 | Ticker sleepChanger; |
jurica238814 | 19:abf14a5ada93 | 97 | #if USE_ACC |
jurica238814 | 19:abf14a5ada93 | 98 | DigitalOut accPower(p7); |
jurica238814 | 19:abf14a5ada93 | 99 | DigitalOut i2cPower(p5); |
jurica238814 | 19:abf14a5ada93 | 100 | InterruptIn accPulse(INT2_PIN); |
jurica238814 | 21:10c3b8176be0 | 101 | Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS); |
jurica238814 | 19:abf14a5ada93 | 102 | #endif |
jurica238814 | 1:5f34885f5cff | 103 | BLE &ble = BLE::Instance(); |
jurica238814 | 16:a338d2417fd5 | 104 | ACKService<4> *ackServicePtr; |
jurica238814 | 10:fd91664032d8 | 105 | |
jurica238814 | 22:8d106fd5fa84 | 106 | #if DEBUG || DEBUG_MAC || DEBUG_CONNECTION |
Dautor | 26:148aa2e2460c | 107 | NRF52_DigitalOut advLED(p22); // Red |
Dautor | 26:148aa2e2460c | 108 | NRF52_DigitalOut scanLED(p23); // Blue |
Dautor | 26:148aa2e2460c | 109 | NRF52_DigitalOut connectedLED(p24); // Green |
jurica238814 | 10:fd91664032d8 | 110 | #endif |
jurica238814 | 18:e844d3e6ab88 | 111 | |
jurica238814 | 10:fd91664032d8 | 112 | #if DEBUG_ACC |
Dautor | 26:148aa2e2460c | 113 | NRF52_DigitalOut int_led(p22); |
Dautor | 26:148aa2e2460c | 114 | NRF52_DigitalOut act_led(p22); |
jurica238814 | 8:570eb66d50b5 | 115 | #endif |
jurica238814 | 8:570eb66d50b5 | 116 | |
Dautor | 26:148aa2e2460c | 117 | NRF52_PWM buzzer(NRF_PWM0); |
Dautor | 26:148aa2e2460c | 118 | |
Dautor | 26:148aa2e2460c | 119 | void buzzerStart(){ |
Dautor | 26:148aa2e2460c | 120 | buzzer.enable(3000); |
Dautor | 26:148aa2e2460c | 121 | buzzer.enableChannel(0, BUZZER); |
Dautor | 26:148aa2e2460c | 122 | buzzer.setDuty(0, 0.5f); |
Dautor | 26:148aa2e2460c | 123 | } |
Dautor | 26:148aa2e2460c | 124 | void buzzerStop(){ |
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]){ |
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); |
Dautor | 26:148aa2e2460c | 198 | buzzerStop(); |
jurica238814 | 25:8ac3ff431ab1 | 199 | } |
jurica238814 | 25:8ac3ff431ab1 | 200 | } |
Dautor | 26:148aa2e2460c | 201 | else if(params->data[0] == 0x55){ |
Dautor | 26:148aa2e2460c | 202 | beaconStateActive = 0; |
Dautor | 26:148aa2e2460c | 203 | ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION); |
Dautor | 26:148aa2e2460c | 204 | } |
Dautor | 26:148aa2e2460c | 205 | |
jurica238814 | 16:a338d2417fd5 | 206 | } |
jurica238814 | 16:a338d2417fd5 | 207 | else{ |
jurica238814 | 25:8ac3ff431ab1 | 208 | // Execute this for wrong data written into characteristic |
jurica238814 | 25:8ac3ff431ab1 | 209 | return; |
jurica238814 | 16:a338d2417fd5 | 210 | } |
jurica238814 | 16:a338d2417fd5 | 211 | } |
jurica238814 | 0:f8c1e0b2d473 | 212 | |
jurica238814 | 0:f8c1e0b2d473 | 213 | /** |
jurica238814 | 0:f8c1e0b2d473 | 214 | * This function is called when the ble initialization process has failed |
jurica238814 | 0:f8c1e0b2d473 | 215 | */ |
jurica238814 | 0:f8c1e0b2d473 | 216 | void onBleInitError(BLE &ble, ble_error_t error){ |
jurica238814 | 0:f8c1e0b2d473 | 217 | /* Avoid compiler warnings */ |
jurica238814 | 0:f8c1e0b2d473 | 218 | (void) ble; |
jurica238814 | 0:f8c1e0b2d473 | 219 | (void) error; |
jurica238814 | 0:f8c1e0b2d473 | 220 | /* Initialization error handling should go here */ |
jurica238814 | 0:f8c1e0b2d473 | 221 | } |
jurica238814 | 0:f8c1e0b2d473 | 222 | |
jurica238814 | 0:f8c1e0b2d473 | 223 | /** |
jurica238814 | 0:f8c1e0b2d473 | 224 | * Callback triggered when the ble initialization process has finished |
jurica238814 | 0:f8c1e0b2d473 | 225 | */ |
jurica238814 | 0:f8c1e0b2d473 | 226 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
jurica238814 | 0:f8c1e0b2d473 | 227 | BLE& ble = params->ble; |
jurica238814 | 0:f8c1e0b2d473 | 228 | ble_error_t error = params->error; |
jurica238814 | 0:f8c1e0b2d473 | 229 | |
jurica238814 | 0:f8c1e0b2d473 | 230 | if (error != BLE_ERROR_NONE) { |
jurica238814 | 0:f8c1e0b2d473 | 231 | /* In case of error, forward the error handling to onBleInitError */ |
jurica238814 | 0:f8c1e0b2d473 | 232 | onBleInitError(ble, error); |
jurica238814 | 0:f8c1e0b2d473 | 233 | return; |
jurica238814 | 0:f8c1e0b2d473 | 234 | } |
jurica238814 | 0:f8c1e0b2d473 | 235 | |
jurica238814 | 0:f8c1e0b2d473 | 236 | /* Ensure that it is the default instance of BLE */ |
jurica238814 | 0:f8c1e0b2d473 | 237 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
jurica238814 | 0:f8c1e0b2d473 | 238 | return; |
jurica238814 | 0:f8c1e0b2d473 | 239 | } |
jurica238814 | 16:a338d2417fd5 | 240 | |
jurica238814 | 16:a338d2417fd5 | 241 | uint8_t init_values[4] = {0,0,0,0}; |
jurica238814 | 1:5f34885f5cff | 242 | /* Get my MAC address */ |
jurica238814 | 1:5f34885f5cff | 243 | BLEProtocol::AddressType_t temp_address_type; |
jurica238814 | 14:d506c0679c0b | 244 | ble.gap().getAddress(&temp_address_type, myMacAddress); |
jurica238814 | 16:a338d2417fd5 | 245 | ackServicePtr = new ACKService<4>(ble, init_values); |
jurica238814 | 16:a338d2417fd5 | 246 | ackServicePtr->updateMacAddress(myMacAddress); // Update MAC address |
jurica238814 | 21:10c3b8176be0 | 247 | |
jurica238814 | 16:a338d2417fd5 | 248 | ble.gap().onDisconnection(disconnectionCallback); |
jurica238814 | 17:51a5456a46cd | 249 | ble.gap().onConnection(onConnectionCallback); // -->> Uncomment these two lines for shush-shush |
jurica238814 | 17:51a5456a46cd | 250 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
jurica238814 | 1:5f34885f5cff | 251 | |
jurica238814 | 14:d506c0679c0b | 252 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)msd, MSD_SIZE); |
jurica238814 | 10:fd91664032d8 | 253 | ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms! |
jurica238814 | 0:f8c1e0b2d473 | 254 | } |
jurica238814 | 0:f8c1e0b2d473 | 255 | |
jurica238814 | 3:2a4ac5b87046 | 256 | |
jurica238814 | 3:2a4ac5b87046 | 257 | uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){ |
jurica238814 | 1:5f34885f5cff | 258 | uint8_t i=0; |
jurica238814 | 18:e844d3e6ab88 | 259 | uint8_t advLen = params->advertisingDataLen; |
jurica238814 | 18:e844d3e6ab88 | 260 | uint8_t dataLen; |
jurica238814 | 18:e844d3e6ab88 | 261 | |
jurica238814 | 18:e844d3e6ab88 | 262 | if((advLen < (MAC_SIZE_B + 2)) || advLen == 0){ |
jurica238814 | 15:934a04c958f5 | 263 | // Empty advertisement or not long enough for MAX |
jurica238814 | 18:e844d3e6ab88 | 264 | // +2 for SIZE and MSD ID |
jurica238814 | 15:934a04c958f5 | 265 | return 0; |
jurica238814 | 15:934a04c958f5 | 266 | } |
jurica238814 | 3:2a4ac5b87046 | 267 | |
jurica238814 | 3:2a4ac5b87046 | 268 | do{ |
jurica238814 | 18:e844d3e6ab88 | 269 | dataLen = params->advertisingData[i]; |
jurica238814 | 3:2a4ac5b87046 | 270 | i++; |
jurica238814 | 3:2a4ac5b87046 | 271 | if(params->advertisingData[i] == MSD_ID) return i; |
jurica238814 | 18:e844d3e6ab88 | 272 | else i += (dataLen); |
jurica238814 | 18:e844d3e6ab88 | 273 | }while(i<advLen); |
jurica238814 | 3:2a4ac5b87046 | 274 | |
jurica238814 | 3:2a4ac5b87046 | 275 | return 0; |
jurica238814 | 3:2a4ac5b87046 | 276 | } |
jurica238814 | 3:2a4ac5b87046 | 277 | |
jurica238814 | 14:d506c0679c0b | 278 | uint8_t CheckMac(const Gap::AdvertisementCallbackParams_t *params, uint8_t *myMacAddress, uint8_t msdOffset){ |
jurica238814 | 14:d506c0679c0b | 279 | int i=0; |
jurica238814 | 14:d506c0679c0b | 280 | |
jurica238814 | 16:a338d2417fd5 | 281 | /* Get my MAC address */ |
jurica238814 | 16:a338d2417fd5 | 282 | BLEProtocol::AddressType_t temp_address_type; |
jurica238814 | 16:a338d2417fd5 | 283 | ble.gap().getAddress(&temp_address_type, myMacAddress); |
jurica238814 | 16:a338d2417fd5 | 284 | |
jurica238814 | 18:e844d3e6ab88 | 285 | if(!msdOffset){ |
jurica238814 | 17:51a5456a46cd | 286 | #if DEBUG_MAC |
jurica238814 | 16:a338d2417fd5 | 287 | for(i=0; i<10; i++){ |
jurica238814 | 16:a338d2417fd5 | 288 | scanLED = !scanLED; // BLUE |
jurica238814 | 16:a338d2417fd5 | 289 | wait_ms(100); |
jurica238814 | 16:a338d2417fd5 | 290 | } |
jurica238814 | 17:51a5456a46cd | 291 | #endif |
jurica238814 | 14:d506c0679c0b | 292 | return 0; // There's no MSD in BLE advertisement data |
jurica238814 | 14:d506c0679c0b | 293 | } |
jurica238814 | 14:d506c0679c0b | 294 | for(i=0; i<6; i++){ |
jurica238814 | 16:a338d2417fd5 | 295 | if(params->advertisingData[msdOffset + 3 + i] != myMacAddress[5-i]){ // myMacAddress[0] == 0x91 |
jurica238814 | 17:51a5456a46cd | 296 | #if DEBUG_MAC |
jurica238814 | 17:51a5456a46cd | 297 | for(i=0; i<10; i++){ |
jurica238814 | 17:51a5456a46cd | 298 | connectedLED = !connectedLED; // Green |
jurica238814 | 17:51a5456a46cd | 299 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 300 | } |
jurica238814 | 17:51a5456a46cd | 301 | #endif |
jurica238814 | 14:d506c0679c0b | 302 | return 0; |
jurica238814 | 14:d506c0679c0b | 303 | } |
jurica238814 | 14:d506c0679c0b | 304 | } |
jurica238814 | 17:51a5456a46cd | 305 | #if DEBUG_MAC |
jurica238814 | 17:51a5456a46cd | 306 | for(i=0; i<10; i++){ |
jurica238814 | 17:51a5456a46cd | 307 | advLED = !advLED; // RED |
jurica238814 | 17:51a5456a46cd | 308 | wait_ms(100); |
jurica238814 | 17:51a5456a46cd | 309 | } |
jurica238814 | 18:e844d3e6ab88 | 310 | advLED = 1; |
jurica238814 | 17:51a5456a46cd | 311 | #endif |
jurica238814 | 14:d506c0679c0b | 312 | return 1; |
jurica238814 | 14:d506c0679c0b | 313 | } |
jurica238814 | 14:d506c0679c0b | 314 | |
jurica238814 | 3:2a4ac5b87046 | 315 | /** |
jurica238814 | 3:2a4ac5b87046 | 316 | * Function is called when BLE radio discovers any kind of advertisment |
jurica238814 | 3:2a4ac5b87046 | 317 | */ |
jurica238814 | 3:2a4ac5b87046 | 318 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){ |
jurica238814 | 19:abf14a5ada93 | 319 | uint8_t msdOffset; |
jurica238814 | 15:934a04c958f5 | 320 | |
jurica238814 | 18:e844d3e6ab88 | 321 | msdOffset = findMSDIndex(params); // Should be 1 or 4 |
jurica238814 | 3:2a4ac5b87046 | 322 | if(msdOffset == 0){ |
jurica238814 | 14:d506c0679c0b | 323 | return; // There's no MSD in BLE advertisement data |
jurica238814 | 3:2a4ac5b87046 | 324 | } |
jurica238814 | 14:d506c0679c0b | 325 | if ((params->advertisingData[msdOffset]) == MSD_ID){ |
jurica238814 | 1:5f34885f5cff | 326 | // Follows Manufacturer Specific Data |
jurica238814 | 3:2a4ac5b87046 | 327 | if ((params->advertisingData[msdOffset+1]) == 0x59){ |
jurica238814 | 3:2a4ac5b87046 | 328 | if ((params->advertisingData[msdOffset+2]) == 0x00){ |
jurica238814 | 16:a338d2417fd5 | 329 | if(CheckMac(params, myMacAddress, msdOffset)){ |
jurica238814 | 17:51a5456a46cd | 330 | //ble.gap().stopScan(); |
jurica238814 | 14:d506c0679c0b | 331 | WakeSleepT.detach(); |
jurica238814 | 22:8d106fd5fa84 | 332 | WakeSleepT.attach(WakeMeUp, FREE_TIME_S); |
jurica238814 | 1:5f34885f5cff | 333 | } |
jurica238814 | 1:5f34885f5cff | 334 | } |
jurica238814 | 1:5f34885f5cff | 335 | } |
jurica238814 | 2:5504b714c9ae | 336 | } |
jurica238814 | 2:5504b714c9ae | 337 | } |
jurica238814 | 2:5504b714c9ae | 338 | |
jurica238814 | 12:6b072c2a061c | 339 | |
jurica238814 | 7:89c9abaa257e | 340 | /* Call this function few minutes (TBD) after a last scanned advertisment */ |
jurica238814 | 7:89c9abaa257e | 341 | void changeSleepTime(){ |
jurica238814 | 14:d506c0679c0b | 342 | tempSleepTime = SLEEP_TIME_S; |
jurica238814 | 7:89c9abaa257e | 343 | sleepChanger.detach(); |
jurica238814 | 7:89c9abaa257e | 344 | } |
jurica238814 | 7:89c9abaa257e | 345 | |
jurica238814 | 10:fd91664032d8 | 346 | void startAdvertising(){ |
jurica238814 | 19:abf14a5ada93 | 347 | #if USE_ACC |
jurica238814 | 19:abf14a5ada93 | 348 | i2cPower = 1; |
jurica238814 | 19:abf14a5ada93 | 349 | #endif |
jurica238814 | 19:abf14a5ada93 | 350 | wait_ms(10); |
jurica238814 | 19:abf14a5ada93 | 351 | |
jurica238814 | 16:a338d2417fd5 | 352 | if(shushShush){ |
jurica238814 | 16:a338d2417fd5 | 353 | // Do not advertise! Go to sleep |
jurica238814 | 16:a338d2417fd5 | 354 | WakeSleepT.detach(); |
jurica238814 | 16:a338d2417fd5 | 355 | ble.gap().stopAdvertising(); |
jurica238814 | 16:a338d2417fd5 | 356 | WakeMeUp(); |
jurica238814 | 16:a338d2417fd5 | 357 | } |
jurica238814 | 16:a338d2417fd5 | 358 | else{ |
jurica238814 | 16:a338d2417fd5 | 359 | ble.gap().startAdvertising(); |
jurica238814 | 16:a338d2417fd5 | 360 | #if DEBUG |
jurica238814 | 16:a338d2417fd5 | 361 | advLED = 0; |
jurica238814 | 16:a338d2417fd5 | 362 | scanLED = 1; |
jurica238814 | 16:a338d2417fd5 | 363 | #endif |
jurica238814 | 16:a338d2417fd5 | 364 | WakeSleepT.detach(); |
jurica238814 | 16:a338d2417fd5 | 365 | WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME_S); // Call the wakeMeUp function |
jurica238814 | 16:a338d2417fd5 | 366 | } |
jurica238814 | 10:fd91664032d8 | 367 | } |
jurica238814 | 10:fd91664032d8 | 368 | |
jurica238814 | 10:fd91664032d8 | 369 | void startScanning(){ |
jurica238814 | 10:fd91664032d8 | 370 | ble.gap().stopAdvertising(); |
jurica238814 | 23:729717272b31 | 371 | ble.gap().setScanInterval(SCAN_INTERVAL); |
jurica238814 | 23:729717272b31 | 372 | ble.gap().setScanWindow(SCAN_WINDOW); |
jurica238814 | 23:729717272b31 | 373 | ble.gap().setScanParams(); |
jurica238814 | 2:5504b714c9ae | 374 | ble.gap().startScan(advertisementCallback); |
jurica238814 | 10:fd91664032d8 | 375 | #if DEBUG |
jurica238814 | 10:fd91664032d8 | 376 | advLED = 1; |
jurica238814 | 10:fd91664032d8 | 377 | scanLED = 0; |
jurica238814 | 10:fd91664032d8 | 378 | #endif |
jurica238814 | 2:5504b714c9ae | 379 | WakeSleepT.detach(); |
jurica238814 | 14:d506c0679c0b | 380 | WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME_S); |
jurica238814 | 10:fd91664032d8 | 381 | } |
jurica238814 | 10:fd91664032d8 | 382 | |
jurica238814 | 10:fd91664032d8 | 383 | void WakeMeUp(){ |
jurica238814 | 8:570eb66d50b5 | 384 | sleepFlag = 0; |
jurica238814 | 10:fd91664032d8 | 385 | switch(radioState){ |
jurica238814 | 10:fd91664032d8 | 386 | case OFF:{ |
jurica238814 | 25:8ac3ff431ab1 | 387 | radioState = SCANNING; |
jurica238814 | 10:fd91664032d8 | 388 | startAdvertising(); |
jurica238814 | 10:fd91664032d8 | 389 | break; |
jurica238814 | 10:fd91664032d8 | 390 | } |
jurica238814 | 10:fd91664032d8 | 391 | case ADVERTISING:{ |
jurica238814 | 14:d506c0679c0b | 392 | radioState = SCANNING; |
jurica238814 | 10:fd91664032d8 | 393 | startScanning(); |
jurica238814 | 10:fd91664032d8 | 394 | break; |
jurica238814 | 10:fd91664032d8 | 395 | } |
jurica238814 | 10:fd91664032d8 | 396 | case SCANNING:{ |
jurica238814 | 10:fd91664032d8 | 397 | radioState = OFF; |
jurica238814 | 10:fd91664032d8 | 398 | WakeSleepT.detach(); |
jurica238814 | 16:a338d2417fd5 | 399 | //WakeSleepT.attach(GoToSleep, FREE_TIME_S); |
jurica238814 | 16:a338d2417fd5 | 400 | GoToSleep(); |
jurica238814 | 10:fd91664032d8 | 401 | break; |
jurica238814 | 10:fd91664032d8 | 402 | } |
jurica238814 | 10:fd91664032d8 | 403 | default: return; |
jurica238814 | 10:fd91664032d8 | 404 | } |
jurica238814 | 2:5504b714c9ae | 405 | } |
jurica238814 | 2:5504b714c9ae | 406 | |
jurica238814 | 14:d506c0679c0b | 407 | void GoToSleep(){ |
jurica238814 | 2:5504b714c9ae | 408 | WakeSleepT.detach(); |
jurica238814 | 7:89c9abaa257e | 409 | WakeSleepT.attach(WakeMeUp, tempSleepTime); |
jurica238814 | 2:5504b714c9ae | 410 | ble.gap().stopAdvertising(); |
jurica238814 | 2:5504b714c9ae | 411 | ble.gap().stopScan(); |
jurica238814 | 8:570eb66d50b5 | 412 | sleepFlag = 1; |
jurica238814 | 16:a338d2417fd5 | 413 | #if DEBUG |
jurica238814 | 16:a338d2417fd5 | 414 | advLED = 1; |
jurica238814 | 16:a338d2417fd5 | 415 | scanLED = 1; |
jurica238814 | 16:a338d2417fd5 | 416 | #endif |
jurica238814 | 1:5f34885f5cff | 417 | } |
jurica238814 | 1:5f34885f5cff | 418 | |
jurica238814 | 19:abf14a5ada93 | 419 | #if USE_ACC |
Dautor | 26:148aa2e2460c | 420 | void pulse_handler(){ |
Dautor | 26:148aa2e2460c | 421 | #if DEBUG_WAKEUP_BUZZER |
Dautor | 26:148aa2e2460c | 422 | if(beaconStateActive == 0){ |
Dautor | 26:148aa2e2460c | 423 | buzzerStart(); |
Dautor | 26:148aa2e2460c | 424 | wait_ms(500); |
Dautor | 26:148aa2e2460c | 425 | buzzerStop(); |
Dautor | 26:148aa2e2460c | 426 | beaconStateActive = 1; |
jurica238814 | 19:abf14a5ada93 | 427 | } |
jurica238814 | 19:abf14a5ada93 | 428 | #endif |
Dautor | 26:148aa2e2460c | 429 | #if DEBUG_ACC |
Dautor | 26:148aa2e2460c | 430 | int_led = !int_led; |
Dautor | 26:148aa2e2460c | 431 | #endif |
Dautor | 26:148aa2e2460c | 432 | } |
Dautor | 26:148aa2e2460c | 433 | #endif |
jurica238814 | 6:d14e3df498f4 | 434 | |
Dautor | 26:148aa2e2460c | 435 | int main(){ |
jurica238814 | 18:e844d3e6ab88 | 436 | #if DEBUG || DEBUG_MAC |
jurica238814 | 10:fd91664032d8 | 437 | advLED = 1; |
jurica238814 | 10:fd91664032d8 | 438 | scanLED = 1; |
jurica238814 | 16:a338d2417fd5 | 439 | connectedLED = 1; |
jurica238814 | 10:fd91664032d8 | 440 | #endif |
jurica238814 | 14:d506c0679c0b | 441 | |
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 | |
Dautor | 26:148aa2e2460c | 447 | int_led = 0; |
jurica238814 | 16:a338d2417fd5 | 448 | |
jurica238814 | 21:10c3b8176be0 | 449 | //WakeSleepT.attach(GoToSleep, AWAKE_TIME_S); |
jurica238814 | 21:10c3b8176be0 | 450 | GoToSleep(); |
jurica238814 | 16:a338d2417fd5 | 451 | ble.init(bleInitComplete); |
jurica238814 | 16:a338d2417fd5 | 452 | ble.gap().setTxPower(txPower); |
jurica238814 | 16:a338d2417fd5 | 453 | |
jurica238814 | 19:abf14a5ada93 | 454 | #if USE_ACC |
jurica238814 | 19:abf14a5ada93 | 455 | // Pulse interrupt detection |
Dautor | 26:148aa2e2460c | 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); |
Dautor | 26:148aa2e2460c | 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); |
Dautor | 26:148aa2e2460c | 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); |
Dautor | 26:148aa2e2460c | 462 | acc.set_register((char)FF_COUNT, (char)0x08); // Set Counter degister value (10ms) |
jurica238814 | 19:abf14a5ada93 | 463 | wait_ms(1); |
Dautor | 26:148aa2e2460c | 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); // ------------------------------------- |
Dautor | 26:148aa2e2460c | 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. */ |
Dautor | 26:148aa2e2460c | 476 | while (ble.hasInitialized() == false){ /* spin loop */ } |
Dautor | 26:148aa2e2460c | 477 | |
jurica238814 | 1:5f34885f5cff | 478 | while(true){ |
Dautor | 26:148aa2e2460c | 479 | if(beaconStateActive){ |
Dautor | 26:148aa2e2460c | 480 | // wake up |
Dautor | 26:148aa2e2460c | 481 | |
Dautor | 26:148aa2e2460c | 482 | startAdvertising(); |
Dautor | 26:148aa2e2460c | 483 | while(beaconStateActive){ |
Dautor | 26:148aa2e2460c | 484 | if(sleepFlag){ |
Dautor | 26:148aa2e2460c | 485 | #if USE_ACC |
Dautor | 26:148aa2e2460c | 486 | i2cPower = 0; |
Dautor | 26:148aa2e2460c | 487 | #endif |
Dautor | 26:148aa2e2460c | 488 | ble.waitForEvent(); |
Dautor | 26:148aa2e2460c | 489 | __WFI(); |
Dautor | 26:148aa2e2460c | 490 | } |
Dautor | 26:148aa2e2460c | 491 | else{ |
Dautor | 26:148aa2e2460c | 492 | ble.waitForEvent(); |
Dautor | 26:148aa2e2460c | 493 | } |
Dautor | 26:148aa2e2460c | 494 | } |
Dautor | 26:148aa2e2460c | 495 | } |
Dautor | 26:148aa2e2460c | 496 | |
Dautor | 26:148aa2e2460c | 497 | scanLED = 0; |
Dautor | 26:148aa2e2460c | 498 | // prepare for sleep |
Dautor | 26:148aa2e2460c | 499 | |
Dautor | 26:148aa2e2460c | 500 | // disable everything |
Dautor | 26:148aa2e2460c | 501 | i2cPower = 0; |
Dautor | 26:148aa2e2460c | 502 | WakeSleepT.detach(); |
Dautor | 26:148aa2e2460c | 503 | // set accelerometer refresh rate to lowest |
Dautor | 26:148aa2e2460c | 504 | |
Dautor | 26:148aa2e2460c | 505 | // do not wake up until beaconStateActive is set by the accelerometer |
Dautor | 26:148aa2e2460c | 506 | while(!beaconStateActive){ |
jurica238814 | 9:2ab2be19add9 | 507 | __WFI(); |
jurica238814 | 9:2ab2be19add9 | 508 | } |
Dautor | 26:148aa2e2460c | 509 | scanLED = 1; |
jurica238814 | 0:f8c1e0b2d473 | 510 | } |
jurica238814 | 0:f8c1e0b2d473 | 511 | } |