
y
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
main.cpp@0:7bbabab69671, 2017-12-13 (annotated)
- Committer:
- klebermagno
- Date:
- Wed Dec 13 16:31:36 2017 +0000
- Revision:
- 0:7bbabab69671
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klebermagno | 0:7bbabab69671 | 1 | /* mbed Microcontroller Library |
klebermagno | 0:7bbabab69671 | 2 | * Copyright (c) 2006-2013 ARM Limited |
klebermagno | 0:7bbabab69671 | 3 | * |
klebermagno | 0:7bbabab69671 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
klebermagno | 0:7bbabab69671 | 5 | * you may not use this file except in compliance with the License. |
klebermagno | 0:7bbabab69671 | 6 | * You may obtain a copy of the License at |
klebermagno | 0:7bbabab69671 | 7 | * |
klebermagno | 0:7bbabab69671 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
klebermagno | 0:7bbabab69671 | 9 | * |
klebermagno | 0:7bbabab69671 | 10 | * Unless required by applicable law or agreed to in writing, software |
klebermagno | 0:7bbabab69671 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
klebermagno | 0:7bbabab69671 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
klebermagno | 0:7bbabab69671 | 13 | * See the License for the specific language governing permissions and |
klebermagno | 0:7bbabab69671 | 14 | * limitations under the License. |
klebermagno | 0:7bbabab69671 | 15 | */ |
klebermagno | 0:7bbabab69671 | 16 | |
klebermagno | 0:7bbabab69671 | 17 | #include "mbed.h" |
klebermagno | 0:7bbabab69671 | 18 | #include "ble/BLE.h" |
klebermagno | 0:7bbabab69671 | 19 | #include "ble/services/HealthThermometerService.h" |
klebermagno | 0:7bbabab69671 | 20 | |
klebermagno | 0:7bbabab69671 | 21 | DigitalOut led1(LED1); |
klebermagno | 0:7bbabab69671 | 22 | |
klebermagno | 0:7bbabab69671 | 23 | static HealthThermometerService *thermometerServicePtr; |
klebermagno | 0:7bbabab69671 | 24 | |
klebermagno | 0:7bbabab69671 | 25 | static const char DEVICE_NAME[] = "Therm"; |
klebermagno | 0:7bbabab69671 | 26 | static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE}; |
klebermagno | 0:7bbabab69671 | 27 | static volatile bool triggerSensorPolling = false; |
klebermagno | 0:7bbabab69671 | 28 | static float currentTemperature = 39.6; |
klebermagno | 0:7bbabab69671 | 29 | |
klebermagno | 0:7bbabab69671 | 30 | /* Restart Advertising on disconnection*/ |
klebermagno | 0:7bbabab69671 | 31 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
klebermagno | 0:7bbabab69671 | 32 | { |
klebermagno | 0:7bbabab69671 | 33 | BLE::Instance().gap().startAdvertising(); |
klebermagno | 0:7bbabab69671 | 34 | } |
klebermagno | 0:7bbabab69671 | 35 | |
klebermagno | 0:7bbabab69671 | 36 | void periodicCallback(void) |
klebermagno | 0:7bbabab69671 | 37 | { |
klebermagno | 0:7bbabab69671 | 38 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
klebermagno | 0:7bbabab69671 | 39 | |
klebermagno | 0:7bbabab69671 | 40 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
klebermagno | 0:7bbabab69671 | 41 | * heavy-weight sensor polling from the main thread. */ |
klebermagno | 0:7bbabab69671 | 42 | triggerSensorPolling = true; |
klebermagno | 0:7bbabab69671 | 43 | } |
klebermagno | 0:7bbabab69671 | 44 | |
klebermagno | 0:7bbabab69671 | 45 | /** |
klebermagno | 0:7bbabab69671 | 46 | * This function is called when the ble initialization process has failed |
klebermagno | 0:7bbabab69671 | 47 | */ |
klebermagno | 0:7bbabab69671 | 48 | void onBleInitError(BLE &ble, ble_error_t error) |
klebermagno | 0:7bbabab69671 | 49 | { |
klebermagno | 0:7bbabab69671 | 50 | /* Avoid compiler warnings */ |
klebermagno | 0:7bbabab69671 | 51 | (void) ble; |
klebermagno | 0:7bbabab69671 | 52 | (void) error; |
klebermagno | 0:7bbabab69671 | 53 | /* Initialization error handling should go here */ |
klebermagno | 0:7bbabab69671 | 54 | } |
klebermagno | 0:7bbabab69671 | 55 | |
klebermagno | 0:7bbabab69671 | 56 | /** |
klebermagno | 0:7bbabab69671 | 57 | * Callback triggered when the ble initialization process has finished |
klebermagno | 0:7bbabab69671 | 58 | */ |
klebermagno | 0:7bbabab69671 | 59 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
klebermagno | 0:7bbabab69671 | 60 | { |
klebermagno | 0:7bbabab69671 | 61 | BLE& ble = params->ble; |
klebermagno | 0:7bbabab69671 | 62 | ble_error_t error = params->error; |
klebermagno | 0:7bbabab69671 | 63 | |
klebermagno | 0:7bbabab69671 | 64 | if (error != BLE_ERROR_NONE) { |
klebermagno | 0:7bbabab69671 | 65 | /* In case of error, forward the error handling to onBleInitError */ |
klebermagno | 0:7bbabab69671 | 66 | onBleInitError(ble, error); |
klebermagno | 0:7bbabab69671 | 67 | return; |
klebermagno | 0:7bbabab69671 | 68 | } |
klebermagno | 0:7bbabab69671 | 69 | |
klebermagno | 0:7bbabab69671 | 70 | /* Ensure that it is the default instance of BLE */ |
klebermagno | 0:7bbabab69671 | 71 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
klebermagno | 0:7bbabab69671 | 72 | return; |
klebermagno | 0:7bbabab69671 | 73 | } |
klebermagno | 0:7bbabab69671 | 74 | |
klebermagno | 0:7bbabab69671 | 75 | ble.gap().onDisconnection(disconnectionCallback); |
klebermagno | 0:7bbabab69671 | 76 | |
klebermagno | 0:7bbabab69671 | 77 | /* Setup primary service. */ |
klebermagno | 0:7bbabab69671 | 78 | thermometerServicePtr = new HealthThermometerService(ble, currentTemperature, HealthThermometerService::LOCATION_EAR); |
klebermagno | 0:7bbabab69671 | 79 | |
klebermagno | 0:7bbabab69671 | 80 | /* setup advertising */ |
klebermagno | 0:7bbabab69671 | 81 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
klebermagno | 0:7bbabab69671 | 82 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
klebermagno | 0:7bbabab69671 | 83 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR); |
klebermagno | 0:7bbabab69671 | 84 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
klebermagno | 0:7bbabab69671 | 85 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
klebermagno | 0:7bbabab69671 | 86 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
klebermagno | 0:7bbabab69671 | 87 | ble.gap().startAdvertising(); |
klebermagno | 0:7bbabab69671 | 88 | } |
klebermagno | 0:7bbabab69671 | 89 | |
klebermagno | 0:7bbabab69671 | 90 | int main(void) |
klebermagno | 0:7bbabab69671 | 91 | { |
klebermagno | 0:7bbabab69671 | 92 | led1 = 1; |
klebermagno | 0:7bbabab69671 | 93 | Ticker ticker; |
klebermagno | 0:7bbabab69671 | 94 | ticker.attach(periodicCallback, 1); |
klebermagno | 0:7bbabab69671 | 95 | |
klebermagno | 0:7bbabab69671 | 96 | BLE &ble = BLE::Instance(); |
klebermagno | 0:7bbabab69671 | 97 | ble.init(bleInitComplete); |
klebermagno | 0:7bbabab69671 | 98 | |
klebermagno | 0:7bbabab69671 | 99 | /* SpinWait for initialization to complete. This is necessary because the |
klebermagno | 0:7bbabab69671 | 100 | * BLE object is used in the main loop below. */ |
klebermagno | 0:7bbabab69671 | 101 | while (ble.hasInitialized() == false) { /* spin loop */ } |
klebermagno | 0:7bbabab69671 | 102 | |
klebermagno | 0:7bbabab69671 | 103 | while (true) { |
klebermagno | 0:7bbabab69671 | 104 | if (triggerSensorPolling && ble.gap().getState().connected) { |
klebermagno | 0:7bbabab69671 | 105 | triggerSensorPolling = false; |
klebermagno | 0:7bbabab69671 | 106 | |
klebermagno | 0:7bbabab69671 | 107 | /* In our case, we simply update the dummy temperature measurement. */ |
klebermagno | 0:7bbabab69671 | 108 | currentTemperature += 0.1; |
klebermagno | 0:7bbabab69671 | 109 | thermometerServicePtr->updateTemperature(currentTemperature); |
klebermagno | 0:7bbabab69671 | 110 | } else { |
klebermagno | 0:7bbabab69671 | 111 | ble.waitForEvent(); |
klebermagno | 0:7bbabab69671 | 112 | } |
klebermagno | 0:7bbabab69671 | 113 | } |
klebermagno | 0:7bbabab69671 | 114 | } |