Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
main.cpp@6:d14e3df498f4, 2017-07-19 (annotated)
- Committer:
- jurica238814
- Date:
- Wed Jul 19 12:07:23 2017 +0000
- Revision:
- 6:d14e3df498f4
- Parent:
- 5:0f42dcae4cdf
- Child:
- 8:570eb66d50b5
Dummy commit.
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 | 3:2a4ac5b87046 | 15 | #define SLEEP_TIME (0.85) // Sleep time in seconds WAS 0.85 |
| jurica238814 | 3:2a4ac5b87046 | 16 | #define AWAKE_TIME (0.15) // Was 0.15 |
| jurica238814 | 1:5f34885f5cff | 17 | #define BUZZER (p31) |
| jurica238814 | 0:f8c1e0b2d473 | 18 | |
| jurica238814 | 1:5f34885f5cff | 19 | /* Static constants for the BLE example */ |
| jurica238814 | 3:2a4ac5b87046 | 20 | #define MAX_BLE_PACKET_SIZE (31) |
| jurica238814 | 3:2a4ac5b87046 | 21 | #define MSD_SIZE (18) |
| jurica238814 | 3:2a4ac5b87046 | 22 | #define MSD_ID (0xFF) |
| jurica238814 | 3:2a4ac5b87046 | 23 | #define BUZZ_TIME (1.0) // Buzz time in s |
| jurica238814 | 1:5f34885f5cff | 24 | |
| jurica238814 | 6:d14e3df498f4 | 25 | /* Static constants for the accelerometer */ |
| jurica238814 | 6:d14e3df498f4 | 26 | #define WHO_AM_I 0x0D // Type 'read' : This should return the device id of 0x2A |
| jurica238814 | 6:d14e3df498f4 | 27 | #define OUT_Z_MSB 0x05 // Type 'read' : z axis - 8 most significatn bit of a 12 bit sample |
| jurica238814 | 6:d14e3df498f4 | 28 | #define I2C_DATA (p29) |
| jurica238814 | 6:d14e3df498f4 | 29 | #define I2C_CLK (p2) |
| jurica238814 | 6:d14e3df498f4 | 30 | #define INT2_PIN (p4) |
| jurica238814 | 0:f8c1e0b2d473 | 31 | |
| jurica238814 | 6:d14e3df498f4 | 32 | |
| jurica238814 | 6:d14e3df498f4 | 33 | //uint8_t SLEEP = true; |
| jurica238814 | 0:f8c1e0b2d473 | 34 | int8_t txPower = 4; |
| jurica238814 | 1:5f34885f5cff | 35 | 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 | 36 | uint8_t my_mac_address[6] = {}; |
| jurica238814 | 6:d14e3df498f4 | 37 | uint8_t buzzer_flag = 0; |
| jurica238814 | 0:f8c1e0b2d473 | 38 | |
| jurica238814 | 2:5504b714c9ae | 39 | void turnBuzzOff(void); |
| jurica238814 | 2:5504b714c9ae | 40 | void goToSleep(); |
| jurica238814 | 0:f8c1e0b2d473 | 41 | |
| jurica238814 | 2:5504b714c9ae | 42 | Ticker WakeSleepT; |
| jurica238814 | 2:5504b714c9ae | 43 | Ticker turnBuzzOffT; |
| jurica238814 | 1:5f34885f5cff | 44 | PwmOut buzzer(BUZZER); |
| jurica238814 | 6:d14e3df498f4 | 45 | PwmOut gyro_power(p7); |
| jurica238814 | 6:d14e3df498f4 | 46 | PwmOut i2c_power(p5); // I2C Pull-ups power pin |
| jurica238814 | 6:d14e3df498f4 | 47 | InterruptIn gyro_pulse(INT2_PIN); |
| jurica238814 | 6:d14e3df498f4 | 48 | Acc_MMA8452 acc(I2C_DATA, I2C_CLK, MMA8452_ADDRESS); |
| jurica238814 | 1:5f34885f5cff | 49 | BLE &ble = BLE::Instance(); |
| jurica238814 | 2:5504b714c9ae | 50 | |
| jurica238814 | 0:f8c1e0b2d473 | 51 | /* Restart Advertising on disconnection*/ |
| jurica238814 | 0:f8c1e0b2d473 | 52 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
| jurica238814 | 0:f8c1e0b2d473 | 53 | BLE::Instance().gap().startAdvertising(); |
| jurica238814 | 0:f8c1e0b2d473 | 54 | } |
| jurica238814 | 0:f8c1e0b2d473 | 55 | |
| jurica238814 | 3:2a4ac5b87046 | 56 | /** |
| jurica238814 | 3:2a4ac5b87046 | 57 | * The function is called when ticker generates interrupt |
| jurica238814 | 3:2a4ac5b87046 | 58 | */ |
| jurica238814 | 2:5504b714c9ae | 59 | void turnBuzzOff(void){ |
| jurica238814 | 2:5504b714c9ae | 60 | buzzer.write(0.0F); |
| jurica238814 | 2:5504b714c9ae | 61 | turnBuzzOffT.detach(); |
| jurica238814 | 2:5504b714c9ae | 62 | WakeSleepT.attach(goToSleep, AWAKE_TIME); |
| jurica238814 | 1:5f34885f5cff | 63 | } |
| jurica238814 | 0:f8c1e0b2d473 | 64 | |
| jurica238814 | 0:f8c1e0b2d473 | 65 | /** |
| jurica238814 | 0:f8c1e0b2d473 | 66 | * This function is called when the ble initialization process has failed |
| jurica238814 | 0:f8c1e0b2d473 | 67 | */ |
| jurica238814 | 0:f8c1e0b2d473 | 68 | void onBleInitError(BLE &ble, ble_error_t error){ |
| jurica238814 | 0:f8c1e0b2d473 | 69 | /* Avoid compiler warnings */ |
| jurica238814 | 0:f8c1e0b2d473 | 70 | (void) ble; |
| jurica238814 | 0:f8c1e0b2d473 | 71 | (void) error; |
| jurica238814 | 0:f8c1e0b2d473 | 72 | /* Initialization error handling should go here */ |
| jurica238814 | 0:f8c1e0b2d473 | 73 | } |
| jurica238814 | 0:f8c1e0b2d473 | 74 | |
| jurica238814 | 0:f8c1e0b2d473 | 75 | /** |
| jurica238814 | 0:f8c1e0b2d473 | 76 | * Callback triggered when the ble initialization process has finished |
| jurica238814 | 0:f8c1e0b2d473 | 77 | */ |
| jurica238814 | 0:f8c1e0b2d473 | 78 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
| jurica238814 | 0:f8c1e0b2d473 | 79 | BLE& ble = params->ble; |
| jurica238814 | 0:f8c1e0b2d473 | 80 | ble_error_t error = params->error; |
| jurica238814 | 0:f8c1e0b2d473 | 81 | |
| jurica238814 | 0:f8c1e0b2d473 | 82 | if (error != BLE_ERROR_NONE) { |
| jurica238814 | 0:f8c1e0b2d473 | 83 | /* In case of error, forward the error handling to onBleInitError */ |
| jurica238814 | 0:f8c1e0b2d473 | 84 | onBleInitError(ble, error); |
| jurica238814 | 0:f8c1e0b2d473 | 85 | return; |
| jurica238814 | 0:f8c1e0b2d473 | 86 | } |
| jurica238814 | 0:f8c1e0b2d473 | 87 | |
| jurica238814 | 0:f8c1e0b2d473 | 88 | /* Ensure that it is the default instance of BLE */ |
| jurica238814 | 0:f8c1e0b2d473 | 89 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
| jurica238814 | 0:f8c1e0b2d473 | 90 | return; |
| jurica238814 | 0:f8c1e0b2d473 | 91 | } |
| jurica238814 | 0:f8c1e0b2d473 | 92 | |
| jurica238814 | 0:f8c1e0b2d473 | 93 | ble.gap().onDisconnection(disconnectionCallback); |
| jurica238814 | 0:f8c1e0b2d473 | 94 | |
| jurica238814 | 1:5f34885f5cff | 95 | /* Get my MAC address */ |
| jurica238814 | 1:5f34885f5cff | 96 | BLEProtocol::AddressType_t temp_address_type; |
| jurica238814 | 1:5f34885f5cff | 97 | ble.gap().getAddress(&temp_address_type, my_mac_address); |
| jurica238814 | 1:5f34885f5cff | 98 | |
| jurica238814 | 1:5f34885f5cff | 99 | |
| jurica238814 | 0:f8c1e0b2d473 | 100 | /* setup advertising */ |
| jurica238814 | 0:f8c1e0b2d473 | 101 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
| jurica238814 | 1:5f34885f5cff | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE); |
| jurica238814 | 0:f8c1e0b2d473 | 103 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
| jurica238814 | 2:5504b714c9ae | 104 | ble.gap().setAdvertisingInterval(100); // --> Has to be at least 100ms! |
| jurica238814 | 2:5504b714c9ae | 105 | ble.gap().startAdvertising(); |
| jurica238814 | 0:f8c1e0b2d473 | 106 | } |
| jurica238814 | 0:f8c1e0b2d473 | 107 | |
| jurica238814 | 3:2a4ac5b87046 | 108 | |
| jurica238814 | 3:2a4ac5b87046 | 109 | uint8_t findMSDIndex(const Gap::AdvertisementCallbackParams_t *params){ |
| jurica238814 | 1:5f34885f5cff | 110 | uint8_t i=0; |
| jurica238814 | 3:2a4ac5b87046 | 111 | uint8_t len; |
| jurica238814 | 3:2a4ac5b87046 | 112 | |
| jurica238814 | 3:2a4ac5b87046 | 113 | do{ |
| jurica238814 | 3:2a4ac5b87046 | 114 | len = params->advertisingData[i]; |
| jurica238814 | 3:2a4ac5b87046 | 115 | i++; |
| jurica238814 | 3:2a4ac5b87046 | 116 | if(params->advertisingData[i] == MSD_ID) return i; |
| jurica238814 | 3:2a4ac5b87046 | 117 | else i += (len-1); |
| jurica238814 | 3:2a4ac5b87046 | 118 | }while(i<MAX_BLE_PACKET_SIZE); |
| jurica238814 | 3:2a4ac5b87046 | 119 | |
| jurica238814 | 3:2a4ac5b87046 | 120 | return 0; |
| jurica238814 | 3:2a4ac5b87046 | 121 | } |
| jurica238814 | 3:2a4ac5b87046 | 122 | |
| jurica238814 | 3:2a4ac5b87046 | 123 | /** |
| jurica238814 | 3:2a4ac5b87046 | 124 | * Function is called when BLE radio discovers any kind of advertisment |
| jurica238814 | 3:2a4ac5b87046 | 125 | */ |
| jurica238814 | 3:2a4ac5b87046 | 126 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){ |
| jurica238814 | 3:2a4ac5b87046 | 127 | uint8_t i=0; |
| jurica238814 | 3:2a4ac5b87046 | 128 | uint8_t msdOffset; |
| jurica238814 | 3:2a4ac5b87046 | 129 | |
| jurica238814 | 3:2a4ac5b87046 | 130 | msdOffset = findMSDIndex(params); |
| jurica238814 | 3:2a4ac5b87046 | 131 | if(msdOffset == 0){ |
| jurica238814 | 3:2a4ac5b87046 | 132 | // There's no MSD in BLE advertisement data |
| jurica238814 | 3:2a4ac5b87046 | 133 | return; |
| jurica238814 | 3:2a4ac5b87046 | 134 | } |
| jurica238814 | 3:2a4ac5b87046 | 135 | |
| jurica238814 | 3:2a4ac5b87046 | 136 | if ((params->advertisingData[msdOffset]) == MSD_ID){ |
| jurica238814 | 1:5f34885f5cff | 137 | // Follows Manufacturer Specific Data |
| jurica238814 | 3:2a4ac5b87046 | 138 | if ((params->advertisingData[msdOffset+1]) == 0x59){ |
| jurica238814 | 3:2a4ac5b87046 | 139 | if ((params->advertisingData[msdOffset+2]) == 0x00){ |
| jurica238814 | 1:5f34885f5cff | 140 | for(i=0; i<6; i++){ |
| jurica238814 | 3:2a4ac5b87046 | 141 | if((params->advertisingData[msdOffset+i+3]) == my_mac_address[5-i]){ |
| jurica238814 | 1:5f34885f5cff | 142 | continue; |
| jurica238814 | 1:5f34885f5cff | 143 | } |
| jurica238814 | 1:5f34885f5cff | 144 | else{ |
| jurica238814 | 1:5f34885f5cff | 145 | return; |
| jurica238814 | 1:5f34885f5cff | 146 | } |
| jurica238814 | 1:5f34885f5cff | 147 | } |
| jurica238814 | 2:5504b714c9ae | 148 | turnBuzzOffT.detach(); |
| jurica238814 | 2:5504b714c9ae | 149 | WakeSleepT.detach(); |
| jurica238814 | 2:5504b714c9ae | 150 | buzzer.write(0.5F); |
| jurica238814 | 2:5504b714c9ae | 151 | turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME); |
| jurica238814 | 1:5f34885f5cff | 152 | } |
| jurica238814 | 1:5f34885f5cff | 153 | } |
| jurica238814 | 2:5504b714c9ae | 154 | } |
| jurica238814 | 2:5504b714c9ae | 155 | } |
| jurica238814 | 2:5504b714c9ae | 156 | |
| jurica238814 | 2:5504b714c9ae | 157 | void WakeMeUp(){ |
| jurica238814 | 2:5504b714c9ae | 158 | WakeSleepT.detach(); |
| jurica238814 | 2:5504b714c9ae | 159 | WakeSleepT.attach(goToSleep, AWAKE_TIME); |
| jurica238814 | 2:5504b714c9ae | 160 | |
| jurica238814 | 2:5504b714c9ae | 161 | ble.gap().startScan(advertisementCallback); |
| jurica238814 | 2:5504b714c9ae | 162 | ble.gap().startAdvertising(); |
| jurica238814 | 2:5504b714c9ae | 163 | SLEEP = false; |
| jurica238814 | 2:5504b714c9ae | 164 | } |
| jurica238814 | 2:5504b714c9ae | 165 | |
| jurica238814 | 2:5504b714c9ae | 166 | void goToSleep(){ |
| jurica238814 | 2:5504b714c9ae | 167 | WakeSleepT.detach(); |
| jurica238814 | 2:5504b714c9ae | 168 | WakeSleepT.attach(WakeMeUp, SLEEP_TIME); |
| jurica238814 | 2:5504b714c9ae | 169 | |
| jurica238814 | 2:5504b714c9ae | 170 | ble.gap().stopAdvertising(); |
| jurica238814 | 2:5504b714c9ae | 171 | ble.gap().stopScan(); |
| jurica238814 | 2:5504b714c9ae | 172 | SLEEP = true; |
| jurica238814 | 1:5f34885f5cff | 173 | } |
| jurica238814 | 1:5f34885f5cff | 174 | |
| jurica238814 | 6:d14e3df498f4 | 175 | void buzz(void){ |
| jurica238814 | 6:d14e3df498f4 | 176 | buzzer.write(0.5f); |
| jurica238814 | 6:d14e3df498f4 | 177 | wait_ms(100); |
| jurica238814 | 6:d14e3df498f4 | 178 | buzzer.write(0.0f); |
| jurica238814 | 6:d14e3df498f4 | 179 | buzzer_flag = 0; |
| jurica238814 | 6:d14e3df498f4 | 180 | } |
| jurica238814 | 6:d14e3df498f4 | 181 | |
| jurica238814 | 6:d14e3df498f4 | 182 | void pulse_handler(void){ |
| jurica238814 | 6:d14e3df498f4 | 183 | #if DEBUG |
| jurica238814 | 6:d14e3df498f4 | 184 | int_led = !int_led; |
| jurica238814 | 6:d14e3df498f4 | 185 | #endif |
| jurica238814 | 6:d14e3df498f4 | 186 | i2c_power.write(1.0F); |
| jurica238814 | 6:d14e3df498f4 | 187 | buzzer_flag = 1; |
| jurica238814 | 6:d14e3df498f4 | 188 | } |
| jurica238814 | 6:d14e3df498f4 | 189 | |
| jurica238814 | 0:f8c1e0b2d473 | 190 | int main(void){ |
| jurica238814 | 2:5504b714c9ae | 191 | WakeSleepT.attach(goToSleep, AWAKE_TIME); |
| jurica238814 | 0:f8c1e0b2d473 | 192 | ble.init(bleInitComplete); |
| jurica238814 | 0:f8c1e0b2d473 | 193 | ble.gap().setTxPower(txPower); |
| jurica238814 | 0:f8c1e0b2d473 | 194 | GapAdvertisingData postavke = GapAdvertisingData(); |
| jurica238814 | 0:f8c1e0b2d473 | 195 | |
| jurica238814 | 2:5504b714c9ae | 196 | ble.gap().setScanParams(100, 100); |
| jurica238814 | 1:5f34885f5cff | 197 | ble.gap().startScan(advertisementCallback); |
| jurica238814 | 1:5f34885f5cff | 198 | |
| jurica238814 | 1:5f34885f5cff | 199 | buzzer.period(0.001F); |
| jurica238814 | 1:5f34885f5cff | 200 | buzzer.write(0.0F); |
| jurica238814 | 6:d14e3df498f4 | 201 | gyro_power.period(0.01F); |
| jurica238814 | 6:d14e3df498f4 | 202 | gyro_power.write(1.0F); |
| jurica238814 | 6:d14e3df498f4 | 203 | i2c_power.period(0.01F); |
| jurica238814 | 6:d14e3df498f4 | 204 | i2c_power.write(1.0F); |
| jurica238814 | 6:d14e3df498f4 | 205 | wait_ms(1000); |
| jurica238814 | 6:d14e3df498f4 | 206 | |
| jurica238814 | 6:d14e3df498f4 | 207 | /* Pulse interrupt detection */ |
| jurica238814 | 6:d14e3df498f4 | 208 | acc.set_register((char)CTRL_REG_4, (char) 0x04); // INT_EN_FF_MT Freefall/motion interrupt enabled |
| jurica238814 | 6:d14e3df498f4 | 209 | wait_ms(1); |
| jurica238814 | 6:d14e3df498f4 | 210 | acc.set_register((char)FF_MT_CFG, (char) 0b01011000); //ELE, Motion Flag ON, YEFE, X Event Flag Enable |
| jurica238814 | 6:d14e3df498f4 | 211 | wait_ms(1); |
| jurica238814 | 6:d14e3df498f4 | 212 | acc.set_register((char)CTRL_REG_5, (char) 0x00); // INT_EN_FF_MT interrupt is router t0 INT2 |
| jurica238814 | 6:d14e3df498f4 | 213 | wait_ms(1); |
| jurica238814 | 6:d14e3df498f4 | 214 | acc.set_register((char)FF_COUNT, (char) 0x08); // Set Counter degister value (10ms) |
| jurica238814 | 6:d14e3df498f4 | 215 | wait_ms(1); |
| jurica238814 | 6:d14e3df498f4 | 216 | 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 | 217 | wait_ms(1); |
| jurica238814 | 6:d14e3df498f4 | 218 | |
| jurica238814 | 6:d14e3df498f4 | 219 | /* Setup for the interrupt handler */ |
| jurica238814 | 6:d14e3df498f4 | 220 | gyro_pulse.rise(&pulse_handler); |
| jurica238814 | 6:d14e3df498f4 | 221 | acc.set_register((char)CTRL_REG_1, (char) 0x01); // Flow data rate and Active mode |
| jurica238814 | 6:d14e3df498f4 | 222 | wait(1); |
| jurica238814 | 0:f8c1e0b2d473 | 223 | |
| jurica238814 | 2:5504b714c9ae | 224 | __enable_irq(); |
| jurica238814 | 2:5504b714c9ae | 225 | |
| jurica238814 | 0:f8c1e0b2d473 | 226 | /* SpinWait for initialization to complete. This is necessary because the |
| jurica238814 | 0:f8c1e0b2d473 | 227 | * BLE object is used in the main loop below. */ |
| jurica238814 | 3:2a4ac5b87046 | 228 | while (ble.hasInitialized() == false){ |
| jurica238814 | 3:2a4ac5b87046 | 229 | /* spin loop */ |
| jurica238814 | 3:2a4ac5b87046 | 230 | } |
| jurica238814 | 1:5f34885f5cff | 231 | |
| jurica238814 | 1:5f34885f5cff | 232 | while(true){ |
| jurica238814 | 3:2a4ac5b87046 | 233 | if(SLEEP) __WFI(); |
| jurica238814 | 6:d14e3df498f4 | 234 | else if(buzzer_flag) buzz(); |
| jurica238814 | 3:2a4ac5b87046 | 235 | else ble.waitForEvent(); |
| jurica238814 | 0:f8c1e0b2d473 | 236 | } |
| jurica238814 | 0:f8c1e0b2d473 | 237 | } |

