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