Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
mbed-os-example-ble-master/BLE_Thermometer/source/main.cpp@0:599bc564478b, 2018-06-29 (annotated)
- Committer:
- bpd227
- Date:
- Fri Jun 29 01:19:08 2018 +0000
- Revision:
- 0:599bc564478b
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bpd227 | 0:599bc564478b | 1 | /* mbed Microcontroller Library |
bpd227 | 0:599bc564478b | 2 | * Copyright (c) 2006-2013 ARM Limited |
bpd227 | 0:599bc564478b | 3 | * |
bpd227 | 0:599bc564478b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bpd227 | 0:599bc564478b | 5 | * you may not use this file except in compliance with the License. |
bpd227 | 0:599bc564478b | 6 | * You may obtain a copy of the License at |
bpd227 | 0:599bc564478b | 7 | * |
bpd227 | 0:599bc564478b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bpd227 | 0:599bc564478b | 9 | * |
bpd227 | 0:599bc564478b | 10 | * Unless required by applicable law or agreed to in writing, software |
bpd227 | 0:599bc564478b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bpd227 | 0:599bc564478b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bpd227 | 0:599bc564478b | 13 | * See the License for the specific language governing permissions and |
bpd227 | 0:599bc564478b | 14 | * limitations under the License. |
bpd227 | 0:599bc564478b | 15 | */ |
bpd227 | 0:599bc564478b | 16 | |
bpd227 | 0:599bc564478b | 17 | #include <events/mbed_events.h> |
bpd227 | 0:599bc564478b | 18 | #include "mbed.h" |
bpd227 | 0:599bc564478b | 19 | #include "ble/BLE.h" |
bpd227 | 0:599bc564478b | 20 | #include "ble/services/HealthThermometerService.h" |
bpd227 | 0:599bc564478b | 21 | |
bpd227 | 0:599bc564478b | 22 | DigitalOut led1(LED1, 1); |
bpd227 | 0:599bc564478b | 23 | |
bpd227 | 0:599bc564478b | 24 | const static char DEVICE_NAME[] = "Therm"; |
bpd227 | 0:599bc564478b | 25 | static const uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE}; |
bpd227 | 0:599bc564478b | 26 | |
bpd227 | 0:599bc564478b | 27 | static float currentTemperature = 39.6; |
bpd227 | 0:599bc564478b | 28 | static HealthThermometerService *thermometerServicePtr; |
bpd227 | 0:599bc564478b | 29 | |
bpd227 | 0:599bc564478b | 30 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
bpd227 | 0:599bc564478b | 31 | |
bpd227 | 0:599bc564478b | 32 | /* Restart Advertising on disconnection*/ |
bpd227 | 0:599bc564478b | 33 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
bpd227 | 0:599bc564478b | 34 | { |
bpd227 | 0:599bc564478b | 35 | BLE::Instance().gap().startAdvertising(); |
bpd227 | 0:599bc564478b | 36 | } |
bpd227 | 0:599bc564478b | 37 | |
bpd227 | 0:599bc564478b | 38 | void updateSensorValue(void) { |
bpd227 | 0:599bc564478b | 39 | /* Do blocking calls or whatever is necessary for sensor polling. |
bpd227 | 0:599bc564478b | 40 | In our case, we simply update the Temperature measurement. */ |
bpd227 | 0:599bc564478b | 41 | currentTemperature = (currentTemperature + 0.1 > 43.0) ? 39.6 : currentTemperature + 0.1; |
bpd227 | 0:599bc564478b | 42 | thermometerServicePtr->updateTemperature(currentTemperature); |
bpd227 | 0:599bc564478b | 43 | } |
bpd227 | 0:599bc564478b | 44 | |
bpd227 | 0:599bc564478b | 45 | void periodicCallback(void) |
bpd227 | 0:599bc564478b | 46 | { |
bpd227 | 0:599bc564478b | 47 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
bpd227 | 0:599bc564478b | 48 | |
bpd227 | 0:599bc564478b | 49 | if (BLE::Instance().gap().getState().connected) { |
bpd227 | 0:599bc564478b | 50 | eventQueue.call(updateSensorValue); |
bpd227 | 0:599bc564478b | 51 | } |
bpd227 | 0:599bc564478b | 52 | } |
bpd227 | 0:599bc564478b | 53 | |
bpd227 | 0:599bc564478b | 54 | void onBleInitError(BLE &ble, ble_error_t error) |
bpd227 | 0:599bc564478b | 55 | { |
bpd227 | 0:599bc564478b | 56 | /* Initialization error handling should go here */ |
bpd227 | 0:599bc564478b | 57 | } |
bpd227 | 0:599bc564478b | 58 | |
bpd227 | 0:599bc564478b | 59 | void printMacAddress() |
bpd227 | 0:599bc564478b | 60 | { |
bpd227 | 0:599bc564478b | 61 | /* Print out device MAC address to the console*/ |
bpd227 | 0:599bc564478b | 62 | Gap::AddressType_t addr_type; |
bpd227 | 0:599bc564478b | 63 | Gap::Address_t address; |
bpd227 | 0:599bc564478b | 64 | BLE::Instance().gap().getAddress(&addr_type, address); |
bpd227 | 0:599bc564478b | 65 | printf("DEVICE MAC ADDRESS: "); |
bpd227 | 0:599bc564478b | 66 | for (int i = 5; i >= 1; i--){ |
bpd227 | 0:599bc564478b | 67 | printf("%02x:", address[i]); |
bpd227 | 0:599bc564478b | 68 | } |
bpd227 | 0:599bc564478b | 69 | printf("%02x\r\n", address[0]); |
bpd227 | 0:599bc564478b | 70 | } |
bpd227 | 0:599bc564478b | 71 | |
bpd227 | 0:599bc564478b | 72 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
bpd227 | 0:599bc564478b | 73 | { |
bpd227 | 0:599bc564478b | 74 | BLE& ble = params->ble; |
bpd227 | 0:599bc564478b | 75 | ble_error_t error = params->error; |
bpd227 | 0:599bc564478b | 76 | |
bpd227 | 0:599bc564478b | 77 | if (error != BLE_ERROR_NONE) { |
bpd227 | 0:599bc564478b | 78 | onBleInitError(ble, error); |
bpd227 | 0:599bc564478b | 79 | return; |
bpd227 | 0:599bc564478b | 80 | } |
bpd227 | 0:599bc564478b | 81 | |
bpd227 | 0:599bc564478b | 82 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
bpd227 | 0:599bc564478b | 83 | return; |
bpd227 | 0:599bc564478b | 84 | } |
bpd227 | 0:599bc564478b | 85 | |
bpd227 | 0:599bc564478b | 86 | ble.gap().onDisconnection(disconnectionCallback); |
bpd227 | 0:599bc564478b | 87 | |
bpd227 | 0:599bc564478b | 88 | /* Setup primary service. */ |
bpd227 | 0:599bc564478b | 89 | thermometerServicePtr = new HealthThermometerService(ble, currentTemperature, HealthThermometerService::LOCATION_EAR); |
bpd227 | 0:599bc564478b | 90 | |
bpd227 | 0:599bc564478b | 91 | /* setup advertising */ |
bpd227 | 0:599bc564478b | 92 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
bpd227 | 0:599bc564478b | 93 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
bpd227 | 0:599bc564478b | 94 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR); |
bpd227 | 0:599bc564478b | 95 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
bpd227 | 0:599bc564478b | 96 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
bpd227 | 0:599bc564478b | 97 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
bpd227 | 0:599bc564478b | 98 | ble.gap().startAdvertising(); |
bpd227 | 0:599bc564478b | 99 | |
bpd227 | 0:599bc564478b | 100 | printMacAddress(); |
bpd227 | 0:599bc564478b | 101 | } |
bpd227 | 0:599bc564478b | 102 | |
bpd227 | 0:599bc564478b | 103 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
bpd227 | 0:599bc564478b | 104 | BLE &ble = BLE::Instance(); |
bpd227 | 0:599bc564478b | 105 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
bpd227 | 0:599bc564478b | 106 | } |
bpd227 | 0:599bc564478b | 107 | |
bpd227 | 0:599bc564478b | 108 | int main() |
bpd227 | 0:599bc564478b | 109 | { |
bpd227 | 0:599bc564478b | 110 | eventQueue.call_every(500, periodicCallback); |
bpd227 | 0:599bc564478b | 111 | |
bpd227 | 0:599bc564478b | 112 | BLE &ble = BLE::Instance(); |
bpd227 | 0:599bc564478b | 113 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
bpd227 | 0:599bc564478b | 114 | ble.init(bleInitComplete); |
bpd227 | 0:599bc564478b | 115 | |
bpd227 | 0:599bc564478b | 116 | eventQueue.dispatch_forever(); |
bpd227 | 0:599bc564478b | 117 | |
bpd227 | 0:599bc564478b | 118 | return 0; |
bpd227 | 0:599bc564478b | 119 | } |