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