Library for the Grove Earbud Heartrate Sensor

Dependents:   BLE_Police_HRM_Earbud df-2014-salesforce-hrm-k64f BLE_HeartRate_ppm emoSound ... more

Committer:
ansond
Date:
Thu Sep 25 21:40:36 2014 +0000
Revision:
2:d4df045fa503
Parent:
1:ea14b019224f
Child:
3:6d5410cdd47d
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:35588fbd6d5c 1 /* Copyright C2014 ARM, MIT License
ansond 0:35588fbd6d5c 2 *
ansond 0:35588fbd6d5c 3 * Author: Doug Anson (doug.anson@arm.com)
ansond 0:35588fbd6d5c 4 *
ansond 0:35588fbd6d5c 5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:35588fbd6d5c 6 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:35588fbd6d5c 7 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:35588fbd6d5c 8 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:35588fbd6d5c 9 * furnished to do so, subject to the following conditions:
ansond 0:35588fbd6d5c 10 *
ansond 0:35588fbd6d5c 11 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:35588fbd6d5c 12 * substantial portions of the Software.
ansond 0:35588fbd6d5c 13 *
ansond 0:35588fbd6d5c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:35588fbd6d5c 15 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:35588fbd6d5c 16 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:35588fbd6d5c 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:35588fbd6d5c 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:35588fbd6d5c 19 */
ansond 0:35588fbd6d5c 20
ansond 0:35588fbd6d5c 21 #ifndef _GROVE_EARBUD_SENSOR_H_
ansond 0:35588fbd6d5c 22 #define _GROVE_EARBUD_SENSOR_H_
ansond 0:35588fbd6d5c 23
ansond 0:35588fbd6d5c 24
ansond 0:35588fbd6d5c 25 #include "mbed.h"
ansond 0:35588fbd6d5c 26
ansond 0:35588fbd6d5c 27 // ********* BEGIN Tunables *****************
ansond 0:35588fbd6d5c 28
ansond 0:35588fbd6d5c 29 #define NUM_SLOTS 11 // set higher for greater accuracy (but slower callback frequency)..
ansond 0:35588fbd6d5c 30 #define HEARTPULSE_DUTY 2000 // Change to follow your system's request. System returns error if the duty overtrips by 2 seconds. (in MS)
ansond 0:35588fbd6d5c 31
ansond 0:35588fbd6d5c 32 #define HEARTRATE_OFF 0 // earbud sensor is offline
ansond 0:35588fbd6d5c 33 #define HEARTRATE_MIN 10 // min heartrate
ansond 0:35588fbd6d5c 34 #define HEARTRATE_MAX 250 // max heartrate
ansond 0:35588fbd6d5c 35
ansond 0:35588fbd6d5c 36 // ********* END Tunables *****************
ansond 0:35588fbd6d5c 37
ansond 0:35588fbd6d5c 38 // Callback function signature
ansond 0:35588fbd6d5c 39 typedef void (GroveEarbudSensorCallback)(float,void *);
ansond 0:35588fbd6d5c 40
ansond 0:35588fbd6d5c 41 /**
ansond 0:35588fbd6d5c 42 * GroveEarbudSensor
ansond 0:35588fbd6d5c 43 * GroveEarbudSensor a simple API to receive heartrate telemetry from the Grove Earbud Sensor
ansond 0:35588fbd6d5c 44 *
ansond 0:35588fbd6d5c 45 * Based upon/Credit: http://www.seeedstudio.com/wiki/Grove_-_Ear-clip_Heart_Rate_Sensor
ansond 0:35588fbd6d5c 46 *
ansond 0:35588fbd6d5c 47 * Example Project: http://mbed.org/users/ansond/code/grove-earbud-sensor-sample/
ansond 0:35588fbd6d5c 48 *
ansond 0:35588fbd6d5c 49 * @code
ansond 1:ea14b019224f 50 *
ansond 0:35588fbd6d5c 51
ansond 0:35588fbd6d5c 52 #include "mbed.h"
ansond 0:35588fbd6d5c 53
ansond 0:35588fbd6d5c 54 // Console
ansond 0:35588fbd6d5c 55 Serial pc(USBTX,USBRX);
ansond 0:35588fbd6d5c 56
ansond 0:35588fbd6d5c 57 // Blinky
ansond 0:35588fbd6d5c 58 DigitalOut led(LED1);
ansond 0:35588fbd6d5c 59
ansond 0:35588fbd6d5c 60 // Our sensor as an InterruptIn
ansond 0:35588fbd6d5c 61 InterruptIn sensor(D0);
ansond 0:35588fbd6d5c 62
ansond 0:35588fbd6d5c 63 // Grove Earbud Sensor include
ansond 0:35588fbd6d5c 64 #include "GroveEarbudSensor.h"
ansond 0:35588fbd6d5c 65
ansond 0:35588fbd6d5c 66 // callback for receiving heartrate values
ansond 0:35588fbd6d5c 67 void heartrateCallback(float heartrate,void *data) {
ansond 0:35588fbd6d5c 68 pc.printf("Callback: heartrate = %.1f\r\n",heartrate);
ansond 0:35588fbd6d5c 69 }
ansond 0:35588fbd6d5c 70
ansond 0:35588fbd6d5c 71 int main()
ansond 0:35588fbd6d5c 72 {
ansond 0:35588fbd6d5c 73 // announce
ansond 0:35588fbd6d5c 74 pc.printf("Grove Earbud Sensor Example v1.0.0\r\n");
ansond 0:35588fbd6d5c 75
ansond 0:35588fbd6d5c 76 // allocate the earbud sensor
ansond 0:35588fbd6d5c 77 pc.printf("Allocating earbud sensor instance...\r\n");
ansond 0:35588fbd6d5c 78 GroveEarbudSensor earbud(&sensor);
ansond 0:35588fbd6d5c 79
ansond 0:35588fbd6d5c 80 // register our callback function
ansond 0:35588fbd6d5c 81 pc.printf("registering callback...\r\n");
ansond 0:35588fbd6d5c 82 earbud.registerCallback(heartrateCallback);
ansond 0:35588fbd6d5c 83
ansond 0:35588fbd6d5c 84 // begin main loop
ansond 0:35588fbd6d5c 85 pc.printf("Beginning main loop...\r\n");
ansond 0:35588fbd6d5c 86 while (true) {
ansond 0:35588fbd6d5c 87 // blink...
ansond 0:35588fbd6d5c 88 led = !led;
ansond 0:35588fbd6d5c 89 wait(0.5);
ansond 0:35588fbd6d5c 90
ansond 0:35588fbd6d5c 91 // we can also call directly
ansond 0:35588fbd6d5c 92 //pc.printf("Direct: heartrate = %.1f\r\n",earbud.getHeartRate());
ansond 0:35588fbd6d5c 93 }
ansond 0:35588fbd6d5c 94 }
ansond 1:ea14b019224f 95
ansond 0:35588fbd6d5c 96 * @endcode
ansond 1:ea14b019224f 97 *
ansond 1:ea14b019224f 98 */
ansond 0:35588fbd6d5c 99
ansond 0:35588fbd6d5c 100 class GroveEarbudSensor {
ansond 0:35588fbd6d5c 101 private:
ansond 0:35588fbd6d5c 102 Serial *m_pc;
ansond 0:35588fbd6d5c 103 InterruptIn *m_rx;
ansond 0:35588fbd6d5c 104 volatile unsigned long m_temp[NUM_SLOTS];
ansond 0:35588fbd6d5c 105 volatile unsigned long m_sub;
ansond 0:35588fbd6d5c 106 volatile unsigned char m_counter;
ansond 0:35588fbd6d5c 107 volatile bool m_data_effect;
ansond 0:35588fbd6d5c 108 float m_heartrate;
ansond 0:35588fbd6d5c 109 GroveEarbudSensorCallback *m_cb_fn;
ansond 0:35588fbd6d5c 110 void *m_cb_data;
ansond 0:35588fbd6d5c 111 Timer *m_timer;
ansond 0:35588fbd6d5c 112
ansond 0:35588fbd6d5c 113 public:
ansond 0:35588fbd6d5c 114 /**
ansond 0:35588fbd6d5c 115 Default constructor
ansond 2:d4df045fa503 116 @param rx input InterruptIn instance
ansond 2:d4df045fa503 117 @param pc input Serial instance for debugging (if NULL, no debugging output will occur in the library)
ansond 0:35588fbd6d5c 118 */
ansond 0:35588fbd6d5c 119 GroveEarbudSensor(InterruptIn *rx,Serial *pc = NULL);
ansond 0:35588fbd6d5c 120
ansond 0:35588fbd6d5c 121 /**
ansond 0:35588fbd6d5c 122 Default destructor
ansond 0:35588fbd6d5c 123 */
ansond 0:35588fbd6d5c 124 virtual ~GroveEarbudSensor();
ansond 0:35588fbd6d5c 125
ansond 0:35588fbd6d5c 126 /**
ansond 0:35588fbd6d5c 127 registerCallback - Register callback function
ansond 0:35588fbd6d5c 128 @param cb_fn - callback function of type GroveEarbudSensorCallback
ansond 2:d4df045fa503 129 @param cb_data - optional callback data to provide upon callback invocation (default - NULL)
ansond 0:35588fbd6d5c 130 */
ansond 0:35588fbd6d5c 131 void registerCallback(GroveEarbudSensorCallback *cb_fn,void *cb_data = NULL);
ansond 0:35588fbd6d5c 132
ansond 0:35588fbd6d5c 133 /**
ansond 0:35588fbd6d5c 134 getHeartRate - get the last sampled heartrate
ansond 0:35588fbd6d5c 135 @return heartrate - the last calculated heartrate (may also be one of HEARTRATE_OFF, HEARTRATE_MIN, or HEARTRATE_MAX)
ansond 0:35588fbd6d5c 136 */
ansond 0:35588fbd6d5c 137 float getHeartRate(void);
ansond 0:35588fbd6d5c 138
ansond 0:35588fbd6d5c 139 /**
ansond 0:35588fbd6d5c 140 interrupt() - interrupt handler for our instance - not normally invoked manually
ansond 0:35588fbd6d5c 141 */
ansond 0:35588fbd6d5c 142 void interrupt(void);
ansond 0:35588fbd6d5c 143
ansond 0:35588fbd6d5c 144 protected:
ansond 0:35588fbd6d5c 145 void initSummationArray(void);
ansond 0:35588fbd6d5c 146 void sumAndInvokeCallback(void);
ansond 0:35588fbd6d5c 147 };
ansond 0:35588fbd6d5c 148
ansond 0:35588fbd6d5c 149 #endif // _GROVE_EARBUD_SENSOR_H_
ansond 0:35588fbd6d5c 150