Preliminary main mbed library for nexpaq development
features/FEATURE_BLE/ble/services/HeartRateService.h@1:d96dbedaebdb, 2016-11-04 (annotated)
- 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?
User | Revision | Line number | New 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_HEART_RATE_SERVICE_H__ |
nexpaq | 0:6c56fb4bc5f0 | 18 | #define __BLE_HEART_RATE_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 HeartRateService |
nexpaq | 0:6c56fb4bc5f0 | 24 | * @brief BLE Service for HeartRate. This BLE Service contains the location of the sensor and the heart rate in beats per minute. |
nexpaq | 0:6c56fb4bc5f0 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml |
nexpaq | 0:6c56fb4bc5f0 | 26 | * HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml |
nexpaq | 0:6c56fb4bc5f0 | 27 | * Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml |
nexpaq | 0:6c56fb4bc5f0 | 28 | */ |
nexpaq | 0:6c56fb4bc5f0 | 29 | class HeartRateService { |
nexpaq | 0:6c56fb4bc5f0 | 30 | public: |
nexpaq | 0:6c56fb4bc5f0 | 31 | /** |
nexpaq | 0:6c56fb4bc5f0 | 32 | * @enum SensorLocation |
nexpaq | 0:6c56fb4bc5f0 | 33 | * @brief Location of the heart rate sensor on body. |
nexpaq | 0:6c56fb4bc5f0 | 34 | */ |
nexpaq | 0:6c56fb4bc5f0 | 35 | enum { |
nexpaq | 0:6c56fb4bc5f0 | 36 | LOCATION_OTHER = 0, /*!< Other location. */ |
nexpaq | 0:6c56fb4bc5f0 | 37 | LOCATION_CHEST, /*!< Chest. */ |
nexpaq | 0:6c56fb4bc5f0 | 38 | LOCATION_WRIST, /*!< Wrist. */ |
nexpaq | 0:6c56fb4bc5f0 | 39 | LOCATION_FINGER, /*!< Finger. */ |
nexpaq | 0:6c56fb4bc5f0 | 40 | LOCATION_HAND, /*!< Hand. */ |
nexpaq | 0:6c56fb4bc5f0 | 41 | LOCATION_EAR_LOBE, /*!< Earlobe. */ |
nexpaq | 0:6c56fb4bc5f0 | 42 | LOCATION_FOOT, /*!< Foot. */ |
nexpaq | 0:6c56fb4bc5f0 | 43 | }; |
nexpaq | 0:6c56fb4bc5f0 | 44 | |
nexpaq | 0:6c56fb4bc5f0 | 45 | public: |
nexpaq | 0:6c56fb4bc5f0 | 46 | /** |
nexpaq | 0:6c56fb4bc5f0 | 47 | * @brief Constructor with 8-bit HRM Counter value. |
nexpaq | 0:6c56fb4bc5f0 | 48 | * |
nexpaq | 0:6c56fb4bc5f0 | 49 | * @param[ref] _ble |
nexpaq | 0:6c56fb4bc5f0 | 50 | * Reference to the underlying BLE. |
nexpaq | 0:6c56fb4bc5f0 | 51 | * @param[in] hrmCounter (8-bit) |
nexpaq | 0:6c56fb4bc5f0 | 52 | * Initial value for the HRM counter. |
nexpaq | 0:6c56fb4bc5f0 | 53 | * @param[in] location |
nexpaq | 0:6c56fb4bc5f0 | 54 | * Sensor's location. |
nexpaq | 0:6c56fb4bc5f0 | 55 | */ |
nexpaq | 0:6c56fb4bc5f0 | 56 | HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) : |
nexpaq | 0:6c56fb4bc5f0 | 57 | ble(_ble), |
nexpaq | 0:6c56fb4bc5f0 | 58 | valueBytes(hrmCounter), |
nexpaq | 0:6c56fb4bc5f0 | 59 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
nexpaq | 0:6c56fb4bc5f0 | 60 | valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, |
nexpaq | 0:6c56fb4bc5f0 | 61 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
nexpaq | 0:6c56fb4bc5f0 | 62 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location), |
nexpaq | 0:6c56fb4bc5f0 | 63 | controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) { |
nexpaq | 0:6c56fb4bc5f0 | 64 | setupService(); |
nexpaq | 0:6c56fb4bc5f0 | 65 | } |
nexpaq | 0:6c56fb4bc5f0 | 66 | |
nexpaq | 0:6c56fb4bc5f0 | 67 | /** |
nexpaq | 0:6c56fb4bc5f0 | 68 | * @brief Constructor with a 16-bit HRM Counter value. |
nexpaq | 0:6c56fb4bc5f0 | 69 | * |
nexpaq | 0:6c56fb4bc5f0 | 70 | * @param[in] _ble |
nexpaq | 0:6c56fb4bc5f0 | 71 | * Reference to the underlying BLE. |
nexpaq | 0:6c56fb4bc5f0 | 72 | * @param[in] hrmCounter (8-bit) |
nexpaq | 0:6c56fb4bc5f0 | 73 | * Initial value for the HRM counter. |
nexpaq | 0:6c56fb4bc5f0 | 74 | * @param[in] location |
nexpaq | 0:6c56fb4bc5f0 | 75 | * Sensor's location. |
nexpaq | 0:6c56fb4bc5f0 | 76 | */ |
nexpaq | 0:6c56fb4bc5f0 | 77 | HeartRateService(BLE &_ble, uint16_t hrmCounter, uint8_t location) : |
nexpaq | 0:6c56fb4bc5f0 | 78 | ble(_ble), |
nexpaq | 0:6c56fb4bc5f0 | 79 | valueBytes(hrmCounter), |
nexpaq | 0:6c56fb4bc5f0 | 80 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
nexpaq | 0:6c56fb4bc5f0 | 81 | valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, |
nexpaq | 0:6c56fb4bc5f0 | 82 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
nexpaq | 0:6c56fb4bc5f0 | 83 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location), |
nexpaq | 0:6c56fb4bc5f0 | 84 | controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) { |
nexpaq | 0:6c56fb4bc5f0 | 85 | setupService(); |
nexpaq | 0:6c56fb4bc5f0 | 86 | } |
nexpaq | 0:6c56fb4bc5f0 | 87 | |
nexpaq | 0:6c56fb4bc5f0 | 88 | /** |
nexpaq | 0:6c56fb4bc5f0 | 89 | * @brief Set a new 8-bit value for the heart rate. |
nexpaq | 0:6c56fb4bc5f0 | 90 | * |
nexpaq | 0:6c56fb4bc5f0 | 91 | * @param[in] hrmCounter |
nexpaq | 0:6c56fb4bc5f0 | 92 | * Heart rate in BPM. |
nexpaq | 0:6c56fb4bc5f0 | 93 | */ |
nexpaq | 0:6c56fb4bc5f0 | 94 | void updateHeartRate(uint8_t hrmCounter) { |
nexpaq | 0:6c56fb4bc5f0 | 95 | valueBytes.updateHeartRate(hrmCounter); |
nexpaq | 0:6c56fb4bc5f0 | 96 | ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
nexpaq | 0:6c56fb4bc5f0 | 97 | } |
nexpaq | 0:6c56fb4bc5f0 | 98 | |
nexpaq | 0:6c56fb4bc5f0 | 99 | /** |
nexpaq | 0:6c56fb4bc5f0 | 100 | * Set a new 16-bit value for the heart rate. |
nexpaq | 0:6c56fb4bc5f0 | 101 | * |
nexpaq | 0:6c56fb4bc5f0 | 102 | * @param[in] hrmCounter |
nexpaq | 0:6c56fb4bc5f0 | 103 | * Heart rate in BPM. |
nexpaq | 0:6c56fb4bc5f0 | 104 | */ |
nexpaq | 0:6c56fb4bc5f0 | 105 | void updateHeartRate(uint16_t hrmCounter) { |
nexpaq | 0:6c56fb4bc5f0 | 106 | valueBytes.updateHeartRate(hrmCounter); |
nexpaq | 0:6c56fb4bc5f0 | 107 | ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
nexpaq | 0:6c56fb4bc5f0 | 108 | } |
nexpaq | 0:6c56fb4bc5f0 | 109 | |
nexpaq | 0:6c56fb4bc5f0 | 110 | /** |
nexpaq | 0:6c56fb4bc5f0 | 111 | * This callback allows the heart rate service to receive updates to the |
nexpaq | 0:6c56fb4bc5f0 | 112 | * controlPoint characteristic. |
nexpaq | 0:6c56fb4bc5f0 | 113 | * |
nexpaq | 0:6c56fb4bc5f0 | 114 | * @param[in] params |
nexpaq | 0:6c56fb4bc5f0 | 115 | * Information about the characterisitc being updated. |
nexpaq | 0:6c56fb4bc5f0 | 116 | */ |
nexpaq | 0:6c56fb4bc5f0 | 117 | virtual void onDataWritten(const GattWriteCallbackParams *params) { |
nexpaq | 0:6c56fb4bc5f0 | 118 | if (params->handle == controlPoint.getValueAttribute().getHandle()) { |
nexpaq | 0:6c56fb4bc5f0 | 119 | /* Do something here if the new value is 1; else you can override this method by |
nexpaq | 0:6c56fb4bc5f0 | 120 | * extending this class. |
nexpaq | 0:6c56fb4bc5f0 | 121 | * @NOTE: If you are extending this class, be sure to also call |
nexpaq | 0:6c56fb4bc5f0 | 122 | * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in |
nexpaq | 0:6c56fb4bc5f0 | 123 | * your constructor. |
nexpaq | 0:6c56fb4bc5f0 | 124 | */ |
nexpaq | 0:6c56fb4bc5f0 | 125 | } |
nexpaq | 0:6c56fb4bc5f0 | 126 | } |
nexpaq | 0:6c56fb4bc5f0 | 127 | |
nexpaq | 0:6c56fb4bc5f0 | 128 | protected: |
nexpaq | 0:6c56fb4bc5f0 | 129 | void setupService(void) { |
nexpaq | 0:6c56fb4bc5f0 | 130 | GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint}; |
nexpaq | 0:6c56fb4bc5f0 | 131 | GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
nexpaq | 0:6c56fb4bc5f0 | 132 | |
nexpaq | 0:6c56fb4bc5f0 | 133 | ble.addService(hrmService); |
nexpaq | 0:6c56fb4bc5f0 | 134 | ble.onDataWritten(this, &HeartRateService::onDataWritten); |
nexpaq | 0:6c56fb4bc5f0 | 135 | } |
nexpaq | 0:6c56fb4bc5f0 | 136 | |
nexpaq | 0:6c56fb4bc5f0 | 137 | protected: |
nexpaq | 0:6c56fb4bc5f0 | 138 | /* Private internal representation for the bytes used to work with the value of the heart rate characteristic. */ |
nexpaq | 0:6c56fb4bc5f0 | 139 | struct HeartRateValueBytes { |
nexpaq | 0:6c56fb4bc5f0 | 140 | static const unsigned MAX_VALUE_BYTES = 3; /* Flags, and up to two bytes for heart rate. */ |
nexpaq | 0:6c56fb4bc5f0 | 141 | static const unsigned FLAGS_BYTE_INDEX = 0; |
nexpaq | 0:6c56fb4bc5f0 | 142 | |
nexpaq | 0:6c56fb4bc5f0 | 143 | static const unsigned VALUE_FORMAT_BITNUM = 0; |
nexpaq | 0:6c56fb4bc5f0 | 144 | static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM); |
nexpaq | 0:6c56fb4bc5f0 | 145 | |
nexpaq | 0:6c56fb4bc5f0 | 146 | HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() { |
nexpaq | 0:6c56fb4bc5f0 | 147 | updateHeartRate(hrmCounter); |
nexpaq | 0:6c56fb4bc5f0 | 148 | } |
nexpaq | 0:6c56fb4bc5f0 | 149 | |
nexpaq | 0:6c56fb4bc5f0 | 150 | HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() { |
nexpaq | 0:6c56fb4bc5f0 | 151 | updateHeartRate(hrmCounter); |
nexpaq | 0:6c56fb4bc5f0 | 152 | } |
nexpaq | 0:6c56fb4bc5f0 | 153 | |
nexpaq | 0:6c56fb4bc5f0 | 154 | void updateHeartRate(uint8_t hrmCounter) { |
nexpaq | 0:6c56fb4bc5f0 | 155 | valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG; |
nexpaq | 0:6c56fb4bc5f0 | 156 | valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter; |
nexpaq | 0:6c56fb4bc5f0 | 157 | } |
nexpaq | 0:6c56fb4bc5f0 | 158 | |
nexpaq | 0:6c56fb4bc5f0 | 159 | void updateHeartRate(uint16_t hrmCounter) { |
nexpaq | 0:6c56fb4bc5f0 | 160 | valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG; |
nexpaq | 0:6c56fb4bc5f0 | 161 | valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF); |
nexpaq | 0:6c56fb4bc5f0 | 162 | valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8); |
nexpaq | 0:6c56fb4bc5f0 | 163 | } |
nexpaq | 0:6c56fb4bc5f0 | 164 | |
nexpaq | 0:6c56fb4bc5f0 | 165 | uint8_t *getPointer(void) { |
nexpaq | 0:6c56fb4bc5f0 | 166 | return valueBytes; |
nexpaq | 0:6c56fb4bc5f0 | 167 | } |
nexpaq | 0:6c56fb4bc5f0 | 168 | |
nexpaq | 0:6c56fb4bc5f0 | 169 | const uint8_t *getPointer(void) const { |
nexpaq | 0:6c56fb4bc5f0 | 170 | return valueBytes; |
nexpaq | 0:6c56fb4bc5f0 | 171 | } |
nexpaq | 0:6c56fb4bc5f0 | 172 | |
nexpaq | 0:6c56fb4bc5f0 | 173 | unsigned getNumValueBytes(void) const { |
nexpaq | 0:6c56fb4bc5f0 | 174 | return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t)); |
nexpaq | 0:6c56fb4bc5f0 | 175 | } |
nexpaq | 0:6c56fb4bc5f0 | 176 | |
nexpaq | 0:6c56fb4bc5f0 | 177 | private: |
nexpaq | 0:6c56fb4bc5f0 | 178 | /* First byte: 8-bit values, no extra info. Second byte: uint8_t HRM value */ |
nexpaq | 0:6c56fb4bc5f0 | 179 | /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
nexpaq | 0:6c56fb4bc5f0 | 180 | uint8_t valueBytes[MAX_VALUE_BYTES]; |
nexpaq | 0:6c56fb4bc5f0 | 181 | }; |
nexpaq | 0:6c56fb4bc5f0 | 182 | |
nexpaq | 0:6c56fb4bc5f0 | 183 | protected: |
nexpaq | 0:6c56fb4bc5f0 | 184 | BLE &ble; |
nexpaq | 0:6c56fb4bc5f0 | 185 | |
nexpaq | 0:6c56fb4bc5f0 | 186 | HeartRateValueBytes valueBytes; |
nexpaq | 0:6c56fb4bc5f0 | 187 | uint8_t controlPointValue; |
nexpaq | 0:6c56fb4bc5f0 | 188 | |
nexpaq | 0:6c56fb4bc5f0 | 189 | GattCharacteristic hrmRate; |
nexpaq | 0:6c56fb4bc5f0 | 190 | ReadOnlyGattCharacteristic<uint8_t> hrmLocation; |
nexpaq | 0:6c56fb4bc5f0 | 191 | WriteOnlyGattCharacteristic<uint8_t> controlPoint; |
nexpaq | 0:6c56fb4bc5f0 | 192 | }; |
nexpaq | 0:6c56fb4bc5f0 | 193 | |
nexpaq | 0:6c56fb4bc5f0 | 194 | #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/ |