Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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