Heart Rate Monitor example for the BLE API using nRF51822 native mode drivers

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
ankuraga
Date:
Wed Sep 02 06:14:39 2015 +0000
Revision:
73:82447ab9efd8
Parent:
71:ecf479422c04
Simulating heart rate and temperature.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ankuraga 71:ecf479422c04 1 #ifndef PULSE_SENSOR_H
ankuraga 71:ecf479422c04 2 #define PULSE_SENSOR_H
ankuraga 71:ecf479422c04 3
ankuraga 71:ecf479422c04 4 #include "mbed.h"
ankuraga 71:ecf479422c04 5
ankuraga 71:ecf479422c04 6
ankuraga 71:ecf479422c04 7 /**
ankuraga 71:ecf479422c04 8 * Class for interfacing with a http://pulsesensor.myshopify.com/ 'Pulse Sensor Amped'.
ankuraga 71:ecf479422c04 9 * The contents of this class are based on the "Pulse Sensor Amped 1.1" Arduino Sketch.
ankuraga 71:ecf479422c04 10 *
ankuraga 71:ecf479422c04 11 * When using this class for the first time, it is recommended that you use the Processing
ankuraga 71:ecf479422c04 12 * GUI app available http://pulsesensor.myshopify.com/pages/code-and-guide. Using this, you
ankuraga 71:ecf479422c04 13 * will easily be able to verify the operating of your sensor, and the integration of this
ankuraga 71:ecf479422c04 14 * class into your mbed project.
ankuraga 71:ecf479422c04 15 */
ankuraga 71:ecf479422c04 16
ankuraga 71:ecf479422c04 17 class PulseSensor
ankuraga 71:ecf479422c04 18 {
ankuraga 71:ecf479422c04 19 private:
ankuraga 71:ecf479422c04 20 volatile int rate[10]; // used to hold last ten IBI values
ankuraga 71:ecf479422c04 21 volatile unsigned long sampleCounter; // used to determine pulse timing
ankuraga 71:ecf479422c04 22 volatile unsigned long lastBeatTime; // used to find the inter beat interval
ankuraga 71:ecf479422c04 23 volatile int P; // used to find peak in pulse wave
ankuraga 71:ecf479422c04 24 volatile int T; // used to find trough in pulse wave
ankuraga 71:ecf479422c04 25 volatile int thresh; // used to find instant moment of heart beat
ankuraga 71:ecf479422c04 26 volatile int amp; // used to hold amplitude of pulse waveform
ankuraga 71:ecf479422c04 27 volatile bool firstBeat; // used to seed rate array so we startup with reasonable BPM
ankuraga 71:ecf479422c04 28 volatile bool secondBeat; // used to seed rate array so we startup with reasonable BPM
ankuraga 71:ecf479422c04 29
ankuraga 71:ecf479422c04 30 // these variables are volatile because they are used during the interrupt service routine!
ankuraga 71:ecf479422c04 31 volatile int BPM; // used to hold the pulse rate
ankuraga 71:ecf479422c04 32 volatile int Signal; // holds the incoming raw data
ankuraga 71:ecf479422c04 33 volatile int IBI; // holds the time between beats, the Inter-Beat Interval
ankuraga 71:ecf479422c04 34 volatile bool Pulse; // true when pulse wave is high, false when it's low
ankuraga 71:ecf479422c04 35 volatile bool QS; // becomes true when a beat is found
ankuraga 71:ecf479422c04 36
ankuraga 71:ecf479422c04 37
ankuraga 71:ecf479422c04 38 void (*_printDataCallback)(int);
ankuraga 71:ecf479422c04 39 static const int _sensorTickRateMs = 2;
ankuraga 71:ecf479422c04 40 int _callbackRateMs;
ankuraga 71:ecf479422c04 41 bool _started;
ankuraga 71:ecf479422c04 42
ankuraga 71:ecf479422c04 43 AnalogIn *_pAin;
ankuraga 71:ecf479422c04 44 Ticker _pulseSensorTicker;
ankuraga 71:ecf479422c04 45 Ticker _processDataTicker;
ankuraga 71:ecf479422c04 46
ankuraga 71:ecf479422c04 47 void sensor_ticker_callback(void);
ankuraga 71:ecf479422c04 48 void process_data_ticker_callback(void);
ankuraga 71:ecf479422c04 49
ankuraga 71:ecf479422c04 50 public:
ankuraga 71:ecf479422c04 51 /** PulseSensor Constructor - Note this does not start the reading of the sensor.
ankuraga 71:ecf479422c04 52 * @param analogPin Name of the analog pin that the sensor is connected to.
ankuraga 71:ecf479422c04 53 * @param printDataCallback Pointer to function which will be called to print the latest data. Output format available here: http://pulsesensor.myshopify.com/pages/code-and-guide
ankuraga 71:ecf479422c04 54 * @param callbackRateMs Rate at which the printDataCallback is to be called, recommended is 20ms for graphing of pulse signal.
ankuraga 71:ecf479422c04 55 */
ankuraga 71:ecf479422c04 56 PulseSensor(PinName analogPin, void (*printDataCallback)(int), int callbackRateMs=20);
ankuraga 71:ecf479422c04 57
ankuraga 71:ecf479422c04 58 /** Destructor */
ankuraga 71:ecf479422c04 59 ~PulseSensor();
ankuraga 71:ecf479422c04 60
ankuraga 71:ecf479422c04 61 /** Start reading the Pulse Sensor, and sending current readings to the print data callback.
ankuraga 71:ecf479422c04 62 * @returns true if reading of the sensor is started, false if reading was aleady in progress.
ankuraga 71:ecf479422c04 63 */
ankuraga 71:ecf479422c04 64 bool start();
ankuraga 71:ecf479422c04 65
ankuraga 71:ecf479422c04 66 /** Stops the current reading of the Pulse Senson.
ankuraga 71:ecf479422c04 67 * @return true if reading is stopped, false if reading was already stopped.
ankuraga 71:ecf479422c04 68 */
ankuraga 71:ecf479422c04 69 bool stop();
ankuraga 71:ecf479422c04 70
ankuraga 71:ecf479422c04 71 };
ankuraga 71:ecf479422c04 72
ankuraga 71:ecf479422c04 73 #endif