BLE Library with custom services for the tortuga bike
Fork of BLE_API by
ble/services/BikeBatteryService.h@1205:86f0783a5420, 2016-07-12 (annotated)
- Committer:
- ptuytsch
- Date:
- Tue Jul 12 11:55:13 2016 +0000
- Revision:
- 1205:86f0783a5420
- Child:
- 1206:8afbec0520f5
finetuning bikeservice and adding name change characteristic
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptuytsch | 1205:86f0783a5420 | 1 | /* mbed Microcontroller Library |
ptuytsch | 1205:86f0783a5420 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ptuytsch | 1205:86f0783a5420 | 3 | * |
ptuytsch | 1205:86f0783a5420 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ptuytsch | 1205:86f0783a5420 | 5 | * you may not use this file except in compliance with the License. |
ptuytsch | 1205:86f0783a5420 | 6 | * You may obtain a copy of the License at |
ptuytsch | 1205:86f0783a5420 | 7 | * |
ptuytsch | 1205:86f0783a5420 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ptuytsch | 1205:86f0783a5420 | 9 | * |
ptuytsch | 1205:86f0783a5420 | 10 | * Unless required by applicable law or agreed to in writing, software |
ptuytsch | 1205:86f0783a5420 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ptuytsch | 1205:86f0783a5420 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ptuytsch | 1205:86f0783a5420 | 13 | * See the License for the specific language governing permissions and |
ptuytsch | 1205:86f0783a5420 | 14 | * limitations under the License. |
ptuytsch | 1205:86f0783a5420 | 15 | */ |
ptuytsch | 1205:86f0783a5420 | 16 | |
ptuytsch | 1205:86f0783a5420 | 17 | #ifndef __BLE_BATTERY_SERVICE_H__ |
ptuytsch | 1205:86f0783a5420 | 18 | #define __BLE_BATTERY_SERVICE_H__ |
ptuytsch | 1205:86f0783a5420 | 19 | |
ptuytsch | 1205:86f0783a5420 | 20 | #include "ble/BLE.h" |
ptuytsch | 1205:86f0783a5420 | 21 | |
ptuytsch | 1205:86f0783a5420 | 22 | /** |
ptuytsch | 1205:86f0783a5420 | 23 | * @class BatteryService |
ptuytsch | 1205:86f0783a5420 | 24 | * @brief BLE Battery Service. This service displays the battery level from 0% to 100%, represented as an 8bit number. |
ptuytsch | 1205:86f0783a5420 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml |
ptuytsch | 1205:86f0783a5420 | 26 | * Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml |
ptuytsch | 1205:86f0783a5420 | 27 | */ |
ptuytsch | 1205:86f0783a5420 | 28 | class BatteryService { |
ptuytsch | 1205:86f0783a5420 | 29 | public: |
ptuytsch | 1205:86f0783a5420 | 30 | |
ptuytsch | 1205:86f0783a5420 | 31 | const static uint16_t BATTERY_SERVICE_UUID = 0x3000; |
ptuytsch | 1205:86f0783a5420 | 32 | |
ptuytsch | 1205:86f0783a5420 | 33 | const static uint16_t BATTERY_LEVEL_1_CHARACTERISTIC_UUID = 0x3001; |
ptuytsch | 1205:86f0783a5420 | 34 | const static uint16_t BATTERY_LEVEL_2_CHARACTERISTIC_UUID = 0x3002; |
ptuytsch | 1205:86f0783a5420 | 35 | const static uint16_t BATTERY_LEVEL_3_CHARACTERISTIC_UUID = 0x3003; |
ptuytsch | 1205:86f0783a5420 | 36 | |
ptuytsch | 1205:86f0783a5420 | 37 | /** |
ptuytsch | 1205:86f0783a5420 | 38 | * @param[ref] _ble |
ptuytsch | 1205:86f0783a5420 | 39 | * BLE object for the underlying controller. |
ptuytsch | 1205:86f0783a5420 | 40 | * @param[in] level |
ptuytsch | 1205:86f0783a5420 | 41 | * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. |
ptuytsch | 1205:86f0783a5420 | 42 | */ |
ptuytsch | 1205:86f0783a5420 | 43 | BatteryService(BLE &_ble, uint8_t level1 = 100, uint8_t level2 = 100, uint8_t level3 = 100) : |
ptuytsch | 1205:86f0783a5420 | 44 | ble(_ble), |
ptuytsch | 1205:86f0783a5420 | 45 | batteryLevel1(level1), |
ptuytsch | 1205:86f0783a5420 | 46 | batteryLevel2(level2), |
ptuytsch | 1205:86f0783a5420 | 47 | batteryLevel3(level3), |
ptuytsch | 1205:86f0783a5420 | 48 | batteryLevelCharacteristic1(BATTERY_LEVEL_1_CHARACTERISTIC_UUID, &batteryLevel1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1205:86f0783a5420 | 49 | batteryLevelCharacteristic2(BATTERY_LEVEL_2_CHARACTERISTIC_UUID, &batteryLevel2, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
ptuytsch | 1205:86f0783a5420 | 50 | batteryLevelCharacteristic3(BATTERY_LEVEL_3_CHARACTERISTIC_UUID, &batteryLevel3, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) { |
ptuytsch | 1205:86f0783a5420 | 51 | |
ptuytsch | 1205:86f0783a5420 | 52 | GattCharacteristic *charTable[] = {&batteryLevelCharacteristic1, &batteryLevelCharacteristic2, &batteryLevelCharacteristic3}; |
ptuytsch | 1205:86f0783a5420 | 53 | GattService batteryService(BATTERY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
ptuytsch | 1205:86f0783a5420 | 54 | |
ptuytsch | 1205:86f0783a5420 | 55 | ble.gattServer().addService(batteryService); |
ptuytsch | 1205:86f0783a5420 | 56 | } |
ptuytsch | 1205:86f0783a5420 | 57 | |
ptuytsch | 1205:86f0783a5420 | 58 | /** |
ptuytsch | 1205:86f0783a5420 | 59 | * @brief Update the battery level with a new value. [Valid values lie between 0 and 100]; |
ptuytsch | 1205:86f0783a5420 | 60 | * anything outside this range will be ignored. |
ptuytsch | 1205:86f0783a5420 | 61 | * |
ptuytsch | 1205:86f0783a5420 | 62 | * @param newLevel |
ptuytsch | 1205:86f0783a5420 | 63 | * Update to battery level. |
ptuytsch | 1205:86f0783a5420 | 64 | */ |
ptuytsch | 1205:86f0783a5420 | 65 | void updateBatteryLevel1(uint8_t newLevel) { |
ptuytsch | 1205:86f0783a5420 | 66 | /*if (newLevel != batteryLevel) |
ptuytsch | 1205:86f0783a5420 | 67 | {*/ |
ptuytsch | 1205:86f0783a5420 | 68 | batteryLevel1 = newLevel; |
ptuytsch | 1205:86f0783a5420 | 69 | ble.gattServer().write(batteryLevelCharacteristic1.getValueHandle(), &batteryLevel1, 1); |
ptuytsch | 1205:86f0783a5420 | 70 | //} |
ptuytsch | 1205:86f0783a5420 | 71 | } |
ptuytsch | 1205:86f0783a5420 | 72 | void updateBatteryLevel2(uint8_t newLevel) { |
ptuytsch | 1205:86f0783a5420 | 73 | /*if (newLevel != batteryLevel) |
ptuytsch | 1205:86f0783a5420 | 74 | {*/ |
ptuytsch | 1205:86f0783a5420 | 75 | batteryLevel2 = newLevel; |
ptuytsch | 1205:86f0783a5420 | 76 | ble.gattServer().write(batteryLevelCharacteristic2.getValueHandle(), &batteryLevel2, 1); |
ptuytsch | 1205:86f0783a5420 | 77 | //} |
ptuytsch | 1205:86f0783a5420 | 78 | } |
ptuytsch | 1205:86f0783a5420 | 79 | void updateBatteryLevel3(uint8_t newLevel) { |
ptuytsch | 1205:86f0783a5420 | 80 | /*if (newLevel != batteryLevel) |
ptuytsch | 1205:86f0783a5420 | 81 | {*/ |
ptuytsch | 1205:86f0783a5420 | 82 | batteryLevel3 = newLevel; |
ptuytsch | 1205:86f0783a5420 | 83 | ble.gattServer().write(batteryLevelCharacteristic3.getValueHandle(), &batteryLevel3, 1); |
ptuytsch | 1205:86f0783a5420 | 84 | //} |
ptuytsch | 1205:86f0783a5420 | 85 | } |
ptuytsch | 1205:86f0783a5420 | 86 | |
ptuytsch | 1205:86f0783a5420 | 87 | protected: |
ptuytsch | 1205:86f0783a5420 | 88 | BLE &ble; |
ptuytsch | 1205:86f0783a5420 | 89 | uint8_t batteryLevel1; |
ptuytsch | 1205:86f0783a5420 | 90 | uint8_t batteryLevel2; |
ptuytsch | 1205:86f0783a5420 | 91 | uint8_t batteryLevel3; |
ptuytsch | 1205:86f0783a5420 | 92 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic1; |
ptuytsch | 1205:86f0783a5420 | 93 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic2; |
ptuytsch | 1205:86f0783a5420 | 94 | ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic3; |
ptuytsch | 1205:86f0783a5420 | 95 | }; |
ptuytsch | 1205:86f0783a5420 | 96 | |
ptuytsch | 1205:86f0783a5420 | 97 | #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/ |