Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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