Chanel Richardson / Mbed OS vitalsplus-max86150_chanel

Dependencies:   max32630fthr USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PPGService.h Source File

PPGService.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_PPG_SERVICE_H__
00018 #define MBED_BLE_PPG_SERVICE_H__
00019 
00020 #include "ble/BLE.h"
00021 
00022 //#if BLE_FEATURE_GATT_SERVER
00023 
00024 /**
00025  * BLE Heart Rate Service.
00026  *
00027  * @par purpose
00028  *
00029  * Fitness applications use the heart rate service to expose the heart
00030  * beat per minute measured by a heart rate sensor.
00031  *
00032  * Clients can read the intended location of the sensor and the last heart rate
00033  * value measured. Additionally, clients can subscribe to server initiated
00034  * updates of the heart rate value measured by the sensor. The service delivers
00035  * these updates to the subscribed client in a notification packet.
00036  *
00037  * The subscription mechanism is useful to save power; it avoids unecessary data
00038  * traffic between the client and the server, which may be induced by polling the
00039  * value of the heart rate measurement characteristic.
00040  *
00041  * @par usage
00042  *
00043  * When this class is instantiated, it adds a heart rate service in the GattServer.
00044  * The service contains the location of the sensor and the initial value measured
00045  * by the sensor.
00046  *
00047  * Application code can invoke updateHeartRate() when a new heart rate measurement
00048  * is acquired; this function updates the value of the heart rate measurement
00049  * characteristic and notifies the new value to subscribed clients.
00050  *
00051  * @note You can find specification of the heart rate service here:
00052  * https://www.bluetooth.com/specifications/gatt
00053  *
00054  * @attention The service does not expose information related to the sensor
00055  * contact, the accumulated energy expanded or the interbeat intervals.
00056  *
00057  * @attention The heart rate profile limits the number of instantiations of the
00058  * heart rate services to one.
00059  */
00060  extern Serial pc;
00061  const char* UUID_STR_PPG_SERVICE  = "E14C6C9D-3497-4835-8F8B-28D7AF2E6A15";
00062  const char* UUID_STR_PPG_RED_CHAR  = "E14C6C9D-3497-4836-8F8B-28D7AF2E6A15";
00063  const char* UUID_STR_PPG_IR_CHAR  = "E14C6C9D-3497-4837-8F8B-28D7AF2E6A15";
00064 
00065  
00066 class PPGService {
00067 
00068 public:
00069     /**
00070      * Construct and initialize a heart rate service.
00071      *
00072      * The construction process adds a GATT heart rate service in @p _ble
00073      * GattServer, sets the value of the heart rate measurement characteristic
00074      * to @p ppgVal and the value of the body sensor location characteristic
00075      * to @p location.
00076      *
00077      * @param[in] _ble BLE device that hosts the heart rate service.
00078      * @param[in] ppgVal Heart beats per minute measured by the heart rate
00079      * sensor.
00080      * @param[in] location Intended location of the heart rate sensor.
00081      */
00082     PPGService(BLE &_ble, int16_t ppgValRed, int16_t ppgValIR) :
00083         ble(_ble),
00084         valueBytesRed(ppgValRed),
00085         valueBytesIR(ppgValIR),
00086         ppgValueRed(
00087             UUID_STR_PPG_RED_CHAR, //TODO: replace with UUID for PPG sample characteristic
00088             valueBytesRed.getPointer(),
00089             valueBytesRed.getNumValueBytes(),
00090             PPGValueBytes::MAX_VALUE_BYTES,
00091             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00092         ppgValueIR(
00093             UUID_STR_PPG_IR_CHAR, //TODO: replace with UUID for PPG sample characteristic
00094             valueBytesIR.getPointer(),
00095             valueBytesIR.getNumValueBytes(),
00096             PPGValueBytes::MAX_VALUE_BYTES,
00097             GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00098     {
00099         //pc.printf("Setup service");
00100         setupService();
00101     }
00102 
00103     /**
00104      * Update the heart rate that the service exposes.
00105      *
00106      * The server sends a notification of the new value to clients that have
00107      * subscribed to updates of the heart rate measurement characteristic; clients
00108      * reading the heart rate measurement characteristic after the update obtain
00109      * the updated value.
00110      *
00111      * @param[in] ppgVal Heart rate measured in BPM.
00112      *
00113      * @attention This function must be called in the execution context of the
00114      * BLE stack.
00115      */
00116     void updatePPGRedValue(uint16_t ppgVal) {
00117         valueBytesRed.updatePPGValue(ppgVal);
00118         ble.gattServer().write(
00119             ppgValueRed.getValueHandle(),
00120             valueBytesRed.getPointer(),
00121             valueBytesRed.getNumValueBytes()
00122         );
00123     }
00124     void updatePPGIRValue(uint16_t ppgVal) {
00125         valueBytesIR.updatePPGValue(ppgVal);
00126         ble.gattServer().write(
00127             ppgValueIR.getValueHandle(),
00128             valueBytesIR.getPointer(),
00129             valueBytesIR.getNumValueBytes()
00130         );
00131     }
00132 
00133 protected:
00134     /**
00135      * Construct and add to the GattServer the heart rate service.
00136      */
00137     void setupService(void) {
00138         GattCharacteristic *charTable[] = {
00139             &ppgValueRed,
00140             &ppgValueIR
00141         };
00142         GattService ppgService(
00143             UUID_STR_PPG_SERVICE, //TODO: ECG service UUID
00144             charTable,
00145             sizeof(charTable) / sizeof(GattCharacteristic*)
00146         );
00147 
00148         ble.gattServer().addService(ppgService);
00149     }
00150 
00151 protected:
00152     /*
00153      * Heart rate measurement value.
00154      */
00155     struct PPGValueBytes {
00156         /* 1 byte for the Flags, and up to two bytes for heart rate value. */
00157         static const unsigned MAX_VALUE_BYTES = 2; // int16 per ECG sample
00158         static const unsigned FLAGS_BYTE_INDEX = 0;
00159 
00160         static const unsigned VALUE_FORMAT_BITNUM = 0;
00161         static const uint8_t  VALUE_FORMAT_FLAG = (1 << VALUE_FORMAT_BITNUM);
00162 
00163         PPGValueBytes(int16_t ppgVal) : valueBytes()
00164         {
00165             updatePPGValue(ppgVal);
00166         }
00167 
00168         void updatePPGValue(int16_t ppgVal)
00169         {
00170             *valueBytes = ppgVal;
00171             
00172         }
00173 
00174         uint8_t *getPointer(void)
00175         {
00176             return valueBytes;
00177         }
00178 
00179         const uint8_t *getPointer(void) const
00180         {
00181             return valueBytes;
00182         }
00183 
00184         unsigned getNumValueBytes(void) const
00185         {   
00186             /*
00187             if (valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) {
00188                 return 1 + sizeof(uint16_t);
00189             } else {
00190                 return 1 + sizeof(uint8_t);
00191             }
00192             */
00193             return sizeof(int16_t); // sending two bytes per ECG sample
00194         }
00195 
00196     private:
00197         uint8_t valueBytes[MAX_VALUE_BYTES];
00198     };
00199 
00200 protected:
00201     BLE &ble;
00202     PPGValueBytes valueBytesRed;
00203     PPGValueBytes valueBytesIR;
00204     GattCharacteristic ppgValueRed;
00205     GattCharacteristic ppgValueIR;
00206 };
00207 
00208 //#endif // BLE_FEATURE_GATT_SERVER
00209 
00210 #endif /* #ifndef MBED_BLE_HEART_RATE_SERVICE_H__*/