Minor fixes

Fork of BLE_API by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BatteryService.h Source File

BatteryService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef __BLE_BATTERY_SERVICE_H__
00018 #define __BLE_BATTERY_SERVICE_H__
00019 
00020 #include "ble/BLE.h"
00021 
00022 /**
00023 * @class BatteryService
00024 * @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br>
00025 * Service:  https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br>
00026 * Battery Level Char:  https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml
00027 */
00028 class BatteryService {
00029 public:
00030     /**
00031     * @param[ref] _ble
00032     *               BLE object for the underlying controller.
00033     * @param[in] level
00034     *               8bit batterly level. Usually used to represent percentage of batterly charge remaining.
00035     */
00036     BatteryService (BLE &_ble, uint8_t level = 100) :
00037         ble(_ble),
00038         batteryLevel(level),
00039         batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
00040 
00041         GattCharacteristic *charTable[] = {&batteryLevelCharacteristic};
00042         GattService         batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00043 
00044         ble.addService(batteryService);
00045     }
00046 
00047     /**
00048      * @brief Update the battery level with a new value. Valid values range from
00049      * 0..100. Anything outside this range will be ignored.
00050      *
00051      * @param newLevel
00052      *              update to battery level.
00053      */
00054     void updateBatteryLevel(uint8_t newLevel) {
00055         batteryLevel = newLevel;
00056         ble.gattServer().write(batteryLevelCharacteristic.getValueHandle(), &batteryLevel, 1);
00057     }
00058 
00059 protected:
00060     BLE &ble;
00061 
00062     uint8_t    batteryLevel;
00063     ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic;
00064 };
00065 
00066 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/