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.
Dependencies: max32630fthr USBDevice
ECGService.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef MBED_BLE_ECG_SERVICE_H__ 00018 #define MBED_BLE_ECG_SERVICE_H__ 00019 00020 00021 #include "ble/BLE.h" 00022 #include "UUID.h" 00023 00024 //#if BLE_FEATURE_GATT_SERVER 00025 00026 /** 00027 * BLE Heart Rate Service. 00028 * 00029 * @par purpose 00030 * 00031 * Fitness applications use the heart rate service to expose the heart 00032 * beat per minute measured by a heart rate sensor. 00033 * 00034 * Clients can read the intended location of the sensor and the last heart rate 00035 * value measured. Additionally, clients can subscribe to server initiated 00036 * updates of the heart rate value measured by the sensor. The service delivers 00037 * these updates to the subscribed client in a notification packet. 00038 * 00039 * The subscription mechanism is useful to save power; it avoids unecessary data 00040 * traffic between the client and the server, which may be induced by polling the 00041 * value of the heart rate measurement characteristic. 00042 * 00043 * @par usage 00044 * 00045 * When this class is instantiated, it adds a heart rate service in the GattServer. 00046 * The service contains the location of the sensor and the initial value measured 00047 * by the sensor. 00048 * 00049 * Application code can invoke updateHeartRate() when a new heart rate measurement 00050 * is acquired; this function updates the value of the heart rate measurement 00051 * characteristic and notifies the new value to subscribed clients. 00052 * 00053 * @note You can find specification of the heart rate service here: 00054 * https://www.bluetooth.com/specifications/gatt 00055 * 00056 * @attention The service does not expose information related to the sensor 00057 * contact, the accumulated energy expanded or the interbeat intervals. 00058 * 00059 * @attention The heart rate profile limits the number of instantiations of the 00060 * heart rate services to one. 00061 */ 00062 extern Serial pc; 00063 const char* UUID_STR_ECG_SERVICE = "E51B251DB5BB47CD8F0B176D7004563C"; 00064 const char* UUID_STR_ECG_CHAR = "E51B251DB5BB47CE8F0B176D7004563C"; 00065 00066 class ECGService { 00067 00068 00069 00070 public: 00071 /** 00072 * Construct and initialize a heart rate service. 00073 * 00074 * The construction process adds a GATT heart rate service in @p _ble 00075 * GattServer, sets the value of the heart rate measurement characteristic 00076 * to @p hrmCounter and the value of the body sensor location characteristic 00077 * to @p location. 00078 * 00079 * @param[in] _ble BLE device that hosts the heart rate service. 00080 * @param[in] hrmCounter Heart beats per minute measured by the heart rate 00081 * sensor. 00082 * @param[in] location Intended location of the heart rate sensor. 00083 */ 00084 00085 00086 ECGService(BLE &_ble, int16_t hrmCounter) : 00087 ble(_ble), 00088 valueBytes(hrmCounter), 00089 ecgSample( 00090 UUID_STR_ECG_CHAR, //TODO: replace with UUID for ECG sample characteristic 00091 valueBytes.getPointer(), 00092 valueBytes.getNumValueBytes(), 00093 HeartRateValueBytes::MAX_VALUE_BYTES, 00094 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY 00095 ) 00096 { 00097 //pc.printf("Setup service"); 00098 setupService(); 00099 } 00100 00101 /** 00102 * Update the heart rate that the service exposes. 00103 * 00104 * The server sends a notification of the new value to clients that have 00105 * subscribed to updates of the heart rate measurement characteristic; clients 00106 * reading the heart rate measurement characteristic after the update obtain 00107 * the updated value. 00108 * 00109 * @param[in] hrmCounter Heart rate measured in BPM. 00110 * 00111 * @attention This function must be called in the execution context of the 00112 * BLE stack. 00113 */ 00114 void updateHeartRate(uint16_t hrmCounter) { 00115 valueBytes.updateHeartRate(hrmCounter); 00116 ble.gattServer().write( 00117 ecgSample.getValueHandle(), 00118 valueBytes.getPointer(), 00119 valueBytes.getNumValueBytes() 00120 ); 00121 } 00122 00123 protected: 00124 /** 00125 * Construct and add to the GattServer the heart rate service. 00126 */ 00127 void setupService(void) { 00128 //UUID(const char* stringUUID) 00129 00130 UUID UUID_ECG_SERVICE(UUID_STR_ECG_SERVICE) ; 00131 //UUID_ECG_SERVICE = UUID(UUID_STR); 00132 GattCharacteristic *charTable[] = { 00133 &ecgSample 00134 }; 00135 GattService hrmService( 00136 UUID_ECG_SERVICE, //TODO: ECG service UUID 00137 charTable, 00138 sizeof(charTable) / sizeof(GattCharacteristic*) 00139 ); 00140 00141 ble.gattServer().addService(hrmService); 00142 } 00143 00144 protected: 00145 /* 00146 * Heart rate measurement value. 00147 */ 00148 struct HeartRateValueBytes { 00149 /* 1 byte for the Flags, and up to two bytes for heart rate value. */ 00150 static const unsigned MAX_VALUE_BYTES = 2; // int16 per ECG sample 00151 static const unsigned FLAGS_BYTE_INDEX = 0; 00152 00153 static const unsigned VALUE_FORMAT_BITNUM = 0; 00154 static const uint8_t VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM); 00155 00156 HeartRateValueBytes(int16_t hrmCounter) : valueBytes() 00157 { 00158 updateHeartRate(hrmCounter); 00159 } 00160 00161 void updateHeartRate(int16_t hrmCounter) 00162 { 00163 /* 00164 if (hrmCounter <= 255) { 00165 valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG; 00166 valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter; 00167 } else { 00168 valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG; 00169 valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF); 00170 valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8); 00171 } 00172 */ 00173 *valueBytes = hrmCounter; 00174 00175 } 00176 00177 uint8_t *getPointer(void) 00178 { 00179 return valueBytes; 00180 } 00181 00182 const uint8_t *getPointer(void) const 00183 { 00184 return valueBytes; 00185 } 00186 00187 unsigned getNumValueBytes(void) const 00188 { 00189 /* 00190 if (valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) { 00191 return 1 + sizeof(uint16_t); 00192 } else { 00193 return 1 + sizeof(uint8_t); 00194 } 00195 */ 00196 return sizeof(int16_t); // sending two bytes per ECG sample 00197 } 00198 00199 private: 00200 uint8_t valueBytes[MAX_VALUE_BYTES]; 00201 00202 }; 00203 00204 protected: 00205 BLE &ble; 00206 HeartRateValueBytes valueBytes; 00207 GattCharacteristic ecgSample; 00208 }; 00209 00210 //#endif // BLE_FEATURE_GATT_SERVER 00211 00212 #endif /* #ifndef MBED_BLE_HEART_RATE_SERVICE_H__*/
Generated on Sat Jul 16 2022 12:45:18 by
1.7.2