Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
main.cpp@2:5504b714c9ae, 2017-07-16 (annotated)
- Committer:
- jurica238814
- Date:
- Sun Jul 16 15:10:18 2017 +0000
- Revision:
- 2:5504b714c9ae
- Parent:
- 1:5f34885f5cff
- Child:
- 3:2a4ac5b87046
Tickers added. Works fine. Smarter parser todo.
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 | 1:5f34885f5cff | 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 | 1:5f34885f5cff | 13 | |
jurica238814 | 2:5504b714c9ae | 14 | #define SLEEP_TIME (0.85) // Sleep time in seconds |
jurica238814 | 2:5504b714c9ae | 15 | #define AWAKE_TIME (0.15) |
jurica238814 | 1:5f34885f5cff | 16 | #define BUZZER (p31) |
jurica238814 | 0:f8c1e0b2d473 | 17 | |
jurica238814 | 1:5f34885f5cff | 18 | /* Static constants for the BLE example */ |
jurica238814 | 2:5504b714c9ae | 19 | #define MSD_SIZE (18) |
jurica238814 | 2:5504b714c9ae | 20 | #define MSD_ID (0xFF) |
jurica238814 | 2:5504b714c9ae | 21 | #define MSD_OFFSET (4) |
jurica238814 | 2:5504b714c9ae | 22 | #define BUZZ_TIME (0.4) // Buzz time in s |
jurica238814 | 1:5f34885f5cff | 23 | |
jurica238814 | 0:f8c1e0b2d473 | 24 | |
jurica238814 | 0:f8c1e0b2d473 | 25 | bool SLEEP = true; |
jurica238814 | 0:f8c1e0b2d473 | 26 | int8_t txPower = 4; |
jurica238814 | 1:5f34885f5cff | 27 | uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00}; |
jurica238814 | 1:5f34885f5cff | 28 | uint8_t my_mac_address[6] = {}; |
jurica238814 | 0:f8c1e0b2d473 | 29 | |
jurica238814 | 2:5504b714c9ae | 30 | void turnBuzzOff(void); |
jurica238814 | 2:5504b714c9ae | 31 | void goToSleep(); |
jurica238814 | 0:f8c1e0b2d473 | 32 | |
jurica238814 | 2:5504b714c9ae | 33 | Ticker WakeSleepT; |
jurica238814 | 2:5504b714c9ae | 34 | Ticker turnBuzzOffT; |
jurica238814 | 1:5f34885f5cff | 35 | PwmOut buzzer(BUZZER); |
jurica238814 | 1:5f34885f5cff | 36 | BLE &ble = BLE::Instance(); |
jurica238814 | 2:5504b714c9ae | 37 | |
jurica238814 | 0:f8c1e0b2d473 | 38 | /* Restart Advertising on disconnection*/ |
jurica238814 | 0:f8c1e0b2d473 | 39 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
jurica238814 | 0:f8c1e0b2d473 | 40 | BLE::Instance().gap().startAdvertising(); |
jurica238814 | 0:f8c1e0b2d473 | 41 | } |
jurica238814 | 0:f8c1e0b2d473 | 42 | |
jurica238814 | 2:5504b714c9ae | 43 | void turnBuzzOff(void){ |
jurica238814 | 2:5504b714c9ae | 44 | buzzer.write(0.0F); |
jurica238814 | 2:5504b714c9ae | 45 | turnBuzzOffT.detach(); |
jurica238814 | 2:5504b714c9ae | 46 | WakeSleepT.attach(goToSleep, AWAKE_TIME); |
jurica238814 | 1:5f34885f5cff | 47 | } |
jurica238814 | 0:f8c1e0b2d473 | 48 | |
jurica238814 | 0:f8c1e0b2d473 | 49 | /** |
jurica238814 | 0:f8c1e0b2d473 | 50 | * This function is called when the ble initialization process has failed |
jurica238814 | 0:f8c1e0b2d473 | 51 | */ |
jurica238814 | 0:f8c1e0b2d473 | 52 | void onBleInitError(BLE &ble, ble_error_t error){ |
jurica238814 | 0:f8c1e0b2d473 | 53 | /* Avoid compiler warnings */ |
jurica238814 | 0:f8c1e0b2d473 | 54 | (void) ble; |
jurica238814 | 0:f8c1e0b2d473 | 55 | (void) error; |
jurica238814 | 0:f8c1e0b2d473 | 56 | /* Initialization error handling should go here */ |
jurica238814 | 0:f8c1e0b2d473 | 57 | } |
jurica238814 | 0:f8c1e0b2d473 | 58 | |
jurica238814 | 0:f8c1e0b2d473 | 59 | /** |
jurica238814 | 0:f8c1e0b2d473 | 60 | * Callback triggered when the ble initialization process has finished |
jurica238814 | 0:f8c1e0b2d473 | 61 | */ |
jurica238814 | 0:f8c1e0b2d473 | 62 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
jurica238814 | 0:f8c1e0b2d473 | 63 | BLE& ble = params->ble; |
jurica238814 | 0:f8c1e0b2d473 | 64 | ble_error_t error = params->error; |
jurica238814 | 0:f8c1e0b2d473 | 65 | |
jurica238814 | 0:f8c1e0b2d473 | 66 | if (error != BLE_ERROR_NONE) { |
jurica238814 | 0:f8c1e0b2d473 | 67 | /* In case of error, forward the error handling to onBleInitError */ |
jurica238814 | 0:f8c1e0b2d473 | 68 | onBleInitError(ble, error); |
jurica238814 | 0:f8c1e0b2d473 | 69 | return; |
jurica238814 | 0:f8c1e0b2d473 | 70 | } |
jurica238814 | 0:f8c1e0b2d473 | 71 | |
jurica238814 | 0:f8c1e0b2d473 | 72 | /* Ensure that it is the default instance of BLE */ |
jurica238814 | 0:f8c1e0b2d473 | 73 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
jurica238814 | 0:f8c1e0b2d473 | 74 | return; |
jurica238814 | 0:f8c1e0b2d473 | 75 | } |
jurica238814 | 0:f8c1e0b2d473 | 76 | |
jurica238814 | 0:f8c1e0b2d473 | 77 | ble.gap().onDisconnection(disconnectionCallback); |
jurica238814 | 0:f8c1e0b2d473 | 78 | |
jurica238814 | 1:5f34885f5cff | 79 | /* Get my MAC address */ |
jurica238814 | 1:5f34885f5cff | 80 | BLEProtocol::AddressType_t temp_address_type; |
jurica238814 | 1:5f34885f5cff | 81 | ble.gap().getAddress(&temp_address_type, my_mac_address); |
jurica238814 | 1:5f34885f5cff | 82 | |
jurica238814 | 1:5f34885f5cff | 83 | |
jurica238814 | 0:f8c1e0b2d473 | 84 | /* setup advertising */ |
jurica238814 | 0:f8c1e0b2d473 | 85 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jurica238814 | 1:5f34885f5cff | 86 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE); |
jurica238814 | 0:f8c1e0b2d473 | 87 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
jurica238814 | 2:5504b714c9ae | 88 | ble.gap().setAdvertisingInterval(100); // --> Has to be at least 100ms! |
jurica238814 | 2:5504b714c9ae | 89 | ble.gap().startAdvertising(); |
jurica238814 | 0:f8c1e0b2d473 | 90 | } |
jurica238814 | 0:f8c1e0b2d473 | 91 | |
jurica238814 | 1:5f34885f5cff | 92 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){ |
jurica238814 | 1:5f34885f5cff | 93 | uint8_t i=0; |
jurica238814 | 2:5504b714c9ae | 94 | if ((params->advertisingData[MSD_OFFSET]) == MSD_ID ){ |
jurica238814 | 1:5f34885f5cff | 95 | // Follows Manufacturer Specific Data |
jurica238814 | 1:5f34885f5cff | 96 | if ((params->advertisingData[MSD_OFFSET+1]) == 0x59){ |
jurica238814 | 1:5f34885f5cff | 97 | if ((params->advertisingData[MSD_OFFSET+2]) == 0x00){ |
jurica238814 | 1:5f34885f5cff | 98 | for(i=0; i<6; i++){ |
jurica238814 | 1:5f34885f5cff | 99 | if((params->advertisingData[MSD_OFFSET+i+3]) == my_mac_address[5-i]){ |
jurica238814 | 1:5f34885f5cff | 100 | continue; |
jurica238814 | 1:5f34885f5cff | 101 | } |
jurica238814 | 1:5f34885f5cff | 102 | else{ |
jurica238814 | 1:5f34885f5cff | 103 | return; |
jurica238814 | 1:5f34885f5cff | 104 | } |
jurica238814 | 1:5f34885f5cff | 105 | } |
jurica238814 | 2:5504b714c9ae | 106 | turnBuzzOffT.detach(); |
jurica238814 | 2:5504b714c9ae | 107 | WakeSleepT.detach(); |
jurica238814 | 2:5504b714c9ae | 108 | buzzer.write(0.5F); |
jurica238814 | 2:5504b714c9ae | 109 | turnBuzzOffT.attach(turnBuzzOff, BUZZ_TIME); |
jurica238814 | 1:5f34885f5cff | 110 | } |
jurica238814 | 1:5f34885f5cff | 111 | } |
jurica238814 | 2:5504b714c9ae | 112 | } |
jurica238814 | 2:5504b714c9ae | 113 | } |
jurica238814 | 2:5504b714c9ae | 114 | |
jurica238814 | 2:5504b714c9ae | 115 | void WakeMeUp(){ |
jurica238814 | 2:5504b714c9ae | 116 | WakeSleepT.detach(); |
jurica238814 | 2:5504b714c9ae | 117 | WakeSleepT.attach(goToSleep, AWAKE_TIME); |
jurica238814 | 2:5504b714c9ae | 118 | |
jurica238814 | 2:5504b714c9ae | 119 | ble.gap().startScan(advertisementCallback); |
jurica238814 | 2:5504b714c9ae | 120 | ble.gap().startAdvertising(); |
jurica238814 | 2:5504b714c9ae | 121 | SLEEP = false; |
jurica238814 | 2:5504b714c9ae | 122 | } |
jurica238814 | 2:5504b714c9ae | 123 | |
jurica238814 | 2:5504b714c9ae | 124 | void goToSleep(){ |
jurica238814 | 2:5504b714c9ae | 125 | WakeSleepT.detach(); |
jurica238814 | 2:5504b714c9ae | 126 | WakeSleepT.attach(WakeMeUp, SLEEP_TIME); |
jurica238814 | 2:5504b714c9ae | 127 | |
jurica238814 | 2:5504b714c9ae | 128 | ble.gap().stopAdvertising(); |
jurica238814 | 2:5504b714c9ae | 129 | ble.gap().stopScan(); |
jurica238814 | 2:5504b714c9ae | 130 | SLEEP = true; |
jurica238814 | 1:5f34885f5cff | 131 | } |
jurica238814 | 1:5f34885f5cff | 132 | |
jurica238814 | 0:f8c1e0b2d473 | 133 | int main(void){ |
jurica238814 | 2:5504b714c9ae | 134 | WakeSleepT.attach(goToSleep, AWAKE_TIME); |
jurica238814 | 0:f8c1e0b2d473 | 135 | ble.init(bleInitComplete); |
jurica238814 | 0:f8c1e0b2d473 | 136 | ble.gap().setTxPower(txPower); |
jurica238814 | 0:f8c1e0b2d473 | 137 | GapAdvertisingData postavke = GapAdvertisingData(); |
jurica238814 | 0:f8c1e0b2d473 | 138 | |
jurica238814 | 2:5504b714c9ae | 139 | ble.gap().setScanParams(100, 100); |
jurica238814 | 1:5f34885f5cff | 140 | ble.gap().startScan(advertisementCallback); |
jurica238814 | 1:5f34885f5cff | 141 | |
jurica238814 | 1:5f34885f5cff | 142 | buzzer.period(0.001F); |
jurica238814 | 1:5f34885f5cff | 143 | buzzer.write(0.0F); |
jurica238814 | 0:f8c1e0b2d473 | 144 | |
jurica238814 | 2:5504b714c9ae | 145 | __enable_irq(); |
jurica238814 | 2:5504b714c9ae | 146 | |
jurica238814 | 0:f8c1e0b2d473 | 147 | /* SpinWait for initialization to complete. This is necessary because the |
jurica238814 | 0:f8c1e0b2d473 | 148 | * BLE object is used in the main loop below. */ |
jurica238814 | 0:f8c1e0b2d473 | 149 | while (ble.hasInitialized() == false) { /* spin loop */ } |
jurica238814 | 1:5f34885f5cff | 150 | |
jurica238814 | 1:5f34885f5cff | 151 | while(true){ |
jurica238814 | 2:5504b714c9ae | 152 | if(SLEEP){ |
jurica238814 | 2:5504b714c9ae | 153 | __WFI(); |
jurica238814 | 2:5504b714c9ae | 154 | } |
jurica238814 | 0:f8c1e0b2d473 | 155 | else{ |
jurica238814 | 1:5f34885f5cff | 156 | ble.waitForEvent(); |
jurica238814 | 0:f8c1e0b2d473 | 157 | } |
jurica238814 | 0:f8c1e0b2d473 | 158 | } |
jurica238814 | 2:5504b714c9ae | 159 | |
jurica238814 | 0:f8c1e0b2d473 | 160 | } |