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