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.
Dependents: blinky_max32630fthr
features/FEATURE_BLE/ble/services/HeartRateService.h@3:1198227e6421, 2016-12-16 (annotated)
- Committer:
- switches
- Date:
- Fri Dec 16 16:27:57 2016 +0000
- Revision:
- 3:1198227e6421
- Parent:
- 0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | /* mbed Microcontroller Library |
switches | 0:5c4d7b2438d3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
switches | 0:5c4d7b2438d3 | 3 | * |
switches | 0:5c4d7b2438d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
switches | 0:5c4d7b2438d3 | 5 | * you may not use this file except in compliance with the License. |
switches | 0:5c4d7b2438d3 | 6 | * You may obtain a copy of the License at |
switches | 0:5c4d7b2438d3 | 7 | * |
switches | 0:5c4d7b2438d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
switches | 0:5c4d7b2438d3 | 9 | * |
switches | 0:5c4d7b2438d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
switches | 0:5c4d7b2438d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
switches | 0:5c4d7b2438d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
switches | 0:5c4d7b2438d3 | 13 | * See the License for the specific language governing permissions and |
switches | 0:5c4d7b2438d3 | 14 | * limitations under the License. |
switches | 0:5c4d7b2438d3 | 15 | */ |
switches | 0:5c4d7b2438d3 | 16 | |
switches | 0:5c4d7b2438d3 | 17 | #ifndef __BLE_HEART_RATE_SERVICE_H__ |
switches | 0:5c4d7b2438d3 | 18 | #define __BLE_HEART_RATE_SERVICE_H__ |
switches | 0:5c4d7b2438d3 | 19 | |
switches | 0:5c4d7b2438d3 | 20 | #include "ble/BLE.h" |
switches | 0:5c4d7b2438d3 | 21 | |
switches | 0:5c4d7b2438d3 | 22 | /** |
switches | 0:5c4d7b2438d3 | 23 | * @class HeartRateService |
switches | 0:5c4d7b2438d3 | 24 | * @brief BLE Service for HeartRate. This BLE Service contains the location of the sensor and the heart rate in beats per minute. |
switches | 0:5c4d7b2438d3 | 25 | * Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml |
switches | 0:5c4d7b2438d3 | 26 | * HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml |
switches | 0:5c4d7b2438d3 | 27 | * Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml |
switches | 0:5c4d7b2438d3 | 28 | */ |
switches | 0:5c4d7b2438d3 | 29 | class HeartRateService { |
switches | 0:5c4d7b2438d3 | 30 | public: |
switches | 0:5c4d7b2438d3 | 31 | /** |
switches | 0:5c4d7b2438d3 | 32 | * @enum SensorLocation |
switches | 0:5c4d7b2438d3 | 33 | * @brief Location of the heart rate sensor on body. |
switches | 0:5c4d7b2438d3 | 34 | */ |
switches | 0:5c4d7b2438d3 | 35 | enum { |
switches | 0:5c4d7b2438d3 | 36 | LOCATION_OTHER = 0, /*!< Other location. */ |
switches | 0:5c4d7b2438d3 | 37 | LOCATION_CHEST, /*!< Chest. */ |
switches | 0:5c4d7b2438d3 | 38 | LOCATION_WRIST, /*!< Wrist. */ |
switches | 0:5c4d7b2438d3 | 39 | LOCATION_FINGER, /*!< Finger. */ |
switches | 0:5c4d7b2438d3 | 40 | LOCATION_HAND, /*!< Hand. */ |
switches | 0:5c4d7b2438d3 | 41 | LOCATION_EAR_LOBE, /*!< Earlobe. */ |
switches | 0:5c4d7b2438d3 | 42 | LOCATION_FOOT, /*!< Foot. */ |
switches | 0:5c4d7b2438d3 | 43 | }; |
switches | 0:5c4d7b2438d3 | 44 | |
switches | 0:5c4d7b2438d3 | 45 | public: |
switches | 0:5c4d7b2438d3 | 46 | /** |
switches | 0:5c4d7b2438d3 | 47 | * @brief Constructor with 8-bit HRM Counter value. |
switches | 0:5c4d7b2438d3 | 48 | * |
switches | 0:5c4d7b2438d3 | 49 | * @param[ref] _ble |
switches | 0:5c4d7b2438d3 | 50 | * Reference to the underlying BLE. |
switches | 0:5c4d7b2438d3 | 51 | * @param[in] hrmCounter (8-bit) |
switches | 0:5c4d7b2438d3 | 52 | * Initial value for the HRM counter. |
switches | 0:5c4d7b2438d3 | 53 | * @param[in] location |
switches | 0:5c4d7b2438d3 | 54 | * Sensor's location. |
switches | 0:5c4d7b2438d3 | 55 | */ |
switches | 0:5c4d7b2438d3 | 56 | HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) : |
switches | 0:5c4d7b2438d3 | 57 | ble(_ble), |
switches | 0:5c4d7b2438d3 | 58 | valueBytes(hrmCounter), |
switches | 0:5c4d7b2438d3 | 59 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
switches | 0:5c4d7b2438d3 | 60 | valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, |
switches | 0:5c4d7b2438d3 | 61 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
switches | 0:5c4d7b2438d3 | 62 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location), |
switches | 0:5c4d7b2438d3 | 63 | controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) { |
switches | 0:5c4d7b2438d3 | 64 | setupService(); |
switches | 0:5c4d7b2438d3 | 65 | } |
switches | 0:5c4d7b2438d3 | 66 | |
switches | 0:5c4d7b2438d3 | 67 | /** |
switches | 0:5c4d7b2438d3 | 68 | * @brief Constructor with a 16-bit HRM Counter value. |
switches | 0:5c4d7b2438d3 | 69 | * |
switches | 0:5c4d7b2438d3 | 70 | * @param[in] _ble |
switches | 0:5c4d7b2438d3 | 71 | * Reference to the underlying BLE. |
switches | 0:5c4d7b2438d3 | 72 | * @param[in] hrmCounter (8-bit) |
switches | 0:5c4d7b2438d3 | 73 | * Initial value for the HRM counter. |
switches | 0:5c4d7b2438d3 | 74 | * @param[in] location |
switches | 0:5c4d7b2438d3 | 75 | * Sensor's location. |
switches | 0:5c4d7b2438d3 | 76 | */ |
switches | 0:5c4d7b2438d3 | 77 | HeartRateService(BLE &_ble, uint16_t hrmCounter, uint8_t location) : |
switches | 0:5c4d7b2438d3 | 78 | ble(_ble), |
switches | 0:5c4d7b2438d3 | 79 | valueBytes(hrmCounter), |
switches | 0:5c4d7b2438d3 | 80 | hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, valueBytes.getPointer(), |
switches | 0:5c4d7b2438d3 | 81 | valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES, |
switches | 0:5c4d7b2438d3 | 82 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), |
switches | 0:5c4d7b2438d3 | 83 | hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR, &location), |
switches | 0:5c4d7b2438d3 | 84 | controlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, &controlPointValue) { |
switches | 0:5c4d7b2438d3 | 85 | setupService(); |
switches | 0:5c4d7b2438d3 | 86 | } |
switches | 0:5c4d7b2438d3 | 87 | |
switches | 0:5c4d7b2438d3 | 88 | /** |
switches | 0:5c4d7b2438d3 | 89 | * @brief Set a new 8-bit value for the heart rate. |
switches | 0:5c4d7b2438d3 | 90 | * |
switches | 0:5c4d7b2438d3 | 91 | * @param[in] hrmCounter |
switches | 0:5c4d7b2438d3 | 92 | * Heart rate in BPM. |
switches | 0:5c4d7b2438d3 | 93 | */ |
switches | 0:5c4d7b2438d3 | 94 | void updateHeartRate(uint8_t hrmCounter) { |
switches | 0:5c4d7b2438d3 | 95 | valueBytes.updateHeartRate(hrmCounter); |
switches | 0:5c4d7b2438d3 | 96 | ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
switches | 0:5c4d7b2438d3 | 97 | } |
switches | 0:5c4d7b2438d3 | 98 | |
switches | 0:5c4d7b2438d3 | 99 | /** |
switches | 0:5c4d7b2438d3 | 100 | * Set a new 16-bit value for the heart rate. |
switches | 0:5c4d7b2438d3 | 101 | * |
switches | 0:5c4d7b2438d3 | 102 | * @param[in] hrmCounter |
switches | 0:5c4d7b2438d3 | 103 | * Heart rate in BPM. |
switches | 0:5c4d7b2438d3 | 104 | */ |
switches | 0:5c4d7b2438d3 | 105 | void updateHeartRate(uint16_t hrmCounter) { |
switches | 0:5c4d7b2438d3 | 106 | valueBytes.updateHeartRate(hrmCounter); |
switches | 0:5c4d7b2438d3 | 107 | ble.gattServer().write(hrmRate.getValueHandle(), valueBytes.getPointer(), valueBytes.getNumValueBytes()); |
switches | 0:5c4d7b2438d3 | 108 | } |
switches | 0:5c4d7b2438d3 | 109 | |
switches | 0:5c4d7b2438d3 | 110 | /** |
switches | 0:5c4d7b2438d3 | 111 | * This callback allows the heart rate service to receive updates to the |
switches | 0:5c4d7b2438d3 | 112 | * controlPoint characteristic. |
switches | 0:5c4d7b2438d3 | 113 | * |
switches | 0:5c4d7b2438d3 | 114 | * @param[in] params |
switches | 0:5c4d7b2438d3 | 115 | * Information about the characterisitc being updated. |
switches | 0:5c4d7b2438d3 | 116 | */ |
switches | 0:5c4d7b2438d3 | 117 | virtual void onDataWritten(const GattWriteCallbackParams *params) { |
switches | 0:5c4d7b2438d3 | 118 | if (params->handle == controlPoint.getValueAttribute().getHandle()) { |
switches | 0:5c4d7b2438d3 | 119 | /* Do something here if the new value is 1; else you can override this method by |
switches | 0:5c4d7b2438d3 | 120 | * extending this class. |
switches | 0:5c4d7b2438d3 | 121 | * @NOTE: If you are extending this class, be sure to also call |
switches | 0:5c4d7b2438d3 | 122 | * ble.onDataWritten(this, &ExtendedHRService::onDataWritten); in |
switches | 0:5c4d7b2438d3 | 123 | * your constructor. |
switches | 0:5c4d7b2438d3 | 124 | */ |
switches | 0:5c4d7b2438d3 | 125 | } |
switches | 0:5c4d7b2438d3 | 126 | } |
switches | 0:5c4d7b2438d3 | 127 | |
switches | 0:5c4d7b2438d3 | 128 | protected: |
switches | 0:5c4d7b2438d3 | 129 | void setupService(void) { |
switches | 0:5c4d7b2438d3 | 130 | GattCharacteristic *charTable[] = {&hrmRate, &hrmLocation, &controlPoint}; |
switches | 0:5c4d7b2438d3 | 131 | GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
switches | 0:5c4d7b2438d3 | 132 | |
switches | 0:5c4d7b2438d3 | 133 | ble.addService(hrmService); |
switches | 0:5c4d7b2438d3 | 134 | ble.onDataWritten(this, &HeartRateService::onDataWritten); |
switches | 0:5c4d7b2438d3 | 135 | } |
switches | 0:5c4d7b2438d3 | 136 | |
switches | 0:5c4d7b2438d3 | 137 | protected: |
switches | 0:5c4d7b2438d3 | 138 | /* Private internal representation for the bytes used to work with the value of the heart rate characteristic. */ |
switches | 0:5c4d7b2438d3 | 139 | struct HeartRateValueBytes { |
switches | 0:5c4d7b2438d3 | 140 | static const unsigned MAX_VALUE_BYTES = 3; /* Flags, and up to two bytes for heart rate. */ |
switches | 0:5c4d7b2438d3 | 141 | static const unsigned FLAGS_BYTE_INDEX = 0; |
switches | 0:5c4d7b2438d3 | 142 | |
switches | 0:5c4d7b2438d3 | 143 | static const unsigned VALUE_FORMAT_BITNUM = 0; |
switches | 0:5c4d7b2438d3 | 144 | static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM); |
switches | 0:5c4d7b2438d3 | 145 | |
switches | 0:5c4d7b2438d3 | 146 | HeartRateValueBytes(uint8_t hrmCounter) : valueBytes() { |
switches | 0:5c4d7b2438d3 | 147 | updateHeartRate(hrmCounter); |
switches | 0:5c4d7b2438d3 | 148 | } |
switches | 0:5c4d7b2438d3 | 149 | |
switches | 0:5c4d7b2438d3 | 150 | HeartRateValueBytes(uint16_t hrmCounter) : valueBytes() { |
switches | 0:5c4d7b2438d3 | 151 | updateHeartRate(hrmCounter); |
switches | 0:5c4d7b2438d3 | 152 | } |
switches | 0:5c4d7b2438d3 | 153 | |
switches | 0:5c4d7b2438d3 | 154 | void updateHeartRate(uint8_t hrmCounter) { |
switches | 0:5c4d7b2438d3 | 155 | valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG; |
switches | 0:5c4d7b2438d3 | 156 | valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter; |
switches | 0:5c4d7b2438d3 | 157 | } |
switches | 0:5c4d7b2438d3 | 158 | |
switches | 0:5c4d7b2438d3 | 159 | void updateHeartRate(uint16_t hrmCounter) { |
switches | 0:5c4d7b2438d3 | 160 | valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG; |
switches | 0:5c4d7b2438d3 | 161 | valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF); |
switches | 0:5c4d7b2438d3 | 162 | valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8); |
switches | 0:5c4d7b2438d3 | 163 | } |
switches | 0:5c4d7b2438d3 | 164 | |
switches | 0:5c4d7b2438d3 | 165 | uint8_t *getPointer(void) { |
switches | 0:5c4d7b2438d3 | 166 | return valueBytes; |
switches | 0:5c4d7b2438d3 | 167 | } |
switches | 0:5c4d7b2438d3 | 168 | |
switches | 0:5c4d7b2438d3 | 169 | const uint8_t *getPointer(void) const { |
switches | 0:5c4d7b2438d3 | 170 | return valueBytes; |
switches | 0:5c4d7b2438d3 | 171 | } |
switches | 0:5c4d7b2438d3 | 172 | |
switches | 0:5c4d7b2438d3 | 173 | unsigned getNumValueBytes(void) const { |
switches | 0:5c4d7b2438d3 | 174 | return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t)); |
switches | 0:5c4d7b2438d3 | 175 | } |
switches | 0:5c4d7b2438d3 | 176 | |
switches | 0:5c4d7b2438d3 | 177 | private: |
switches | 0:5c4d7b2438d3 | 178 | /* First byte: 8-bit values, no extra info. Second byte: uint8_t HRM value */ |
switches | 0:5c4d7b2438d3 | 179 | /* See https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */ |
switches | 0:5c4d7b2438d3 | 180 | uint8_t valueBytes[MAX_VALUE_BYTES]; |
switches | 0:5c4d7b2438d3 | 181 | }; |
switches | 0:5c4d7b2438d3 | 182 | |
switches | 0:5c4d7b2438d3 | 183 | protected: |
switches | 0:5c4d7b2438d3 | 184 | BLE &ble; |
switches | 0:5c4d7b2438d3 | 185 | |
switches | 0:5c4d7b2438d3 | 186 | HeartRateValueBytes valueBytes; |
switches | 0:5c4d7b2438d3 | 187 | uint8_t controlPointValue; |
switches | 0:5c4d7b2438d3 | 188 | |
switches | 0:5c4d7b2438d3 | 189 | GattCharacteristic hrmRate; |
switches | 0:5c4d7b2438d3 | 190 | ReadOnlyGattCharacteristic<uint8_t> hrmLocation; |
switches | 0:5c4d7b2438d3 | 191 | WriteOnlyGattCharacteristic<uint8_t> controlPoint; |
switches | 0:5c4d7b2438d3 | 192 | }; |
switches | 0:5c4d7b2438d3 | 193 | |
switches | 0:5c4d7b2438d3 | 194 | #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/ |