IOTIO
Fork of Nucleo_BLE_API by
services/BatteryService.h@0:289fd2dae405, 2014-12-19 (annotated)
- Committer:
- sjallouli
- Date:
- Fri Dec 19 18:54:46 2014 +0000
- Revision:
- 0:289fd2dae405
BLE_API for Nucleo Bluetoothe Low Energy Shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:289fd2dae405 | 1 | /* mbed Microcontroller Library |
sjallouli | 0:289fd2dae405 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:289fd2dae405 | 3 | * |
sjallouli | 0:289fd2dae405 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:289fd2dae405 | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:289fd2dae405 | 6 | * You may obtain a copy of the License at |
sjallouli | 0:289fd2dae405 | 7 | * |
sjallouli | 0:289fd2dae405 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:289fd2dae405 | 9 | * |
sjallouli | 0:289fd2dae405 | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:289fd2dae405 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:289fd2dae405 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:289fd2dae405 | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:289fd2dae405 | 14 | * limitations under the License. |
sjallouli | 0:289fd2dae405 | 15 | */ |
sjallouli | 0:289fd2dae405 | 16 | |
sjallouli | 0:289fd2dae405 | 17 | #ifndef __BLE_BATTERY_SERVICE_H__ |
sjallouli | 0:289fd2dae405 | 18 | #define __BLE_BATTERY_SERVICE_H__ |
sjallouli | 0:289fd2dae405 | 19 | |
sjallouli | 0:289fd2dae405 | 20 | #include "BLEDevice.h" |
sjallouli | 0:289fd2dae405 | 21 | |
sjallouli | 0:289fd2dae405 | 22 | /* Battery Service */ |
sjallouli | 0:289fd2dae405 | 23 | /* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml */ |
sjallouli | 0:289fd2dae405 | 24 | /* Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml */ |
sjallouli | 0:289fd2dae405 | 25 | class BatteryService { |
sjallouli | 0:289fd2dae405 | 26 | public: |
sjallouli | 0:289fd2dae405 | 27 | BatteryService(BLEDevice &_ble, uint8_t level = 100) : |
sjallouli | 0:289fd2dae405 | 28 | ble(_ble), |
sjallouli | 0:289fd2dae405 | 29 | batteryLevel(level), |
sjallouli | 0:289fd2dae405 | 30 | batteryLevelCharacteristic(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, &batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel), |
sjallouli | 0:289fd2dae405 | 31 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
sjallouli | 0:289fd2dae405 | 32 | |
sjallouli | 0:289fd2dae405 | 33 | static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */ |
sjallouli | 0:289fd2dae405 | 34 | if (serviceAdded) { |
sjallouli | 0:289fd2dae405 | 35 | return; |
sjallouli | 0:289fd2dae405 | 36 | } |
sjallouli | 0:289fd2dae405 | 37 | |
sjallouli | 0:289fd2dae405 | 38 | GattCharacteristic *charTable[] = {&batteryLevelCharacteristic}; |
sjallouli | 0:289fd2dae405 | 39 | GattService batteryService(GattService::UUID_BATTERY_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
sjallouli | 0:289fd2dae405 | 40 | |
sjallouli | 0:289fd2dae405 | 41 | ble.addService(batteryService); |
sjallouli | 0:289fd2dae405 | 42 | serviceAdded = true; |
sjallouli | 0:289fd2dae405 | 43 | } |
sjallouli | 0:289fd2dae405 | 44 | |
sjallouli | 0:289fd2dae405 | 45 | /** |
sjallouli | 0:289fd2dae405 | 46 | * Update the battery level with a new value. Valid values range from |
sjallouli | 0:289fd2dae405 | 47 | * 0..100. Anything outside this range will be ignored. |
sjallouli | 0:289fd2dae405 | 48 | * @param newLevel New level. |
sjallouli | 0:289fd2dae405 | 49 | */ |
sjallouli | 0:289fd2dae405 | 50 | void updateBatteryLevel(uint8_t newLevel) { |
sjallouli | 0:289fd2dae405 | 51 | batteryLevel = newLevel; |
sjallouli | 0:289fd2dae405 | 52 | ble.updateCharacteristicValue(batteryLevelCharacteristic.getValueAttribute().getHandle(), &batteryLevel, 1); |
sjallouli | 0:289fd2dae405 | 53 | } |
sjallouli | 0:289fd2dae405 | 54 | |
sjallouli | 0:289fd2dae405 | 55 | private: |
sjallouli | 0:289fd2dae405 | 56 | BLEDevice &ble; |
sjallouli | 0:289fd2dae405 | 57 | uint8_t batteryLevel; |
sjallouli | 0:289fd2dae405 | 58 | GattCharacteristic batteryLevelCharacteristic; |
sjallouli | 0:289fd2dae405 | 59 | }; |
sjallouli | 0:289fd2dae405 | 60 | |
sjallouli | 0:289fd2dae405 | 61 | #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |