Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
main.cpp@11:92a9fffd5015, 2017-07-25 (annotated)
- Committer:
- jurica238814
- Date:
- Tue Jul 25 11:29:01 2017 +0000
- Revision:
- 11:92a9fffd5015
- Parent:
- 10:fd91664032d8
- Child:
- 12:6b072c2a061c
Stable version. Before merge.
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 | 6:d14e3df498f4 | 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 | 1:5f34885f5cff | 14 | |
jurica238814 | 10:fd91664032d8 | 15 | #define DEBUG (1) |
jurica238814 | 10:fd91664032d8 | 16 | #define DEBUG_ACC (0) |
jurica238814 | 8:570eb66d50b5 | 17 | |
jurica238814 | 10:fd91664032d8 | 18 | #define SLEEP_TIME (2.0) // Sleep time in seconds WAS 0.85 |
jurica238814 | 11:92a9fffd5015 | 19 | #define ADV_TIMER_TIME (0.1) // Advertising time (in s) |
jurica238814 | 11:92a9fffd5015 | 20 | #define SCAN_TIMER_TIME (0.5) // Scanning time (in s) |
jurica238814 | 10:fd91664032d8 | 21 | #define FREE_TIME (0.01) // Time between end of a scanning and sleep mode |
jurica238814 | 11:92a9fffd5015 | 22 | #define AWAKE_TIME (ADV_TIMER_TIME+SCAN_TIMER_TIME+FREE_TIME) // Was 0.15 |
jurica238814 | 0:f8c1e0b2d473 | 23 | |
jurica238814 | 1:5f34885f5cff | 24 | /* Static constants for the BLE example */ |
jurica238814 | 3:2a4ac5b87046 | 25 | #define MAX_BLE_PACKET_SIZE (31) |
jurica238814 | 3:2a4ac5b87046 | 26 | #define MSD_SIZE (18) |
jurica238814 | 3:2a4ac5b87046 | 27 | #define MSD_ID (0xFF) |
jurica238814 | 3:2a4ac5b87046 | 28 | #define BUZZ_TIME (1.0) // Buzz time in s |
jurica238814 | 10:fd91664032d8 | 29 | #define ADV_INTERVAL (100) // Advertising interval (in ms) |
jurica238814 | 10:fd91664032d8 | 30 | #define SCAN_INTERVAL (100) // Scan interval (in ms) |
jurica238814 | 10:fd91664032d8 | 31 | #define SCAN_WINDOW (50) |
jurica238814 | 1:5f34885f5cff | 32 | |
jurica238814 | 6:d14e3df498f4 | 33 | /* Static constants for the accelerometer */ |
jurica238814 | 6:d14e3df498f4 | 34 | #define WHO_AM_I 0x0D // Type 'read' : This should return the device id of 0x2A |
jurica238814 | 6:d14e3df498f4 | 35 | #define OUT_Z_MSB 0x05 // Type 'read' : z axis - 8 most significatn bit of a 12 bit sample |
jurica238814 | 6:d14e3df498f4 | 36 | #define I2C_DATA (p29) |
jurica238814 | 6:d14e3df498f4 | 37 | #define I2C_CLK (p2) |
jurica238814 | 6:d14e3df498f4 | 38 | #define INT2_PIN (p4) |
jurica238814 | 10:fd91664032d8 | 39 | #define BUZZER (p31) |
jurica238814 | 0:f8c1e0b2d473 | 40 | |
jurica238814 | 10:fd91664032d8 | 41 | uint8_t sleepFlag = 0; |
jurica238814 | 0:f8c1e0b2d473 | 42 | int8_t txPower = 4; |
jurica238814 | 1:5f34885f5cff | 43 | uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00}; |
jurica238814 | 6:d14e3df498f4 | 44 | uint8_t my_mac_address[6] = {}; |
jurica238814 | 6:d14e3df498f4 | 45 | uint8_t buzzer_flag = 0; |
jurica238814 | 0:f8c1e0b2d473 | 46 | |
jurica238814 | 10:fd91664032d8 | 47 | enum _radioState{ |
jurica238814 | 10:fd91664032d8 | 48 | OFF, |
jurica238814 | 10:fd91664032d8 | 49 | ADVERTISING, |
jurica238814 | 10:fd91664032d8 | 50 | SCANNING |
jurica238814 | 10:fd91664032d8 | 51 | }; |
jurica238814 | 10:fd91664032d8 | 52 | enum _radioState radioState = OFF; |
jurica238814 | 10:fd91664032d8 | 53 | |
jurica238814 | 2:5504b714c9ae | 54 | void turnBuzzOff(void); |
jurica238814 | 2:5504b714c9ae | 55 | void goToSleep(); |
jurica238814 | 10:fd91664032d8 | 56 | void startAdvertising(); |
jurica238814 | 10:fd91664032d8 | 57 | void startScanning(); |
jurica238814 | 10:fd91664032d8 | 58 | void WakeMeUp(); |
jurica238814 | 0:f8c1e0b2d473 | 59 | |
jurica238814 | 2:5504b714c9ae | 60 | Ticker WakeSleepT; |
jurica238814 | 2:5504b714c9ae | 61 | Ticker turnBuzzOffT; |
jurica238814 | 1:5f34885f5cff | 62 | PwmOut buzzer(BUZZER); |
jurica238814 | 6:d14e3df498f4 | 63 | PwmOut gyro_power(p7); |
jurica238814 | 6:d14e3df498f4 | 64 | PwmOut i2c_power(p5); // I2C Pull-ups power pin |
jurica238814 | 6:d14e3df498f4 | 65 | InterruptIn gyro_pulse(INT2_PIN); |
jurica238814 | 6:d14e3df498f4 | 66 | Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS); |
jurica238814 | 1:5f34885f5cff | 67 | BLE &ble = BLE::Instance(); |
jurica238814 | 2:5504b714c9ae | 68 | |
jurica238814 | 10:fd91664032d8 | 69 | |
jurica238814 | 8:570eb66d50b5 | 70 | #if DEBUG |
jurica238814 | 10:fd91664032d8 | 71 | DigitalOut advLED(p22); |
jurica238814 | 10:fd91664032d8 | 72 | DigitalOut scanLED(p23); |
jurica238814 | 10:fd91664032d8 | 73 | DigitalOut awake(p24); |
jurica238814 | 10:fd91664032d8 | 74 | #endif |
jurica238814 | 10:fd91664032d8 | 75 | #if DEBUG_ACC |
jurica238814 | 8:570eb66d50b5 | 76 | DigitalOut int_led(p22); |
jurica238814 | 8:570eb66d50b5 | 77 | #endif |
jurica238814 | 8:570eb66d50b5 | 78 | |
jurica238814 | 0:f8c1e0b2d473 | 79 | /* Restart Advertising on disconnection*/ |
jurica238814 | 0:f8c1e0b2d473 | 80 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
jurica238814 | 10:fd91664032d8 | 81 | //BLE::Instance().gap().startAdvertising(); |
jurica238814 | 1:5f34885f5cff | 82 | } |
jurica238814 | 0:f8c1e0b2d473 | 83 | |
jurica238814 | 0:f8c1e0b2d473 | 84 | /** |
jurica238814 | 0:f8c1e0b2d473 | 85 | * This function is called when the ble initialization process has failed |
jurica238814 | 0:f8c1e0b2d473 | 86 | */ |
jurica238814 | 0:f8c1e0b2d473 | 87 | void onBleInitError(BLE &ble, ble_error_t error){ |
jurica238814 | 0:f8c1e0b2d473 | 88 | /* Avoid compiler warnings */ |
jurica238814 | 0:f8c1e0b2d473 | 89 | (void) ble; |
jurica238814 | 0:f8c1e0b2d473 | 90 | (void) error; |
jurica238814 | 0:f8c1e0b2d473 | 91 | /* Initialization error handling should go here */ |
jurica238814 | 0:f8c1e0b2d473 | 92 | } |
jurica238814 | 0:f8c1e0b2d473 | 93 | |
jurica238814 | 0:f8c1e0b2d473 | 94 | /** |
jurica238814 | 0:f8c1e0b2d473 | 95 | * Callback triggered when the ble initialization process has finished |
jurica238814 | 0:f8c1e0b2d473 | 96 | */ |
jurica238814 | 0:f8c1e0b2d473 | 97 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
jurica238814 | 0:f8c1e0b2d473 | 98 | BLE& ble = params->ble; |
jurica238814 | 0:f8c1e0b2d473 | 99 | ble_error_t error = params->error; |
jurica238814 | 0:f8c1e0b2d473 | 100 | |
jurica238814 | 0:f8c1e0b2d473 | 101 | if (error != BLE_ERROR_NONE) { |
jurica238814 | 0:f8c1e0b2d473 | 102 | /* In case of error, forward the error handling to onBleInitError */ |
jurica238814 | 0:f8c1e0b2d473 | 103 | onBleInitError(ble, error); |
jurica238814 | 0:f8c1e0b2d473 | 104 | return; |
jurica238814 | 0:f8c1e0b2d473 | 105 | } |
jurica238814 | 0:f8c1e0b2d473 | 106 | |
jurica238814 | 0:f8c1e0b2d473 | 107 | /* Ensure that it is the default instance of BLE */ |
jurica238814 | 0:f8c1e0b2d473 | 108 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
jurica238814 | 0:f8c1e0b2d473 | 109 | return; |
jurica238814 | 0:f8c1e0b2d473 | 110 | } |
jurica238814 | 0:f8c1e0b2d473 | 111 | |
jurica238814 | 0:f8c1e0b2d473 | 112 | ble.gap().onDisconnection(disconnectionCallback); |
jurica238814 | 0:f8c1e0b2d473 | 113 | |
jurica238814 | 1:5f34885f5cff | 114 | /* Get my MAC address */ |
jurica238814 | 1:5f34885f5cff | 115 | BLEProtocol::AddressType_t temp_address_type; |
jurica238814 | 1:5f34885f5cff | 116 | ble.gap().getAddress(&temp_address_type, my_mac_address); |
jurica238814 | 1:5f34885f5cff | 117 | |
jurica238814 | 1:5f34885f5cff | 118 | |
jurica238814 | 0:f8c1e0b2d473 | 119 | /* setup advertising */ |
jurica238814 | 10:fd91664032d8 | 120 | |
jurica238814 | 0:f8c1e0b2d473 | 121 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jurica238814 | 1:5f34885f5cff | 122 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE); |
jurica238814 | 0:f8c1e0b2d473 | 123 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
jurica238814 | 10:fd91664032d8 | 124 | ble.gap().setAdvertisingInterval(ADV_INTERVAL); // --> Has to be at least 100ms! |
jurica238814 | 10:fd91664032d8 | 125 | //ble.gap().startAdvertising(); |
jurica238814 | 0:f8c1e0b2d473 | 126 | } |
jurica238814 | 0:f8c1e0b2d473 | 127 | |
jurica238814 | 3:2a4ac5b87046 | 128 | |
jurica238814 | 3:2a4ac5b87046 | 129 | uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){ |
jurica238814 | 1:5f34885f5cff | 130 | uint8_t i=0; |
jurica238814 | 3:2a4ac5b87046 | 131 | uint8_t len; |
jurica238814 | 3:2a4ac5b87046 | 132 | |
jurica238814 | 3:2a4ac5b87046 | 133 | do{ |
jurica238814 | 3:2a4ac5b87046 | 134 | len = params->advertisingData[i]; |
jurica238814 | 3:2a4ac5b87046 | 135 | i++; |
jurica238814 | 3:2a4ac5b87046 | 136 | if(params->advertisingData[i] == MSD_ID) return i; |
jurica238814 | 3:2a4ac5b87046 | 137 | else i += (len-1); |
jurica238814 | 3:2a4ac5b87046 | 138 | }while(i<MAX_BLE_PACKET_SIZE); |
jurica238814 | 3:2a4ac5b87046 | 139 | |
jurica238814 | 3:2a4ac5b87046 | 140 | return 0; |
jurica238814 | 3:2a4ac5b87046 | 141 | } |
jurica238814 | 3:2a4ac5b87046 | 142 | |
jurica238814 | 3:2a4ac5b87046 | 143 | /** |
jurica238814 | 3:2a4ac5b87046 | 144 | * Function is called when BLE radio discovers any kind of advertisment |
jurica238814 | 3:2a4ac5b87046 | 145 | */ |
jurica238814 | 3:2a4ac5b87046 | 146 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){ |
jurica238814 | 3:2a4ac5b87046 | 147 | uint8_t i=0; |
jurica238814 | 3:2a4ac5b87046 | 148 | uint8_t msdOffset; |
jurica238814 | 3:2a4ac5b87046 | 149 | |
jurica238814 | 10:fd91664032d8 | 150 | ble.gap().stopScan(); |
jurica238814 | 3:2a4ac5b87046 | 151 | msdOffset = findMSDIndex(params); |
jurica238814 | 3:2a4ac5b87046 | 152 | if(msdOffset == 0){ |
jurica238814 | 3:2a4ac5b87046 | 153 | // There's no MSD in BLE advertisement data |
jurica238814 | 3:2a4ac5b87046 | 154 | return; |
jurica238814 | 3:2a4ac5b87046 | 155 | } |
jurica238814 | 3:2a4ac5b87046 | 156 | |
jurica238814 | 3:2a4ac5b87046 | 157 | if ((params->advertisingData[msdOffset]) == MSD_ID){ |
jurica238814 | 1:5f34885f5cff | 158 | // Follows Manufacturer Specific Data |
jurica238814 | 3:2a4ac5b87046 | 159 | if ((params->advertisingData[msdOffset+1]) == 0x59){ |
jurica238814 | 3:2a4ac5b87046 | 160 | if ((params->advertisingData[msdOffset+2]) == 0x00){ |
jurica238814 | 1:5f34885f5cff | 161 | for(i=0; i<6; i++){ |
jurica238814 | 11:92a9fffd5015 | 162 | if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i]){ |
jurica238814 | 1:5f34885f5cff | 163 | continue; |
jurica238814 | 1:5f34885f5cff | 164 | } |
jurica238814 | 1:5f34885f5cff | 165 | else{ |
jurica238814 | 10:fd91664032d8 | 166 | return; |
jurica238814 | 1:5f34885f5cff | 167 | } |
jurica238814 | 1:5f34885f5cff | 168 | } |
jurica238814 | 2:5504b714c9ae | 169 | turnBuzzOffT.detach(); |
jurica238814 | 2:5504b714c9ae | 170 | WakeSleepT.detach(); |
jurica238814 | 2:5504b714c9ae | 171 | buzzer.write(0.5F); |
jurica238814 | 2:5504b714c9ae | 172 | turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME); |
jurica238814 | 1:5f34885f5cff | 173 | } |
jurica238814 | 1:5f34885f5cff | 174 | } |
jurica238814 | 2:5504b714c9ae | 175 | } |
jurica238814 | 2:5504b714c9ae | 176 | } |
jurica238814 | 2:5504b714c9ae | 177 | |
jurica238814 | 10:fd91664032d8 | 178 | /** |
jurica238814 | 10:fd91664032d8 | 179 | * The function is called when ticker generates interrupt |
jurica238814 | 10:fd91664032d8 | 180 | */ |
jurica238814 | 10:fd91664032d8 | 181 | void turnBuzzOff(void){ |
jurica238814 | 10:fd91664032d8 | 182 | buzzer.write(0.0F); |
jurica238814 | 10:fd91664032d8 | 183 | turnBuzzOffT.detach(); |
jurica238814 | 10:fd91664032d8 | 184 | ble.gap().startScan(advertisementCallback); |
jurica238814 | 2:5504b714c9ae | 185 | WakeSleepT.attach(goToSleep, AWAKE_TIME); |
jurica238814 | 10:fd91664032d8 | 186 | } |
jurica238814 | 10:fd91664032d8 | 187 | |
jurica238814 | 10:fd91664032d8 | 188 | void startAdvertising(){ |
jurica238814 | 10:fd91664032d8 | 189 | ble.gap().startAdvertising(); |
jurica238814 | 10:fd91664032d8 | 190 | radioState = ADVERTISING; |
jurica238814 | 10:fd91664032d8 | 191 | #if DEBUG |
jurica238814 | 10:fd91664032d8 | 192 | advLED = 0; |
jurica238814 | 10:fd91664032d8 | 193 | scanLED = 1; |
jurica238814 | 10:fd91664032d8 | 194 | #endif |
jurica238814 | 10:fd91664032d8 | 195 | WakeSleepT.detach(); |
jurica238814 | 10:fd91664032d8 | 196 | WakeSleepT.attach(WakeMeUp, ADV_TIMER_TIME); // Call the wakeMeUp function |
jurica238814 | 10:fd91664032d8 | 197 | } |
jurica238814 | 10:fd91664032d8 | 198 | |
jurica238814 | 10:fd91664032d8 | 199 | void startScanning(){ |
jurica238814 | 10:fd91664032d8 | 200 | ble.gap().stopAdvertising(); |
jurica238814 | 10:fd91664032d8 | 201 | ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW); |
jurica238814 | 10:fd91664032d8 | 202 | ble.gap().setScanTimeout(SCAN_TIMER_TIME); |
jurica238814 | 2:5504b714c9ae | 203 | ble.gap().startScan(advertisementCallback); |
jurica238814 | 10:fd91664032d8 | 204 | radioState = SCANNING; |
jurica238814 | 10:fd91664032d8 | 205 | #if DEBUG |
jurica238814 | 10:fd91664032d8 | 206 | advLED = 1; |
jurica238814 | 10:fd91664032d8 | 207 | scanLED = 0; |
jurica238814 | 10:fd91664032d8 | 208 | #endif |
jurica238814 | 10:fd91664032d8 | 209 | WakeSleepT.detach(); |
jurica238814 | 10:fd91664032d8 | 210 | WakeSleepT.attach(WakeMeUp, SCAN_TIMER_TIME); |
jurica238814 | 10:fd91664032d8 | 211 | } |
jurica238814 | 10:fd91664032d8 | 212 | |
jurica238814 | 10:fd91664032d8 | 213 | void WakeMeUp(){ |
jurica238814 | 8:570eb66d50b5 | 214 | sleepFlag = 0; |
jurica238814 | 10:fd91664032d8 | 215 | switch(radioState){ |
jurica238814 | 10:fd91664032d8 | 216 | case OFF:{ |
jurica238814 | 10:fd91664032d8 | 217 | startAdvertising(); |
jurica238814 | 10:fd91664032d8 | 218 | break; |
jurica238814 | 10:fd91664032d8 | 219 | } |
jurica238814 | 10:fd91664032d8 | 220 | case ADVERTISING:{ |
jurica238814 | 10:fd91664032d8 | 221 | startScanning(); |
jurica238814 | 10:fd91664032d8 | 222 | break; |
jurica238814 | 10:fd91664032d8 | 223 | } |
jurica238814 | 10:fd91664032d8 | 224 | case SCANNING:{ |
jurica238814 | 10:fd91664032d8 | 225 | radioState = OFF; |
jurica238814 | 10:fd91664032d8 | 226 | ble.gap().stopAdvertising(); // Just in case |
jurica238814 | 10:fd91664032d8 | 227 | ble.gap().stopScan(); |
jurica238814 | 10:fd91664032d8 | 228 | WakeSleepT.detach(); |
jurica238814 | 10:fd91664032d8 | 229 | WakeSleepT.attach(goToSleep, FREE_TIME); |
jurica238814 | 10:fd91664032d8 | 230 | #if DEBUG |
jurica238814 | 10:fd91664032d8 | 231 | advLED = 1; |
jurica238814 | 10:fd91664032d8 | 232 | scanLED = 1; |
jurica238814 | 10:fd91664032d8 | 233 | #endif |
jurica238814 | 10:fd91664032d8 | 234 | break; |
jurica238814 | 10:fd91664032d8 | 235 | } |
jurica238814 | 10:fd91664032d8 | 236 | default: return; |
jurica238814 | 10:fd91664032d8 | 237 | } |
jurica238814 | 2:5504b714c9ae | 238 | } |
jurica238814 | 2:5504b714c9ae | 239 | |
jurica238814 | 2:5504b714c9ae | 240 | void goToSleep(){ |
jurica238814 | 2:5504b714c9ae | 241 | WakeSleepT.detach(); |
jurica238814 | 2:5504b714c9ae | 242 | WakeSleepT.attach(WakeMeUp, SLEEP_TIME); |
jurica238814 | 2:5504b714c9ae | 243 | ble.gap().stopAdvertising(); |
jurica238814 | 2:5504b714c9ae | 244 | ble.gap().stopScan(); |
jurica238814 | 8:570eb66d50b5 | 245 | sleepFlag = 1; |
jurica238814 | 1:5f34885f5cff | 246 | } |
jurica238814 | 1:5f34885f5cff | 247 | |
jurica238814 | 6:d14e3df498f4 | 248 | void buzz(void){ |
jurica238814 | 6:d14e3df498f4 | 249 | buzzer.write(0.5f); |
jurica238814 | 6:d14e3df498f4 | 250 | wait_ms(100); |
jurica238814 | 6:d14e3df498f4 | 251 | buzzer.write(0.0f); |
jurica238814 | 9:2ab2be19add9 | 252 | sleepFlag = 0; |
jurica238814 | 6:d14e3df498f4 | 253 | } |
jurica238814 | 6:d14e3df498f4 | 254 | |
jurica238814 | 6:d14e3df498f4 | 255 | void pulse_handler(void){ |
jurica238814 | 10:fd91664032d8 | 256 | #if DEBUG_ACC |
jurica238814 | 6:d14e3df498f4 | 257 | int_led = !int_led; |
jurica238814 | 6:d14e3df498f4 | 258 | #endif |
jurica238814 | 6:d14e3df498f4 | 259 | i2c_power.write(1.0F); |
jurica238814 | 6:d14e3df498f4 | 260 | buzzer_flag = 1; |
jurica238814 | 8:570eb66d50b5 | 261 | // Be awake some time |
jurica238814 | 9:2ab2be19add9 | 262 | //WakeSleepT.detach(); |
jurica238814 | 9:2ab2be19add9 | 263 | //WakeSleepT.attach(goToSleep, AWAKE_TIME); |
jurica238814 | 6:d14e3df498f4 | 264 | } |
jurica238814 | 6:d14e3df498f4 | 265 | |
jurica238814 | 10:fd91664032d8 | 266 | int main(void){ |
jurica238814 | 10:fd91664032d8 | 267 | #if DEBUG |
jurica238814 | 10:fd91664032d8 | 268 | awake = 1; |
jurica238814 | 10:fd91664032d8 | 269 | advLED = 1; |
jurica238814 | 10:fd91664032d8 | 270 | scanLED = 1; |
jurica238814 | 10:fd91664032d8 | 271 | #endif |
jurica238814 | 2:5504b714c9ae | 272 | WakeSleepT.attach(goToSleep, AWAKE_TIME); |
jurica238814 | 0:f8c1e0b2d473 | 273 | ble.init(bleInitComplete); |
jurica238814 | 0:f8c1e0b2d473 | 274 | ble.gap().setTxPower(txPower); |
jurica238814 | 0:f8c1e0b2d473 | 275 | GapAdvertisingData postavke = GapAdvertisingData(); |
jurica238814 | 0:f8c1e0b2d473 | 276 | |
jurica238814 | 10:fd91664032d8 | 277 | //ble.gap().setScanParams(SCAN_INTERVAL, SCAN_WINDOW); |
jurica238814 | 10:fd91664032d8 | 278 | //ble.gap().setScanTimeout(0.5); |
jurica238814 | 10:fd91664032d8 | 279 | //ble.gap().startScan(advertisementCallback); |
jurica238814 | 1:5f34885f5cff | 280 | |
jurica238814 | 1:5f34885f5cff | 281 | buzzer.period(0.001F); |
jurica238814 | 1:5f34885f5cff | 282 | buzzer.write(0.0F); |
jurica238814 | 6:d14e3df498f4 | 283 | gyro_power.period(0.01F); |
jurica238814 | 6:d14e3df498f4 | 284 | gyro_power.write(1.0F); |
jurica238814 | 6:d14e3df498f4 | 285 | i2c_power.period(0.01F); |
jurica238814 | 6:d14e3df498f4 | 286 | i2c_power.write(1.0F); |
jurica238814 | 6:d14e3df498f4 | 287 | wait_ms(1000); |
jurica238814 | 6:d14e3df498f4 | 288 | |
jurica238814 | 6:d14e3df498f4 | 289 | /* Pulse interrupt detection */ |
jurica238814 | 6:d14e3df498f4 | 290 | acc.set_register((char)CTRL_REG_4, (char) 0x04); // INT_EN_FF_MT Freefall/motion interrupt enabled |
jurica238814 | 6:d14e3df498f4 | 291 | wait_ms(1); |
jurica238814 | 6:d14e3df498f4 | 292 | acc.set_register((char)FF_MT_CFG, (char) 0b01011000); //ELE, Motion Flag ON, YEFE, X Event Flag Enable |
jurica238814 | 6:d14e3df498f4 | 293 | wait_ms(1); |
jurica238814 | 6:d14e3df498f4 | 294 | acc.set_register((char)CTRL_REG_5, (char) 0x00); // INT_EN_FF_MT interrupt is router t0 INT2 |
jurica238814 | 6:d14e3df498f4 | 295 | wait_ms(1); |
jurica238814 | 6:d14e3df498f4 | 296 | acc.set_register((char)FF_COUNT, (char) 0x08); // Set Counter degister value (10ms) |
jurica238814 | 6:d14e3df498f4 | 297 | wait_ms(1); |
jurica238814 | 6:d14e3df498f4 | 298 | 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 | 299 | wait_ms(1); |
jurica238814 | 6:d14e3df498f4 | 300 | |
jurica238814 | 6:d14e3df498f4 | 301 | /* Setup for the interrupt handler */ |
jurica238814 | 9:2ab2be19add9 | 302 | //gyro_pulse.rise(&pulse_handler); // ------------------------------------- |
jurica238814 | 6:d14e3df498f4 | 303 | acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode |
jurica238814 | 6:d14e3df498f4 | 304 | wait(1); |
jurica238814 | 0:f8c1e0b2d473 | 305 | |
jurica238814 | 2:5504b714c9ae | 306 | __enable_irq(); |
jurica238814 | 2:5504b714c9ae | 307 | |
jurica238814 | 0:f8c1e0b2d473 | 308 | /* SpinWait for initialization to complete. This is necessary because the |
jurica238814 | 0:f8c1e0b2d473 | 309 | * BLE object is used in the main loop below. */ |
jurica238814 | 3:2a4ac5b87046 | 310 | while (ble.hasInitialized() == false){ |
jurica238814 | 3:2a4ac5b87046 | 311 | /* spin loop */ |
jurica238814 | 3:2a4ac5b87046 | 312 | } |
jurica238814 | 9:2ab2be19add9 | 313 | |
jurica238814 | 1:5f34885f5cff | 314 | while(true){ |
jurica238814 | 9:2ab2be19add9 | 315 | if(sleepFlag){ |
jurica238814 | 9:2ab2be19add9 | 316 | if(!awake) awake = 1; |
jurica238814 | 9:2ab2be19add9 | 317 | __WFI(); |
jurica238814 | 9:2ab2be19add9 | 318 | } |
jurica238814 | 9:2ab2be19add9 | 319 | else{ |
jurica238814 | 9:2ab2be19add9 | 320 | if(awake) awake = 0; |
jurica238814 | 9:2ab2be19add9 | 321 | ble.waitForEvent(); |
jurica238814 | 9:2ab2be19add9 | 322 | } |
jurica238814 | 0:f8c1e0b2d473 | 323 | } |
jurica238814 | 0:f8c1e0b2d473 | 324 | } |