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