Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /* mbed Microcontroller Library
switches 0:5c4d7b2438d3 2 * Copyright (c) 2006-2013 ARM Limited
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:5c4d7b2438d3 5 * you may not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 6 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 7 *
switches 0:5c4d7b2438d3 8 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 9 *
switches 0:5c4d7b2438d3 10 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:5c4d7b2438d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 13 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 14 * limitations under the License.
switches 0:5c4d7b2438d3 15 */
switches 0:5c4d7b2438d3 16
switches 0:5c4d7b2438d3 17 #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__
switches 0:5c4d7b2438d3 18 #define __BLE_HEALTH_THERMOMETER_SERVICE_H__
switches 0:5c4d7b2438d3 19
switches 0:5c4d7b2438d3 20 #include "ble/BLE.h"
switches 0:5c4d7b2438d3 21
switches 0:5c4d7b2438d3 22 /**
switches 0:5c4d7b2438d3 23 * @class HealthThermometerService
switches 0:5c4d7b2438d3 24 * @brief BLE Health Thermometer Service. This service provides the location of the thermometer and the temperature.
switches 0:5c4d7b2438d3 25 * Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml
switches 0:5c4d7b2438d3 26 * Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml
switches 0:5c4d7b2438d3 27 * Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml
switches 0:5c4d7b2438d3 28 */
switches 0:5c4d7b2438d3 29 class HealthThermometerService {
switches 0:5c4d7b2438d3 30 public:
switches 0:5c4d7b2438d3 31 /**
switches 0:5c4d7b2438d3 32 * @enum Sensor Location.
switches 0:5c4d7b2438d3 33 * @brief Location of sensor on the body.
switches 0:5c4d7b2438d3 34 */
switches 0:5c4d7b2438d3 35 enum SensorLocation_t {
switches 0:5c4d7b2438d3 36 LOCATION_ARMPIT = 1, /*!< Armpit. */
switches 0:5c4d7b2438d3 37 LOCATION_BODY, /*!< Body. */
switches 0:5c4d7b2438d3 38 LOCATION_EAR, /*!< Ear. */
switches 0:5c4d7b2438d3 39 LOCATION_FINGER, /*!< Finger. */
switches 0:5c4d7b2438d3 40 LOCATION_GI_TRACT, /*!< GI tract */
switches 0:5c4d7b2438d3 41 LOCATION_MOUTH, /*!< Mouth. */
switches 0:5c4d7b2438d3 42 LOCATION_RECTUM, /*!< Rectum. */
switches 0:5c4d7b2438d3 43 LOCATION_TOE, /*!< Toe. */
switches 0:5c4d7b2438d3 44 LOCATION_EAR_DRUM, /*!< Eardrum. */
switches 0:5c4d7b2438d3 45 };
switches 0:5c4d7b2438d3 46
switches 0:5c4d7b2438d3 47 public:
switches 0:5c4d7b2438d3 48 /**
switches 0:5c4d7b2438d3 49 * @brief Add the Health Thermometer Service to an existing BLE object, initialize with temperature and location.
switches 0:5c4d7b2438d3 50 * @param[ref] _ble Reference to the BLE device.
switches 0:5c4d7b2438d3 51 * @param[in] initialTemp Initial value in celsius.
switches 0:5c4d7b2438d3 52 * @param[in] _location
switches 0:5c4d7b2438d3 53 */
switches 0:5c4d7b2438d3 54 HealthThermometerService(BLE &_ble, float initialTemp, uint8_t _location) :
switches 0:5c4d7b2438d3 55 ble(_ble),
switches 0:5c4d7b2438d3 56 valueBytes(initialTemp),
switches 0:5c4d7b2438d3 57 tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, (TemperatureValueBytes *)valueBytes.getPointer(), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
switches 0:5c4d7b2438d3 58 tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, &_location) {
switches 0:5c4d7b2438d3 59
switches 0:5c4d7b2438d3 60 GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, };
switches 0:5c4d7b2438d3 61 GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
switches 0:5c4d7b2438d3 62
switches 0:5c4d7b2438d3 63 ble.addService(hrmService);
switches 0:5c4d7b2438d3 64 }
switches 0:5c4d7b2438d3 65
switches 0:5c4d7b2438d3 66 /**
switches 0:5c4d7b2438d3 67 * @brief Update the temperature being broadcast.
switches 0:5c4d7b2438d3 68 *
switches 0:5c4d7b2438d3 69 * @param[in] temperature
switches 0:5c4d7b2438d3 70 * Floating point value of the temperature.
switches 0:5c4d7b2438d3 71 *
switches 0:5c4d7b2438d3 72 */
switches 0:5c4d7b2438d3 73 void updateTemperature(float temperature) {
switches 0:5c4d7b2438d3 74 if (ble.getGapState().connected) {
switches 0:5c4d7b2438d3 75 valueBytes.updateTemperature(temperature);
switches 0:5c4d7b2438d3 76 ble.gattServer().write(tempMeasurement.getValueHandle(), valueBytes.getPointer(), sizeof(TemperatureValueBytes));
switches 0:5c4d7b2438d3 77 }
switches 0:5c4d7b2438d3 78 }
switches 0:5c4d7b2438d3 79
switches 0:5c4d7b2438d3 80 /**
switches 0:5c4d7b2438d3 81 * @brief Update the location.
switches 0:5c4d7b2438d3 82 * @param loc
switches 0:5c4d7b2438d3 83 * New location value.
switches 0:5c4d7b2438d3 84 */
switches 0:5c4d7b2438d3 85 void updateLocation(SensorLocation_t loc) {
switches 0:5c4d7b2438d3 86 ble.gattServer().write(tempLocation.getValueHandle(), reinterpret_cast<uint8_t *>(&loc), sizeof(uint8_t));
switches 0:5c4d7b2438d3 87 }
switches 0:5c4d7b2438d3 88
switches 0:5c4d7b2438d3 89 private:
switches 0:5c4d7b2438d3 90 /* Private internal representation for the bytes used to work with the vaulue of the temperature characteristic. */
switches 0:5c4d7b2438d3 91 struct TemperatureValueBytes {
switches 0:5c4d7b2438d3 92 static const unsigned OFFSET_OF_FLAGS = 0;
switches 0:5c4d7b2438d3 93 static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t);
switches 0:5c4d7b2438d3 94 static const unsigned SIZEOF_VALUE_BYTES = sizeof(uint8_t) + sizeof(float);
switches 0:5c4d7b2438d3 95
switches 0:5c4d7b2438d3 96 static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0;
switches 0:5c4d7b2438d3 97 static const unsigned TIMESTAMP_FLAG_POS = 1;
switches 0:5c4d7b2438d3 98 static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2;
switches 0:5c4d7b2438d3 99
switches 0:5c4d7b2438d3 100 static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0;
switches 0:5c4d7b2438d3 101 static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1;
switches 0:5c4d7b2438d3 102
switches 0:5c4d7b2438d3 103 TemperatureValueBytes(float initialTemperature) : bytes() {
switches 0:5c4d7b2438d3 104 /* Assumption: temperature values are expressed in celsius */
switches 0:5c4d7b2438d3 105 bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) |
switches 0:5c4d7b2438d3 106 (false << TIMESTAMP_FLAG_POS) |
switches 0:5c4d7b2438d3 107 (false << TEMPERATURE_TYPE_FLAG_POS);
switches 0:5c4d7b2438d3 108 updateTemperature(initialTemperature);
switches 0:5c4d7b2438d3 109 }
switches 0:5c4d7b2438d3 110
switches 0:5c4d7b2438d3 111 void updateTemperature(float temp) {
switches 0:5c4d7b2438d3 112 uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp);
switches 0:5c4d7b2438d3 113 memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float));
switches 0:5c4d7b2438d3 114 }
switches 0:5c4d7b2438d3 115
switches 0:5c4d7b2438d3 116 uint8_t *getPointer(void) {
switches 0:5c4d7b2438d3 117 return bytes;
switches 0:5c4d7b2438d3 118 }
switches 0:5c4d7b2438d3 119
switches 0:5c4d7b2438d3 120 const uint8_t *getPointer(void) const {
switches 0:5c4d7b2438d3 121 return bytes;
switches 0:5c4d7b2438d3 122 }
switches 0:5c4d7b2438d3 123
switches 0:5c4d7b2438d3 124 private:
switches 0:5c4d7b2438d3 125 /**
switches 0:5c4d7b2438d3 126 * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
switches 0:5c4d7b2438d3 127 * @param temperature The temperature as a float.
switches 0:5c4d7b2438d3 128 * @return The temperature in 11073-20601 FLOAT-Type format.
switches 0:5c4d7b2438d3 129 */
switches 0:5c4d7b2438d3 130 uint32_t quick_ieee11073_from_float(float temperature) {
switches 0:5c4d7b2438d3 131 uint8_t exponent = 0xFE; //Exponent is -2
switches 0:5c4d7b2438d3 132 uint32_t mantissa = (uint32_t)(temperature * 100);
switches 0:5c4d7b2438d3 133
switches 0:5c4d7b2438d3 134 return (((uint32_t)exponent) << 24) | mantissa;
switches 0:5c4d7b2438d3 135 }
switches 0:5c4d7b2438d3 136
switches 0:5c4d7b2438d3 137 private:
switches 0:5c4d7b2438d3 138 /* First byte: 8-bit flags. Second field is a float holding the temperature value. */
switches 0:5c4d7b2438d3 139 /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
switches 0:5c4d7b2438d3 140 uint8_t bytes[SIZEOF_VALUE_BYTES];
switches 0:5c4d7b2438d3 141 };
switches 0:5c4d7b2438d3 142
switches 0:5c4d7b2438d3 143 protected:
switches 0:5c4d7b2438d3 144 BLE &ble;
switches 0:5c4d7b2438d3 145 TemperatureValueBytes valueBytes;
switches 0:5c4d7b2438d3 146 ReadOnlyGattCharacteristic<TemperatureValueBytes> tempMeasurement;
switches 0:5c4d7b2438d3 147 ReadOnlyGattCharacteristic<uint8_t> tempLocation;
switches 0:5c4d7b2438d3 148 };
switches 0:5c4d7b2438d3 149
switches 0:5c4d7b2438d3 150 #endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/