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