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