High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
ble/services/BatteryService.h@1201:871f0198b762, 2016-05-08 (annotated)
- Committer:
- luzai
- Date:
- Sun May 08 13:48:54 2016 +0000
- Revision:
- 1201:871f0198b762
- Parent:
- 1183:1589830dbdb7
finally ok;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 1139:911c42364719 | 1 | /* mbed Microcontroller Library |
vcoubard | 1139:911c42364719 | 2 | * Copyright (c) 2006-2013 ARM Limited |
vcoubard | 1139:911c42364719 | 3 | * |
vcoubard | 1139:911c42364719 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vcoubard | 1139:911c42364719 | 5 | * you may not use this file except in compliance with the License. |
vcoubard | 1139:911c42364719 | 6 | * You may obtain a copy of the License at |
vcoubard | 1139:911c42364719 | 7 | * |
vcoubard | 1139:911c42364719 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vcoubard | 1139:911c42364719 | 9 | * |
vcoubard | 1139:911c42364719 | 10 | * Unless required by applicable law or agreed to in writing, software |
vcoubard | 1139:911c42364719 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vcoubard | 1139:911c42364719 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vcoubard | 1139:911c42364719 | 13 | * See the License for the specific language governing permissions and |
vcoubard | 1139:911c42364719 | 14 | * limitations under the License. |
vcoubard | 1139:911c42364719 | 15 | */ |
vcoubard | 1139:911c42364719 | 16 | |
vcoubard | 1139:911c42364719 | 17 | #ifndef __BLE_BATTERY_SERVICE_H__ |
vcoubard | 1139:911c42364719 | 18 | #define __BLE_BATTERY_SERVICE_H__ |
vcoubard | 1139:911c42364719 | 19 | |
vcoubard | 1139:911c42364719 | 20 | #include "ble/BLE.h" |
vcoubard | 1139:911c42364719 | 21 | |
vcoubard | 1139:911c42364719 | 22 | /** |
vcoubard | 1139:911c42364719 | 23 | * @class BatteryService |
vcoubard | 1139:911c42364719 | 24 | * @brief BLE Battery Service. This service displays the battery level from 0% to 100%, represented as an 8bit number. |
vcoubard | 1139:911c42364719 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml |
vcoubard | 1139:911c42364719 | 26 | * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml |
vcoubard | 1139:911c42364719 | 27 | */ |
vcoubard | 1139:911c42364719 | 28 | class BatteryService { |
vcoubard | 1139:911c42364719 | 29 | public: |
vcoubard | 1139:911c42364719 | 30 | /** |
vcoubard | 1183:1589830dbdb7 | 31 | * @param[in] _ble |
vcoubard | 1139:911c42364719 | 32 | * BLE object for the underlying controller. |
vcoubard | 1139:911c42364719 | 33 | * @param[in] level |
vcoubard | 1139:911c42364719 | 34 | * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. |
vcoubard | 1139:911c42364719 | 35 | */ |
vcoubard | 1139:911c42364719 | 36 | BatteryService(BLE &_ble, uint8_t level = 100) : |
vcoubard | 1139:911c42364719 | 37 | ble(_ble), |
vcoubard | 1139:911c42364719 | 38 | batteryLevel(level), |
vcoubard | 1139:911c42364719 | 39 | batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
vcoubard | 1139:911c42364719 | 40 | |
vcoubard | 1139:911c42364719 | 41 | GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
vcoubard | 1139:911c42364719 | 42 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
vcoubard | 1139:911c42364719 | 43 | |
vcoubard | 1139:911c42364719 | 44 | ble.addService(batteryService); |
vcoubard | 1139:911c42364719 | 45 | } |
vcoubard | 1139:911c42364719 | 46 | |
vcoubard | 1139:911c42364719 | 47 | /** |
vcoubard | 1183:1589830dbdb7 | 48 | * @brief Update the battery level with a new value. Valid values lie between 0 and 100, |
vcoubard | 1139:911c42364719 | 49 | * anything outside this range will be ignored. |
vcoubard | 1139:911c42364719 | 50 | * |
vcoubard | 1139:911c42364719 | 51 | * @param newLevel |
vcoubard | 1139:911c42364719 | 52 | * Update to battery level. |
vcoubard | 1139:911c42364719 | 53 | */ |
vcoubard | 1139:911c42364719 | 54 | void updateBatteryLevel(uint8_t newLevel) { |
vcoubard | 1139:911c42364719 | 55 | batteryLevel = newLevel; |
vcoubard | 1139:911c42364719 | 56 | ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1); |
vcoubard | 1139:911c42364719 | 57 | } |
vcoubard | 1139:911c42364719 | 58 | |
vcoubard | 1139:911c42364719 | 59 | protected: |
vcoubard | 1183:1589830dbdb7 | 60 | /** |
vcoubard | 1183:1589830dbdb7 | 61 | * A reference to the underlying BLE instance that this object is attached to. |
vcoubard | 1183:1589830dbdb7 | 62 | * The services and characteristics will be registered in this BLE instance. |
vcoubard | 1183:1589830dbdb7 | 63 | */ |
vcoubard | 1139:911c42364719 | 64 | BLE &ble; |
vcoubard | 1139:911c42364719 | 65 | |
vcoubard | 1183:1589830dbdb7 | 66 | /** |
vcoubard | 1183:1589830dbdb7 | 67 | * The current battery level represented as an integer from 0% to 100%. |
vcoubard | 1183:1589830dbdb7 | 68 | */ |
vcoubard | 1139:911c42364719 | 69 | uint8_t batteryLevel; |
vcoubard | 1183:1589830dbdb7 | 70 | /** |
vcoubard | 1183:1589830dbdb7 | 71 | * A ReadOnlyGattCharacteristic that allows access to the peer device to the |
vcoubard | 1183:1589830dbdb7 | 72 | * batteryLevel value through BLE. |
vcoubard | 1183:1589830dbdb7 | 73 | */ |
vcoubard | 1139:911c42364719 | 74 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic; |
vcoubard | 1139:911c42364719 | 75 | }; |
vcoubard | 1139:911c42364719 | 76 | |
rgrover1 | 712:b04b5db36865 | 77 | #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |