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