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