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