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