Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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