jgh
Fork of BLE_API by
ble/services/EnvironmentalService.h@835:6556fcb2f51d, 2015-11-02 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Nov 02 09:09:03 2015 +0000
- Revision:
- 835:6556fcb2f51d
- Child:
- 836:09dba2bf7c82
Synchronized with git rev f0f42d11
Author: Xabi Crespo
Support for Environmental Service (temperature, pressure and humidity characteristics)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 835:6556fcb2f51d | 1 | |
rgrover1 | 835:6556fcb2f51d | 2 | #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__ |
rgrover1 | 835:6556fcb2f51d | 3 | #define __BLE_ENVIRONMENTAL_SERVICE_H__ |
rgrover1 | 835:6556fcb2f51d | 4 | |
rgrover1 | 835:6556fcb2f51d | 5 | #include "ble/BLE.h" |
rgrover1 | 835:6556fcb2f51d | 6 | |
rgrover1 | 835:6556fcb2f51d | 7 | /** |
rgrover1 | 835:6556fcb2f51d | 8 | * @class EnvironmentalService |
rgrover1 | 835:6556fcb2f51d | 9 | * @brief BLE Environmental Service. This service provides the location of the thermometer and the temperature. <br> |
rgrover1 | 835:6556fcb2f51d | 10 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.environmental_sensing.xml <br> |
rgrover1 | 835:6556fcb2f51d | 11 | * Temperature: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature.xml <br> |
rgrover1 | 835:6556fcb2f51d | 12 | * Humidity: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.humidity.xml <br> |
rgrover1 | 835:6556fcb2f51d | 13 | * Pressure: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.pressure.xml |
rgrover1 | 835:6556fcb2f51d | 14 | */ |
rgrover1 | 835:6556fcb2f51d | 15 | class EnvironmentalService { |
rgrover1 | 835:6556fcb2f51d | 16 | public: |
rgrover1 | 835:6556fcb2f51d | 17 | /** |
rgrover1 | 835:6556fcb2f51d | 18 | * @brief EnvironmentalService constructor. |
rgrover1 | 835:6556fcb2f51d | 19 | * @param ble Reference to BLE device. |
rgrover1 | 835:6556fcb2f51d | 20 | * @param temperature_en Enable this characteristic. |
rgrover1 | 835:6556fcb2f51d | 21 | * @param humidity_en Enable this characteristic. |
rgrover1 | 835:6556fcb2f51d | 22 | * @param pressure_en Enable this characteristic. |
rgrover1 | 835:6556fcb2f51d | 23 | */ |
rgrover1 | 835:6556fcb2f51d | 24 | EnvironmentalService(BLE &_ble, |
rgrover1 | 835:6556fcb2f51d | 25 | bool temperature_en = false, |
rgrover1 | 835:6556fcb2f51d | 26 | bool humidity_en = false, |
rgrover1 | 835:6556fcb2f51d | 27 | bool pressure_en = false) : |
rgrover1 | 835:6556fcb2f51d | 28 | ble(_ble), |
rgrover1 | 835:6556fcb2f51d | 29 | temperatureCharacteristic(GattCharacteristic::UUID_TEMPERATURE_CHAR, |
rgrover1 | 835:6556fcb2f51d | 30 | (uint8_t *) &temperature, |
rgrover1 | 835:6556fcb2f51d | 31 | (temperature_en) ? 2 : 0, // minLength |
rgrover1 | 835:6556fcb2f51d | 32 | (temperature_en) ? 2 : 0, // maxLength |
rgrover1 | 835:6556fcb2f51d | 33 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
rgrover1 | 835:6556fcb2f51d | 34 | humidityCharacteristic(GattCharacteristic::UUID_HUMIDITY_CHAR, |
rgrover1 | 835:6556fcb2f51d | 35 | (uint8_t *) &humidity, |
rgrover1 | 835:6556fcb2f51d | 36 | (humidity_en) ? 2 : 0, // minLength |
rgrover1 | 835:6556fcb2f51d | 37 | (humidity_en) ? 2 : 0, // maxLength |
rgrover1 | 835:6556fcb2f51d | 38 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ), |
rgrover1 | 835:6556fcb2f51d | 39 | pressureCharacteristic(GattCharacteristic::UUID_PRESSURE_CHAR, |
rgrover1 | 835:6556fcb2f51d | 40 | (uint8_t *) &pressure, |
rgrover1 | 835:6556fcb2f51d | 41 | (pressure_en) ? 4 : 0, // minLength |
rgrover1 | 835:6556fcb2f51d | 42 | (pressure_en) ? 4 : 0, // maxLength |
rgrover1 | 835:6556fcb2f51d | 43 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) |
rgrover1 | 835:6556fcb2f51d | 44 | { |
rgrover1 | 835:6556fcb2f51d | 45 | static bool serviceAdded = false; /* We should only ever need to add the information service once. */ |
rgrover1 | 835:6556fcb2f51d | 46 | if (serviceAdded) { |
rgrover1 | 835:6556fcb2f51d | 47 | return; |
rgrover1 | 835:6556fcb2f51d | 48 | } |
rgrover1 | 835:6556fcb2f51d | 49 | |
rgrover1 | 835:6556fcb2f51d | 50 | GattCharacteristic *charTable[] = { &humidityCharacteristic, |
rgrover1 | 835:6556fcb2f51d | 51 | &pressureCharacteristic, |
rgrover1 | 835:6556fcb2f51d | 52 | &temperatureCharacteristic }; |
rgrover1 | 835:6556fcb2f51d | 53 | |
rgrover1 | 835:6556fcb2f51d | 54 | GattService environmentalService(GattService::UUID_ENVIRONMENTAL_SERVICE, charTable, |
rgrover1 | 835:6556fcb2f51d | 55 | sizeof(charTable) / sizeof(GattCharacteristic *)); |
rgrover1 | 835:6556fcb2f51d | 56 | |
rgrover1 | 835:6556fcb2f51d | 57 | ble.gattServer().addService(environmentalService); |
rgrover1 | 835:6556fcb2f51d | 58 | serviceAdded = true; |
rgrover1 | 835:6556fcb2f51d | 59 | } |
rgrover1 | 835:6556fcb2f51d | 60 | |
rgrover1 | 835:6556fcb2f51d | 61 | /** |
rgrover1 | 835:6556fcb2f51d | 62 | * @brief Update humidity characteristic. |
rgrover1 | 835:6556fcb2f51d | 63 | * @param newHumidityVal New humidity measurement. |
rgrover1 | 835:6556fcb2f51d | 64 | */ |
rgrover1 | 835:6556fcb2f51d | 65 | void updateHumidity(uint16_t newHumidityVal) |
rgrover1 | 835:6556fcb2f51d | 66 | { |
rgrover1 | 835:6556fcb2f51d | 67 | humidity = (uint32_t) (newHumidityVal*100); |
rgrover1 | 835:6556fcb2f51d | 68 | ble.gattServer().write(humidityCharacteristic.getValueHandle(), (uint8_t *) &humidity, 2); |
rgrover1 | 835:6556fcb2f51d | 69 | } |
rgrover1 | 835:6556fcb2f51d | 70 | |
rgrover1 | 835:6556fcb2f51d | 71 | /** |
rgrover1 | 835:6556fcb2f51d | 72 | * @brief Update pressure characteristic. |
rgrover1 | 835:6556fcb2f51d | 73 | * @param newPressureVal New pressure measurement. |
rgrover1 | 835:6556fcb2f51d | 74 | */ |
rgrover1 | 835:6556fcb2f51d | 75 | void updatePressure(uint32_t newPressureVal) |
rgrover1 | 835:6556fcb2f51d | 76 | { |
rgrover1 | 835:6556fcb2f51d | 77 | pressure = (uint32_t) (newPressureVal*10); |
rgrover1 | 835:6556fcb2f51d | 78 | ble.gattServer().write(pressureCharacteristic.getValueHandle(), (uint8_t *) &pressure, 4); |
rgrover1 | 835:6556fcb2f51d | 79 | } |
rgrover1 | 835:6556fcb2f51d | 80 | |
rgrover1 | 835:6556fcb2f51d | 81 | /** |
rgrover1 | 835:6556fcb2f51d | 82 | * @brief Update temperature characteristic. |
rgrover1 | 835:6556fcb2f51d | 83 | * @param newTemperatureVal New temperature measurement. |
rgrover1 | 835:6556fcb2f51d | 84 | */ |
rgrover1 | 835:6556fcb2f51d | 85 | void updateTemperature(float newTemperatureVal) |
rgrover1 | 835:6556fcb2f51d | 86 | { |
rgrover1 | 835:6556fcb2f51d | 87 | temperature = (int16_t) (newTemperatureVal*100); |
rgrover1 | 835:6556fcb2f51d | 88 | ble.gattServer().write(temperatureCharacteristic.getValueHandle(), (uint8_t *) &temperature, 2); |
rgrover1 | 835:6556fcb2f51d | 89 | } |
rgrover1 | 835:6556fcb2f51d | 90 | |
rgrover1 | 835:6556fcb2f51d | 91 | private: |
rgrover1 | 835:6556fcb2f51d | 92 | BLE &ble; |
rgrover1 | 835:6556fcb2f51d | 93 | |
rgrover1 | 835:6556fcb2f51d | 94 | int16_t temperature; |
rgrover1 | 835:6556fcb2f51d | 95 | GattCharacteristic temperatureCharacteristic; |
rgrover1 | 835:6556fcb2f51d | 96 | uint16_t humidity; |
rgrover1 | 835:6556fcb2f51d | 97 | GattCharacteristic humidityCharacteristic; |
rgrover1 | 835:6556fcb2f51d | 98 | uint32_t pressure; |
rgrover1 | 835:6556fcb2f51d | 99 | GattCharacteristic pressureCharacteristic; |
rgrover1 | 835:6556fcb2f51d | 100 | }; |
rgrover1 | 835:6556fcb2f51d | 101 | |
rgrover1 | 835:6556fcb2f51d | 102 | #endif /* #ifndef __BLE_ENVIRONMENTAL_SERVICE_H__*/ |