Cycle Fit - All Sensors

Dependencies:   FXOS8700Q mbed

Committer:
roberthill04
Date:
Wed Feb 24 16:53:45 2016 +0000
Revision:
0:ef02694deaa8
Hello

Who changed what in which revision?

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