BLE Library with custom services for the tortuga bike

Dependents:   TORTUGA_BLE

Fork of BLE_API by aapje monkey

Committer:
ptuytsch
Date:
Mon Jul 18 09:25:32 2016 +0000
Revision:
1207:da7745688342
Parent:
1206:8afbec0520f5
make it working with tortuga itself

Who changed what in which revision?

UserRevisionLine numberNew 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 1207:da7745688342 50 batteryLevelCharacteristic3(BATTERY_LEVEL_3_CHARACTERISTIC_UUID, &batteryLevel3, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
ptuytsch 1207:da7745688342 51 {
ptuytsch 1205:86f0783a5420 52
ptuytsch 1205:86f0783a5420 53 GattCharacteristic *charTable[] = {&batteryLevelCharacteristic1, &batteryLevelCharacteristic2, &batteryLevelCharacteristic3};
ptuytsch 1205:86f0783a5420 54 GattService batteryService(BATTERY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
ptuytsch 1205:86f0783a5420 55
ptuytsch 1205:86f0783a5420 56 ble.gattServer().addService(batteryService);
ptuytsch 1207:da7745688342 57 //printf("adress of _ble: %#x\n", &_ble);
ptuytsch 1207:da7745688342 58 //printf("adress of ble: %#x\n", &ble);
ptuytsch 1205:86f0783a5420 59 }
ptuytsch 1205:86f0783a5420 60
ptuytsch 1205:86f0783a5420 61 /**
ptuytsch 1205:86f0783a5420 62 * @brief Update the battery level with a new value. [Valid values lie between 0 and 100];
ptuytsch 1205:86f0783a5420 63 * anything outside this range will be ignored.
ptuytsch 1205:86f0783a5420 64 *
ptuytsch 1205:86f0783a5420 65 * @param newLevel
ptuytsch 1205:86f0783a5420 66 * Update to battery level.
ptuytsch 1205:86f0783a5420 67 */
ptuytsch 1205:86f0783a5420 68 void updateBatteryLevel1(uint8_t newLevel) {
ptuytsch 1207:da7745688342 69 //printf("try to update batlevel 1\n");
ptuytsch 1206:8afbec0520f5 70 if (newLevel != batteryLevel1)
ptuytsch 1206:8afbec0520f5 71 {
ptuytsch 1205:86f0783a5420 72 batteryLevel1 = newLevel;
ptuytsch 1205:86f0783a5420 73 ble.gattServer().write(batteryLevelCharacteristic1.getValueHandle(), &batteryLevel1, 1);
ptuytsch 1207:da7745688342 74 //printf("BLE battery Level 1 update : %i\n", batteryLevel1);
ptuytsch 1207:da7745688342 75 //printf("adress of ble: %#x\n", &ble);
ptuytsch 1206:8afbec0520f5 76 }
ptuytsch 1207:da7745688342 77
ptuytsch 1205:86f0783a5420 78 }
ptuytsch 1205:86f0783a5420 79 void updateBatteryLevel2(uint8_t newLevel) {
ptuytsch 1206:8afbec0520f5 80 if (newLevel != batteryLevel2)
ptuytsch 1206:8afbec0520f5 81 {
ptuytsch 1205:86f0783a5420 82 batteryLevel2 = newLevel;
ptuytsch 1205:86f0783a5420 83 ble.gattServer().write(batteryLevelCharacteristic2.getValueHandle(), &batteryLevel2, 1);
ptuytsch 1206:8afbec0520f5 84 }
ptuytsch 1205:86f0783a5420 85 }
ptuytsch 1205:86f0783a5420 86 void updateBatteryLevel3(uint8_t newLevel) {
ptuytsch 1206:8afbec0520f5 87 if (newLevel != batteryLevel3)
ptuytsch 1206:8afbec0520f5 88 {
ptuytsch 1205:86f0783a5420 89 batteryLevel3 = newLevel;
ptuytsch 1205:86f0783a5420 90 ble.gattServer().write(batteryLevelCharacteristic3.getValueHandle(), &batteryLevel3, 1);
ptuytsch 1207:da7745688342 91 //printf("battery level updated\n");
ptuytsch 1206:8afbec0520f5 92 }
ptuytsch 1205:86f0783a5420 93 }
ptuytsch 1205:86f0783a5420 94
ptuytsch 1205:86f0783a5420 95 protected:
ptuytsch 1205:86f0783a5420 96 BLE &ble;
ptuytsch 1205:86f0783a5420 97 uint8_t batteryLevel1;
ptuytsch 1205:86f0783a5420 98 uint8_t batteryLevel2;
ptuytsch 1205:86f0783a5420 99 uint8_t batteryLevel3;
ptuytsch 1205:86f0783a5420 100 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic1;
ptuytsch 1205:86f0783a5420 101 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic2;
ptuytsch 1205:86f0783a5420 102 ReadOnlyGattCharacteristic<uint8_t> batteryLevelCharacteristic3;
ptuytsch 1205:86f0783a5420 103 };
ptuytsch 1205:86f0783a5420 104
ptuytsch 1205:86f0783a5420 105 #endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/