Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_WallbotBLE_Challenge by
HealthThermometerService.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__ 00018 #define __BLE_HEALTH_THERMOMETER_SERVICE_H__ 00019 00020 #include "BLEDevice.h" 00021 00022 /* Health Thermometer Service */ 00023 /* Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml */ 00024 /* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ 00025 /* Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml */ 00026 class HealthThermometerService { 00027 public: 00028 enum { 00029 LOCATION_ARMPIT = 1, 00030 LOCATION_BODY, 00031 LOCATION_EAR, 00032 LOCATION_FINGER, 00033 LOCATION_GI_TRACT, 00034 LOCATION_MOUTH, 00035 LOCATION_RECTUM, 00036 LOCATION_TOE, 00037 LOCATION_EAR_DRUM, 00038 }; 00039 00040 public: 00041 00042 /** 00043 * @param[in] _ble reference to the BLE device 00044 * @param[in] initialTemp initial value in celsius 00045 * @param[in] _location 00046 */ 00047 HealthThermometerService(BLEDevice &_ble, float initialTemp, uint8_t _location) : 00048 ble(_ble), 00049 valueBytes(initialTemp), 00050 tempMeasurement(GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, valueBytes.getPointer(), 00051 sizeof(TemperatureValueBytes), sizeof(TemperatureValueBytes), 00052 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00053 tempLocation(GattCharacteristic::UUID_TEMPERATURE_TYPE_CHAR, (uint8_t *)&_location, sizeof(_location), sizeof(_location), 00054 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) { 00055 00056 GattCharacteristic *hrmChars[] = {&tempMeasurement, &tempLocation, }; 00057 GattService hrmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); 00058 00059 ble.addService(hrmService); 00060 } 00061 00062 void updateTemperature(float temperature) { 00063 if (ble.getGapState().connected) { 00064 valueBytes.updateTemperature(temperature); 00065 ble.updateCharacteristicValue (tempMeasurement.getValueAttribute().getHandle(), valueBytes.getPointer(), sizeof(TemperatureValueBytes)); 00066 } 00067 } 00068 00069 private: 00070 /* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */ 00071 struct TemperatureValueBytes { 00072 static const unsigned OFFSET_OF_FLAGS = 0; 00073 static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS + sizeof(uint8_t); 00074 static const unsigned SIZEOF_VALUE_BYTES = sizeof(uint8_t) + sizeof(float); 00075 00076 static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0; 00077 static const unsigned TIMESTAMP_FLAG_POS = 1; 00078 static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2; 00079 00080 static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0; 00081 static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1; 00082 00083 TemperatureValueBytes(float initialTemperature) : bytes() { 00084 /* assumption: temperature values are expressed in Celsius */ 00085 bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) | 00086 (false << TIMESTAMP_FLAG_POS) | 00087 (false << TEMPERATURE_TYPE_FLAG_POS); 00088 updateTemperature(initialTemperature); 00089 } 00090 00091 void updateTemperature(float temp) { 00092 uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp); 00093 memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float)); 00094 } 00095 00096 uint8_t *getPointer(void) { 00097 return bytes; 00098 } 00099 00100 const uint8_t *getPointer(void) const { 00101 return bytes; 00102 } 00103 00104 private: 00105 /** 00106 * @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type. 00107 * @param temperature The temperature as a float. 00108 * @return The temperature in 11073-20601 FLOAT-Type format. 00109 */ 00110 uint32_t quick_ieee11073_from_float(float temperature) { 00111 uint8_t exponent = 0xFE; //exponent is -2 00112 uint32_t mantissa = (uint32_t)(temperature * 100); 00113 00114 return (((uint32_t)exponent) << 24) | mantissa; 00115 } 00116 00117 00118 private: 00119 /* First byte = 8-bit flags, Second field is a float holding the temperature value. */ 00120 /* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */ 00121 uint8_t bytes[SIZEOF_VALUE_BYTES]; 00122 }; 00123 00124 private: 00125 BLEDevice &ble; 00126 TemperatureValueBytes valueBytes; 00127 GattCharacteristic tempMeasurement; 00128 GattCharacteristic tempLocation; 00129 }; 00130 00131 #endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/
Generated on Tue Jul 12 2022 13:52:31 by
