テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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