|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2013 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <mbed_events.h> |
| 18 | +#include <rtos.h> |
| 19 | +#include "mbed.h" |
| 20 | +#include "ble/BLE.h" |
| 21 | +#include "ble/services/HealthThermometerService.h" |
| 22 | + |
| 23 | +DigitalOut led1(LED1, 1); |
| 24 | + |
| 25 | +const static char DEVICE_NAME[] = "PIOTherm"; |
| 26 | +static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE}; |
| 27 | + |
| 28 | +static float currentTemperature = 39.6; |
| 29 | +static HealthThermometerService *thermometerServicePtr; |
| 30 | + |
| 31 | +static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
| 32 | + |
| 33 | +/* Restart Advertising on disconnection*/ |
| 34 | +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
| 35 | +{ |
| 36 | + BLE::Instance().gap().startAdvertising(); |
| 37 | +} |
| 38 | + |
| 39 | +void updateSensorValue(void) { |
| 40 | + /* Do blocking calls or whatever is necessary for sensor polling. |
| 41 | + In our case, we simply update the Temperature measurement. */ |
| 42 | + currentTemperature = (currentTemperature + 0.1 > 43.0) ? 39.6 : currentTemperature + 0.1; |
| 43 | + thermometerServicePtr->updateTemperature(currentTemperature); |
| 44 | +} |
| 45 | + |
| 46 | +void periodicCallback(void) |
| 47 | +{ |
| 48 | + led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
| 49 | + |
| 50 | + if (BLE::Instance().gap().getState().connected) { |
| 51 | + eventQueue.call(updateSensorValue); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void onBleInitError(BLE &ble, ble_error_t error) |
| 56 | +{ |
| 57 | + /* Initialization error handling should go here */ |
| 58 | +} |
| 59 | + |
| 60 | +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
| 61 | +{ |
| 62 | + BLE& ble = params->ble; |
| 63 | + ble_error_t error = params->error; |
| 64 | + |
| 65 | + if (error != BLE_ERROR_NONE) { |
| 66 | + onBleInitError(ble, error); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + ble.gap().onDisconnection(disconnectionCallback); |
| 75 | + |
| 76 | + /* Setup primary service. */ |
| 77 | + thermometerServicePtr = new HealthThermometerService(ble, currentTemperature, HealthThermometerService::LOCATION_EAR); |
| 78 | + |
| 79 | + /* setup advertising */ |
| 80 | + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
| 81 | + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
| 82 | + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR); |
| 83 | + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
| 84 | + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| 85 | + ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
| 86 | + ble.gap().startAdvertising(); |
| 87 | +} |
| 88 | + |
| 89 | +void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
| 90 | + BLE &ble = BLE::Instance(); |
| 91 | + eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
| 92 | +} |
| 93 | + |
| 94 | +int main() |
| 95 | +{ |
| 96 | + eventQueue.call_every(500, periodicCallback); |
| 97 | + |
| 98 | + BLE &ble = BLE::Instance(); |
| 99 | + ble.onEventsToProcess(scheduleBleEventsProcessing); |
| 100 | + ble.init(bleInitComplete); |
| 101 | + |
| 102 | + eventQueue.dispatch_forever(); |
| 103 | + |
| 104 | + return 0; |
| 105 | +} |
0 commit comments