BLE Library with custom services for the tortuga bike
Fork of BLE_API by
BikeBatteryService.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% to 100%, represented as an 8bit number. 00025 * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml 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 const static uint16_t BATTERY_SERVICE_UUID = 0x3000; 00032 00033 const static uint16_t BATTERY_LEVEL_1_CHARACTERISTIC_UUID = 0x3001; 00034 const static uint16_t BATTERY_LEVEL_2_CHARACTERISTIC_UUID = 0x3002; 00035 const static uint16_t BATTERY_LEVEL_3_CHARACTERISTIC_UUID = 0x3003; 00036 00037 /** 00038 * @param[ref] _ble 00039 * BLE object for the underlying controller. 00040 * @param[in] level 00041 * 8bit batterly level. Usually used to represent percentage of batterly charge remaining. 00042 */ 00043 BatteryService (BLE &_ble, uint8_t level1 = 100, uint8_t level2 = 100, uint8_t level3 = 100) : 00044 ble(_ble), 00045 batteryLevel1(level1), 00046 batteryLevel2(level2), 00047 batteryLevel3(level3), 00048 batteryLevelCharacteristic1(BATTERY_LEVEL_1_CHARACTERISTIC_UUID, &batteryLevel1, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00049 batteryLevelCharacteristic2(BATTERY_LEVEL_2_CHARACTERISTIC_UUID, &batteryLevel2, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00050 batteryLevelCharacteristic3(BATTERY_LEVEL_3_CHARACTERISTIC_UUID, &batteryLevel3, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) 00051 { 00052 00053 GattCharacteristic *charTable[] = {&batteryLevelCharacteristic1, &batteryLevelCharacteristic2, &batteryLevelCharacteristic3}; 00054 GattService batteryService(BATTERY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00055 00056 ble.gattServer().addService(batteryService); 00057 //printf("adress of _ble: %#x\n", &_ble); 00058 //printf("adress of ble: %#x\n", &ble); 00059 } 00060 00061 /** 00062 * @brief Update the battery level with a new value. [Valid values lie between 0 and 100]; 00063 * anything outside this range will be ignored. 00064 * 00065 * @param newLevel 00066 * Update to battery level. 00067 */ 00068 void updateBatteryLevel1(uint8_t newLevel) { 00069 //printf("try to update batlevel 1\n"); 00070 if (newLevel != batteryLevel1) 00071 { 00072 batteryLevel1 = newLevel; 00073 ble.gattServer().write(batteryLevelCharacteristic1.getValueHandle(), &batteryLevel1, 1); 00074 //printf("BLE battery Level 1 update : %i\n", batteryLevel1); 00075 //printf("adress of ble: %#x\n", &ble); 00076 } 00077 00078 } 00079 void updateBatteryLevel2(uint8_t newLevel) { 00080 if (newLevel != batteryLevel2) 00081 { 00082 batteryLevel2 = newLevel; 00083 ble.gattServer().write(batteryLevelCharacteristic2.getValueHandle(), &batteryLevel2, 1); 00084 } 00085 } 00086 void updateBatteryLevel3(uint8_t newLevel) { 00087 if (newLevel != batteryLevel3) 00088 { 00089 batteryLevel3 = newLevel; 00090 ble.gattServer().write(batteryLevelCharacteristic3.getValueHandle(), &batteryLevel3, 1); 00091 //printf("battery level updated\n"); 00092 } 00093 } 00094 00095 protected: 00096 BLE &ble; 00097 uint8_t batteryLevel1; 00098 uint8_t batteryLevel2; 00099 uint8_t batteryLevel3; 00100 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic1; 00101 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic2; 00102 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic3; 00103 }; 00104 00105 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/
Generated on Tue Jul 12 2022 21:14:55 by
1.7.2
