+ battery and device information
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@22:519a64a68971, 2016-10-12 (annotated)
- Committer:
- a29
- Date:
- Wed Oct 12 10:41:44 2016 +0000
- Revision:
- 22:519a64a68971
- Parent:
- 21:0e7c08f5386f
+ battery and device information service
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:eb7f02ad28a7 | 1 | /* mbed Microcontroller Library |
screamer | 0:eb7f02ad28a7 | 2 | * Copyright (c) 2006-2015 ARM Limited |
screamer | 0:eb7f02ad28a7 | 3 | * |
screamer | 0:eb7f02ad28a7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:eb7f02ad28a7 | 5 | * you may not use this file except in compliance with the License. |
screamer | 0:eb7f02ad28a7 | 6 | * You may obtain a copy of the License at |
screamer | 0:eb7f02ad28a7 | 7 | * |
screamer | 0:eb7f02ad28a7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:eb7f02ad28a7 | 9 | * |
screamer | 0:eb7f02ad28a7 | 10 | * Unless required by applicable law or agreed to in writing, software |
screamer | 0:eb7f02ad28a7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:eb7f02ad28a7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:eb7f02ad28a7 | 13 | * See the License for the specific language governing permissions and |
screamer | 0:eb7f02ad28a7 | 14 | * limitations under the License. |
screamer | 0:eb7f02ad28a7 | 15 | */ |
screamer | 0:eb7f02ad28a7 | 16 | |
screamer | 0:eb7f02ad28a7 | 17 | #include "mbed.h" |
screamer | 0:eb7f02ad28a7 | 18 | #include "ble/BLE.h" |
screamer | 0:eb7f02ad28a7 | 19 | #include "ble/services/HeartRateService.h" |
a29 | 22:519a64a68971 | 20 | #include "ble/services/BatteryService.h" |
a29 | 22:519a64a68971 | 21 | #include "ble/services/DeviceInformationService.h" |
screamer | 0:eb7f02ad28a7 | 22 | |
apalmieri | 13:227a0149b677 | 23 | DigitalOut led1(LED1, 1); |
screamer | 0:eb7f02ad28a7 | 24 | |
screamer | 0:eb7f02ad28a7 | 25 | const static char DEVICE_NAME[] = "HRM1"; |
a29 | 22:519a64a68971 | 26 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
a29 | 22:519a64a68971 | 27 | GattService::UUID_DEVICE_INFORMATION_SERVICE, |
a29 | 22:519a64a68971 | 28 | GattService::UUID_BATTERY_SERVICE}; |
apalmieri | 13:227a0149b677 | 29 | |
screamer | 0:eb7f02ad28a7 | 30 | static volatile bool triggerSensorPolling = false; |
screamer | 0:eb7f02ad28a7 | 31 | |
apalmieri | 2:bc0c0d442a24 | 32 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
screamer | 0:eb7f02ad28a7 | 33 | { |
apalmieri | 13:227a0149b677 | 34 | (void)params; |
apalmieri | 13:227a0149b677 | 35 | BLE::Instance().gap().startAdvertising(); // restart advertising |
screamer | 0:eb7f02ad28a7 | 36 | } |
screamer | 0:eb7f02ad28a7 | 37 | |
screamer | 0:eb7f02ad28a7 | 38 | void periodicCallback(void) |
screamer | 0:eb7f02ad28a7 | 39 | { |
screamer | 0:eb7f02ad28a7 | 40 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
screamer | 0:eb7f02ad28a7 | 41 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
screamer | 0:eb7f02ad28a7 | 42 | * heavy-weight sensor polling from the main thread. */ |
screamer | 0:eb7f02ad28a7 | 43 | triggerSensorPolling = true; |
screamer | 0:eb7f02ad28a7 | 44 | } |
screamer | 0:eb7f02ad28a7 | 45 | |
apalmieri | 13:227a0149b677 | 46 | void onBleInitError(BLE &ble, ble_error_t error) |
apalmieri | 13:227a0149b677 | 47 | { |
apalmieri | 13:227a0149b677 | 48 | (void)ble; |
apalmieri | 13:227a0149b677 | 49 | (void)error; |
apalmieri | 13:227a0149b677 | 50 | /* Initialization error handling should go here */ |
apalmieri | 13:227a0149b677 | 51 | } |
apalmieri | 13:227a0149b677 | 52 | |
apalmieri | 13:227a0149b677 | 53 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
screamer | 0:eb7f02ad28a7 | 54 | { |
apalmieri | 13:227a0149b677 | 55 | BLE& ble = params->ble; |
apalmieri | 13:227a0149b677 | 56 | ble_error_t error = params->error; |
screamer | 0:eb7f02ad28a7 | 57 | |
apalmieri | 13:227a0149b677 | 58 | if (error != BLE_ERROR_NONE) { |
apalmieri | 13:227a0149b677 | 59 | onBleInitError(ble, error); |
apalmieri | 13:227a0149b677 | 60 | return; |
apalmieri | 13:227a0149b677 | 61 | } |
apalmieri | 13:227a0149b677 | 62 | |
apalmieri | 13:227a0149b677 | 63 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
apalmieri | 13:227a0149b677 | 64 | return; |
apalmieri | 13:227a0149b677 | 65 | } |
apalmieri | 13:227a0149b677 | 66 | |
screamer | 0:eb7f02ad28a7 | 67 | ble.gap().onDisconnection(disconnectionCallback); |
screamer | 0:eb7f02ad28a7 | 68 | |
screamer | 0:eb7f02ad28a7 | 69 | /* Setup primary service. */ |
apalmieri | 21:0e7c08f5386f | 70 | uint8_t hrmCounter = 60; // init HRM to 60bps |
screamer | 0:eb7f02ad28a7 | 71 | HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
a29 | 22:519a64a68971 | 72 | |
a29 | 22:519a64a68971 | 73 | /* Setup auxiliary service. */ |
a29 | 22:519a64a68971 | 74 | uint8_t batteryLevel = 90; // init battery level |
a29 | 22:519a64a68971 | 75 | BatteryService battery(ble,batteryLevel); |
a29 | 22:519a64a68971 | 76 | DeviceInformationService deviceInfo(ble, "MY_ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
a29 | 22:519a64a68971 | 77 | |
screamer | 0:eb7f02ad28a7 | 78 | |
screamer | 0:eb7f02ad28a7 | 79 | /* Setup advertising. */ |
screamer | 0:eb7f02ad28a7 | 80 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
screamer | 0:eb7f02ad28a7 | 81 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
screamer | 0:eb7f02ad28a7 | 82 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
screamer | 0:eb7f02ad28a7 | 83 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
screamer | 0:eb7f02ad28a7 | 84 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
screamer | 0:eb7f02ad28a7 | 85 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
screamer | 0:eb7f02ad28a7 | 86 | ble.gap().startAdvertising(); |
screamer | 0:eb7f02ad28a7 | 87 | |
screamer | 0:eb7f02ad28a7 | 88 | // infinite loop |
apalmieri | 13:227a0149b677 | 89 | while (true) { |
screamer | 0:eb7f02ad28a7 | 90 | // check for trigger from periodicCallback() |
screamer | 0:eb7f02ad28a7 | 91 | if (triggerSensorPolling && ble.getGapState().connected) { |
screamer | 0:eb7f02ad28a7 | 92 | triggerSensorPolling = false; |
screamer | 0:eb7f02ad28a7 | 93 | |
screamer | 0:eb7f02ad28a7 | 94 | // Do blocking calls or whatever is necessary for sensor polling. |
screamer | 0:eb7f02ad28a7 | 95 | // In our case, we simply update the HRM measurement. |
screamer | 0:eb7f02ad28a7 | 96 | hrmCounter++; |
screamer | 0:eb7f02ad28a7 | 97 | |
apalmieri | 21:0e7c08f5386f | 98 | // 60 <= HRM bps <= 100 |
a29 | 22:519a64a68971 | 99 | if (hrmCounter == 90) { |
apalmieri | 21:0e7c08f5386f | 100 | hrmCounter = 60; |
screamer | 0:eb7f02ad28a7 | 101 | } |
a29 | 22:519a64a68971 | 102 | |
screamer | 0:eb7f02ad28a7 | 103 | // update bps |
screamer | 0:eb7f02ad28a7 | 104 | hrService.updateHeartRate(hrmCounter); |
a29 | 22:519a64a68971 | 105 | |
a29 | 22:519a64a68971 | 106 | batteryLevel++; |
a29 | 22:519a64a68971 | 107 | |
a29 | 22:519a64a68971 | 108 | // 90 <= battery level <= 100 |
a29 | 22:519a64a68971 | 109 | if (batteryLevel == 100) { |
a29 | 22:519a64a68971 | 110 | batteryLevel = 90; |
a29 | 22:519a64a68971 | 111 | } |
a29 | 22:519a64a68971 | 112 | |
a29 | 22:519a64a68971 | 113 | // update battery level |
a29 | 22:519a64a68971 | 114 | battery.updateBatteryLevel(batteryLevel); |
screamer | 0:eb7f02ad28a7 | 115 | } else { |
screamer | 0:eb7f02ad28a7 | 116 | ble.waitForEvent(); // low power wait for event |
screamer | 0:eb7f02ad28a7 | 117 | } |
screamer | 0:eb7f02ad28a7 | 118 | } |
screamer | 0:eb7f02ad28a7 | 119 | } |
apalmieri | 13:227a0149b677 | 120 | |
apalmieri | 13:227a0149b677 | 121 | int main(void) |
apalmieri | 13:227a0149b677 | 122 | { |
apalmieri | 13:227a0149b677 | 123 | Ticker ticker; |
apalmieri | 13:227a0149b677 | 124 | ticker.attach(periodicCallback, 1); // blink LED every second |
apalmieri | 13:227a0149b677 | 125 | |
apalmieri | 13:227a0149b677 | 126 | BLE::Instance().init(bleInitComplete); |
apalmieri | 13:227a0149b677 | 127 | } |
apalmieri | 14:f715c13eb84f | 128 |