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