Bike service
Fork of BLE_API by
ble/services/EnvironmentalService.h@1201:66159681aa21, 2016-04-06 (annotated)
- Committer:
- vcoubard
- Date:
- Wed Apr 06 19:16:08 2016 +0100
- Revision:
- 1201:66159681aa21
- Parent:
- 1135:d540a48f650d
Synchronized with git rev f19d0940
Author: Bogdan Marinescu
version v2.6.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 1132:692ddf04fc42 | 1 | /* mbed Microcontroller Library |
vcoubard | 1132:692ddf04fc42 | 2 | * Copyright (c) 2006-2013 ARM Limited |
vcoubard | 1132:692ddf04fc42 | 3 | * |
vcoubard | 1132:692ddf04fc42 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vcoubard | 1132:692ddf04fc42 | 5 | * you may not use this file except in compliance with the License. |
vcoubard | 1132:692ddf04fc42 | 6 | * You may obtain a copy of the License at |
vcoubard | 1132:692ddf04fc42 | 7 | * |
vcoubard | 1132:692ddf04fc42 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vcoubard | 1132:692ddf04fc42 | 9 | * |
vcoubard | 1132:692ddf04fc42 | 10 | * Unless required by applicable law or agreed to in writing, software |
vcoubard | 1132:692ddf04fc42 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vcoubard | 1132:692ddf04fc42 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vcoubard | 1132:692ddf04fc42 | 13 | * See the License for the specific language governing permissions and |
vcoubard | 1132:692ddf04fc42 | 14 | * limitations under the License. |
vcoubard | 1132:692ddf04fc42 | 15 | */ |
vcoubard | 1132:692ddf04fc42 | 16 | |
vcoubard | 1132:692ddf04fc42 | 17 | #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__ |
vcoubard | 1132:692ddf04fc42 | 18 | #define __BLE_ENVIRONMENTAL_SERVICE_H__ |
vcoubard | 1132:692ddf04fc42 | 19 | |
vcoubard | 1132:692ddf04fc42 | 20 | #include "ble/BLE.h" |
vcoubard | 1132:692ddf04fc42 | 21 | |
vcoubard | 1132:692ddf04fc42 | 22 | /** |
vcoubard | 1132:692ddf04fc42 | 23 | * @class EnvironmentalService |
vcoubard | 1135:d540a48f650d | 24 | * @brief BLE Environmental Service. This service provides temperature, humidity and pressure measurement. |
vcoubard | 1135:d540a48f650d | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml |
vcoubard | 1135:d540a48f650d | 26 | * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml |
vcoubard | 1135:d540a48f650d | 27 | * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml |
vcoubard | 1132:692ddf04fc42 | 28 | * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml |
vcoubard | 1132:692ddf04fc42 | 29 | */ |
vcoubard | 1132:692ddf04fc42 | 30 | class EnvironmentalService { |
vcoubard | 1132:692ddf04fc42 | 31 | public: |
vcoubard | 1132:692ddf04fc42 | 32 | typedef int16_t TemperatureType_t; |
vcoubard | 1132:692ddf04fc42 | 33 | typedef uint16_t HumidityType_t; |
vcoubard | 1132:692ddf04fc42 | 34 | typedef uint32_t PressureType_t; |
vcoubard | 1132:692ddf04fc42 | 35 | |
vcoubard | 1132:692ddf04fc42 | 36 | /** |
vcoubard | 1132:692ddf04fc42 | 37 | * @brief EnvironmentalService constructor. |
vcoubard | 1132:692ddf04fc42 | 38 | * @param ble Reference to BLE device. |
vcoubard | 1132:692ddf04fc42 | 39 | * @param temperature_en Enable this characteristic. |
vcoubard | 1132:692ddf04fc42 | 40 | * @param humidity_en Enable this characteristic. |
vcoubard | 1132:692ddf04fc42 | 41 | * @param pressure_en Enable this characteristic. |
vcoubard | 1132:692ddf04fc42 | 42 | */ |
vcoubard | 1132:692ddf04fc42 | 43 | EnvironmentalService(BLE& _ble) : |
vcoubard | 1132:692ddf04fc42 | 44 | ble(_ble), |
vcoubard | 1132:692ddf04fc42 | 45 | temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, &temperature), |
vcoubard | 1132:692ddf04fc42 | 46 | humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, &humidity), |
vcoubard | 1132:692ddf04fc42 | 47 | pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, &pressure) |
vcoubard | 1132:692ddf04fc42 | 48 | { |
vcoubard | 1132:692ddf04fc42 | 49 | static bool serviceAdded = false; /* We should only ever need to add the information service once. */ |
vcoubard | 1132:692ddf04fc42 | 50 | if (serviceAdded) { |
vcoubard | 1132:692ddf04fc42 | 51 | return; |
vcoubard | 1132:692ddf04fc42 | 52 | } |
vcoubard | 1132:692ddf04fc42 | 53 | |
vcoubard | 1132:692ddf04fc42 | 54 | GattCharacteristic *charTable[] = { &humidityCharacteristic, |
vcoubard | 1132:692ddf04fc42 | 55 | &pressureCharacteristic, |
vcoubard | 1132:692ddf04fc42 | 56 | &temperatureCharacteristic }; |
vcoubard | 1132:692ddf04fc42 | 57 | |
vcoubard | 1132:692ddf04fc42 | 58 | GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
vcoubard | 1132:692ddf04fc42 | 59 | |
vcoubard | 1132:692ddf04fc42 | 60 | ble.gattServer().addService(environmentalService); |
vcoubard | 1132:692ddf04fc42 | 61 | serviceAdded = true; |
vcoubard | 1132:692ddf04fc42 | 62 | } |
vcoubard | 1132:692ddf04fc42 | 63 | |
vcoubard | 1132:692ddf04fc42 | 64 | /** |
vcoubard | 1132:692ddf04fc42 | 65 | * @brief Update humidity characteristic. |
vcoubard | 1132:692ddf04fc42 | 66 | * @param newHumidityVal New humidity measurement. |
vcoubard | 1132:692ddf04fc42 | 67 | */ |
vcoubard | 1132:692ddf04fc42 | 68 | void updateHumidity(HumidityType_t newHumidityVal) |
vcoubard | 1132:692ddf04fc42 | 69 | { |
vcoubard | 1132:692ddf04fc42 | 70 | humidity = (HumidityType_t) (newHumidityVal * 100); |
vcoubard | 1132:692ddf04fc42 | 71 | ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, sizeof(HumidityType_t)); |
vcoubard | 1132:692ddf04fc42 | 72 | } |
vcoubard | 1132:692ddf04fc42 | 73 | |
vcoubard | 1132:692ddf04fc42 | 74 | /** |
vcoubard | 1132:692ddf04fc42 | 75 | * @brief Update pressure characteristic. |
vcoubard | 1132:692ddf04fc42 | 76 | * @param newPressureVal New pressure measurement. |
vcoubard | 1132:692ddf04fc42 | 77 | */ |
vcoubard | 1132:692ddf04fc42 | 78 | void updatePressure(PressureType_t newPressureVal) |
vcoubard | 1132:692ddf04fc42 | 79 | { |
vcoubard | 1132:692ddf04fc42 | 80 | pressure = (PressureType_t) (newPressureVal * 10); |
vcoubard | 1132:692ddf04fc42 | 81 | ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, sizeof(PressureType_t)); |
vcoubard | 1132:692ddf04fc42 | 82 | } |
vcoubard | 1132:692ddf04fc42 | 83 | |
vcoubard | 1132:692ddf04fc42 | 84 | /** |
vcoubard | 1132:692ddf04fc42 | 85 | * @brief Update temperature characteristic. |
vcoubard | 1132:692ddf04fc42 | 86 | * @param newTemperatureVal New temperature measurement. |
vcoubard | 1132:692ddf04fc42 | 87 | */ |
vcoubard | 1132:692ddf04fc42 | 88 | void updateTemperature(float newTemperatureVal) |
vcoubard | 1132:692ddf04fc42 | 89 | { |
vcoubard | 1132:692ddf04fc42 | 90 | temperature = (TemperatureType_t) (newTemperatureVal * 100); |
vcoubard | 1132:692ddf04fc42 | 91 | ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, sizeof(TemperatureType_t)); |
vcoubard | 1132:692ddf04fc42 | 92 | } |
vcoubard | 1132:692ddf04fc42 | 93 | |
vcoubard | 1132:692ddf04fc42 | 94 | private: |
vcoubard | 1132:692ddf04fc42 | 95 | BLE& ble; |
vcoubard | 1132:692ddf04fc42 | 96 | |
vcoubard | 1132:692ddf04fc42 | 97 | TemperatureType_t temperature; |
vcoubard | 1132:692ddf04fc42 | 98 | HumidityType_t humidity; |
vcoubard | 1132:692ddf04fc42 | 99 | PressureType_t pressure; |
vcoubard | 1132:692ddf04fc42 | 100 | |
vcoubard | 1132:692ddf04fc42 | 101 | ReadOnlyGattCharacteristic<TemperatureType_t> temperatureCharacteristic; |
vcoubard | 1132:692ddf04fc42 | 102 | ReadOnlyGattCharacteristic<HumidityType_t> humidityCharacteristic; |
vcoubard | 1132:692ddf04fc42 | 103 | ReadOnlyGattCharacteristic<PressureType_t> pressureCharacteristic; |
vcoubard | 1132:692ddf04fc42 | 104 | }; |
vcoubard | 1132:692ddf04fc42 | 105 | |
rgrover1 | 906:8ddab3271c2f | 106 | #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/ |