Geo beacon for VF.
Dependencies: MMA8452 aconno_bsp adc52832_common
main.cpp@0:f8c1e0b2d473, 2017-03-06 (annotated)
- Committer:
- jurica238814
- Date:
- Mon Mar 06 09:09:55 2017 +0000
- Revision:
- 0:f8c1e0b2d473
- Child:
- 1:5f34885f5cff
GeoBeacon is working. Tx power is set to 4dB, wake up time is 1s, and sleep time is 5s.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 0:f8c1e0b2d473 | 1 | /* mbed Microcontroller Library |
jurica238814 | 0:f8c1e0b2d473 | 2 | * Copyright (c) 2006-2013 ARM Limited |
jurica238814 | 0:f8c1e0b2d473 | 3 | * |
jurica238814 | 0:f8c1e0b2d473 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jurica238814 | 0:f8c1e0b2d473 | 5 | * you may not use this file except in compliance with the License. |
jurica238814 | 0:f8c1e0b2d473 | 6 | * You may obtain a copy of the License at |
jurica238814 | 0:f8c1e0b2d473 | 7 | * |
jurica238814 | 0:f8c1e0b2d473 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jurica238814 | 0:f8c1e0b2d473 | 9 | * |
jurica238814 | 0:f8c1e0b2d473 | 10 | * Unless required by applicable law or agreed to in writing, software |
jurica238814 | 0:f8c1e0b2d473 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jurica238814 | 0:f8c1e0b2d473 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jurica238814 | 0:f8c1e0b2d473 | 13 | * See the License for the specific language governing permissions and |
jurica238814 | 0:f8c1e0b2d473 | 14 | * limitations under the License. |
jurica238814 | 0:f8c1e0b2d473 | 15 | */ |
jurica238814 | 0:f8c1e0b2d473 | 16 | |
jurica238814 | 0:f8c1e0b2d473 | 17 | #include "mbed.h" |
jurica238814 | 0:f8c1e0b2d473 | 18 | #include "ble/BLE.h" |
jurica238814 | 0:f8c1e0b2d473 | 19 | #include "ble/services/HealthThermometerService.h" |
jurica238814 | 0:f8c1e0b2d473 | 20 | #include "acd52832_bsp.h" |
jurica238814 | 0:f8c1e0b2d473 | 21 | #include "GapAdvertisingData.h" |
jurica238814 | 0:f8c1e0b2d473 | 22 | |
jurica238814 | 0:f8c1e0b2d473 | 23 | #define SLEEP_TIME 5 // Sleep time in seconds |
jurica238814 | 0:f8c1e0b2d473 | 24 | #define WAKE_UP_TIME 1000 // Sleep time in ms |
jurica238814 | 0:f8c1e0b2d473 | 25 | |
jurica238814 | 0:f8c1e0b2d473 | 26 | bool SLEEP = true; |
jurica238814 | 0:f8c1e0b2d473 | 27 | int8_t txPower = 4; |
jurica238814 | 0:f8c1e0b2d473 | 28 | |
jurica238814 | 0:f8c1e0b2d473 | 29 | static HealthThermometerService *thermometerServicePtr; |
jurica238814 | 0:f8c1e0b2d473 | 30 | |
jurica238814 | 0:f8c1e0b2d473 | 31 | uint8_t MSD[18] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0xE8}; |
jurica238814 | 0:f8c1e0b2d473 | 32 | |
jurica238814 | 0:f8c1e0b2d473 | 33 | /* Restart Advertising on disconnection*/ |
jurica238814 | 0:f8c1e0b2d473 | 34 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ |
jurica238814 | 0:f8c1e0b2d473 | 35 | BLE::Instance().gap().startAdvertising(); |
jurica238814 | 0:f8c1e0b2d473 | 36 | } |
jurica238814 | 0:f8c1e0b2d473 | 37 | |
jurica238814 | 0:f8c1e0b2d473 | 38 | void wakeMeUp(void){ |
jurica238814 | 0:f8c1e0b2d473 | 39 | SLEEP = false; |
jurica238814 | 0:f8c1e0b2d473 | 40 | } |
jurica238814 | 0:f8c1e0b2d473 | 41 | |
jurica238814 | 0:f8c1e0b2d473 | 42 | /** |
jurica238814 | 0:f8c1e0b2d473 | 43 | * This function is called when the ble initialization process has failed |
jurica238814 | 0:f8c1e0b2d473 | 44 | */ |
jurica238814 | 0:f8c1e0b2d473 | 45 | void onBleInitError(BLE &ble, ble_error_t error){ |
jurica238814 | 0:f8c1e0b2d473 | 46 | /* Avoid compiler warnings */ |
jurica238814 | 0:f8c1e0b2d473 | 47 | (void) ble; |
jurica238814 | 0:f8c1e0b2d473 | 48 | (void) error; |
jurica238814 | 0:f8c1e0b2d473 | 49 | /* Initialization error handling should go here */ |
jurica238814 | 0:f8c1e0b2d473 | 50 | } |
jurica238814 | 0:f8c1e0b2d473 | 51 | |
jurica238814 | 0:f8c1e0b2d473 | 52 | /** |
jurica238814 | 0:f8c1e0b2d473 | 53 | * Callback triggered when the ble initialization process has finished |
jurica238814 | 0:f8c1e0b2d473 | 54 | */ |
jurica238814 | 0:f8c1e0b2d473 | 55 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
jurica238814 | 0:f8c1e0b2d473 | 56 | BLE& ble = params->ble; |
jurica238814 | 0:f8c1e0b2d473 | 57 | ble_error_t error = params->error; |
jurica238814 | 0:f8c1e0b2d473 | 58 | |
jurica238814 | 0:f8c1e0b2d473 | 59 | if (error != BLE_ERROR_NONE) { |
jurica238814 | 0:f8c1e0b2d473 | 60 | /* In case of error, forward the error handling to onBleInitError */ |
jurica238814 | 0:f8c1e0b2d473 | 61 | onBleInitError(ble, error); |
jurica238814 | 0:f8c1e0b2d473 | 62 | return; |
jurica238814 | 0:f8c1e0b2d473 | 63 | } |
jurica238814 | 0:f8c1e0b2d473 | 64 | |
jurica238814 | 0:f8c1e0b2d473 | 65 | /* Ensure that it is the default instance of BLE */ |
jurica238814 | 0:f8c1e0b2d473 | 66 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
jurica238814 | 0:f8c1e0b2d473 | 67 | return; |
jurica238814 | 0:f8c1e0b2d473 | 68 | } |
jurica238814 | 0:f8c1e0b2d473 | 69 | |
jurica238814 | 0:f8c1e0b2d473 | 70 | ble.gap().onDisconnection(disconnectionCallback); |
jurica238814 | 0:f8c1e0b2d473 | 71 | |
jurica238814 | 0:f8c1e0b2d473 | 72 | /* Setup primary service. */ |
jurica238814 | 0:f8c1e0b2d473 | 73 | thermometerServicePtr = new HealthThermometerService(ble, currentTemperature, HealthThermometerService::LOCATION_EAR); |
jurica238814 | 0:f8c1e0b2d473 | 74 | |
jurica238814 | 0:f8c1e0b2d473 | 75 | /* setup advertising */ |
jurica238814 | 0:f8c1e0b2d473 | 76 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
jurica238814 | 0:f8c1e0b2d473 | 77 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, 18); |
jurica238814 | 0:f8c1e0b2d473 | 78 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
jurica238814 | 0:f8c1e0b2d473 | 79 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
jurica238814 | 0:f8c1e0b2d473 | 80 | ble.gap().startAdvertising(); |
jurica238814 | 0:f8c1e0b2d473 | 81 | } |
jurica238814 | 0:f8c1e0b2d473 | 82 | |
jurica238814 | 0:f8c1e0b2d473 | 83 | int main(void){ |
jurica238814 | 0:f8c1e0b2d473 | 84 | Ticker ticker; |
jurica238814 | 0:f8c1e0b2d473 | 85 | ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up |
jurica238814 | 0:f8c1e0b2d473 | 86 | |
jurica238814 | 0:f8c1e0b2d473 | 87 | BLE &ble = BLE::Instance(); |
jurica238814 | 0:f8c1e0b2d473 | 88 | ble.init(bleInitComplete); |
jurica238814 | 0:f8c1e0b2d473 | 89 | ble.gap().setTxPower(txPower); |
jurica238814 | 0:f8c1e0b2d473 | 90 | GapAdvertisingData postavke = GapAdvertisingData(); |
jurica238814 | 0:f8c1e0b2d473 | 91 | |
jurica238814 | 0:f8c1e0b2d473 | 92 | |
jurica238814 | 0:f8c1e0b2d473 | 93 | /* SpinWait for initialization to complete. This is necessary because the |
jurica238814 | 0:f8c1e0b2d473 | 94 | * BLE object is used in the main loop below. */ |
jurica238814 | 0:f8c1e0b2d473 | 95 | while (ble.hasInitialized() == false) { /* spin loop */ } |
jurica238814 | 0:f8c1e0b2d473 | 96 | |
jurica238814 | 0:f8c1e0b2d473 | 97 | while (true){ |
jurica238814 | 0:f8c1e0b2d473 | 98 | if (SLEEP){ |
jurica238814 | 0:f8c1e0b2d473 | 99 | ble.gap().stopAdvertising(); |
jurica238814 | 0:f8c1e0b2d473 | 100 | sleep(); |
jurica238814 | 0:f8c1e0b2d473 | 101 | } |
jurica238814 | 0:f8c1e0b2d473 | 102 | else{ |
jurica238814 | 0:f8c1e0b2d473 | 103 | // I'm awake |
jurica238814 | 0:f8c1e0b2d473 | 104 | ble.gap().startAdvertising(); |
jurica238814 | 0:f8c1e0b2d473 | 105 | wait_ms(WAKE_UP_TIME); |
jurica238814 | 0:f8c1e0b2d473 | 106 | SLEEP = true; |
jurica238814 | 0:f8c1e0b2d473 | 107 | } |
jurica238814 | 0:f8c1e0b2d473 | 108 | } |
jurica238814 | 0:f8c1e0b2d473 | 109 | } |