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 Mar 19 04:04:59 2015 +0000
Revision:
12:8687d42d6798
Parent:
11:30d8d0a456f3
updates for k64f hrm

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 3:6d5410cdd47d 29 #define NUM_SLOTS 6 // 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 // Blinky
ansond 0:35588fbd6d5c 55 DigitalOut led(LED1);
ansond 0:35588fbd6d5c 56
ansond 0:35588fbd6d5c 57 // Our sensor as an InterruptIn
ansond 0:35588fbd6d5c 58 InterruptIn sensor(D0);
ansond 0:35588fbd6d5c 59
ansond 0:35588fbd6d5c 60 // Grove Earbud Sensor include
ansond 0:35588fbd6d5c 61 #include "GroveEarbudSensor.h"
ansond 0:35588fbd6d5c 62
ansond 0:35588fbd6d5c 63 // callback for receiving heartrate values
ansond 0:35588fbd6d5c 64 void heartrateCallback(float heartrate,void *data) {
sam_grove 8:06be613d8c3e 65 printf("Callback: heartrate = %.1f\r\n",heartrate);
ansond 0:35588fbd6d5c 66 }
ansond 0:35588fbd6d5c 67
ansond 0:35588fbd6d5c 68 int main()
ansond 0:35588fbd6d5c 69 {
ansond 0:35588fbd6d5c 70 // announce
sam_grove 8:06be613d8c3e 71 printf("Grove Earbud Sensor Example v1.0.0\r\n");
ansond 0:35588fbd6d5c 72
ansond 0:35588fbd6d5c 73 // allocate the earbud sensor
sam_grove 8:06be613d8c3e 74 printf("Allocating earbud sensor instance...\r\n");
ansond 0:35588fbd6d5c 75 GroveEarbudSensor earbud(&sensor);
ansond 0:35588fbd6d5c 76
ansond 0:35588fbd6d5c 77 // register our callback function
sam_grove 8:06be613d8c3e 78 printf("registering callback...\r\n");
ansond 0:35588fbd6d5c 79 earbud.registerCallback(heartrateCallback);
ansond 0:35588fbd6d5c 80
ansond 0:35588fbd6d5c 81 // begin main loop
sam_grove 8:06be613d8c3e 82 printf("Beginning main loop...\r\n");
ansond 0:35588fbd6d5c 83 while (true) {
ansond 0:35588fbd6d5c 84 // blink...
ansond 0:35588fbd6d5c 85 led = !led;
ansond 0:35588fbd6d5c 86 wait(0.5);
ansond 0:35588fbd6d5c 87
ansond 0:35588fbd6d5c 88 // we can also call directly
sam_grove 8:06be613d8c3e 89 //printf("Direct: heartrate = %.1f\r\n",earbud.getHeartRate());
ansond 0:35588fbd6d5c 90 }
ansond 0:35588fbd6d5c 91 }
ansond 1:ea14b019224f 92
ansond 0:35588fbd6d5c 93 * @endcode
ansond 1:ea14b019224f 94 *
ansond 1:ea14b019224f 95 */
ansond 0:35588fbd6d5c 96
ansond 0:35588fbd6d5c 97 class GroveEarbudSensor {
ansond 0:35588fbd6d5c 98 private:
ansond 4:618117fe4b04 99 RawSerial *m_pc;
ansond 0:35588fbd6d5c 100 InterruptIn *m_rx;
ansond 0:35588fbd6d5c 101 volatile unsigned long m_temp[NUM_SLOTS];
ansond 0:35588fbd6d5c 102 volatile unsigned long m_sub;
ansond 0:35588fbd6d5c 103 volatile unsigned char m_counter;
ansond 0:35588fbd6d5c 104 volatile bool m_data_effect;
ansond 0:35588fbd6d5c 105 float m_heartrate;
ansond 0:35588fbd6d5c 106 GroveEarbudSensorCallback *m_cb_fn;
ansond 0:35588fbd6d5c 107 void *m_cb_data;
ansond 0:35588fbd6d5c 108 Timer *m_timer;
ansond 11:30d8d0a456f3 109 bool m_internal_interrupt_instance;
ansond 0:35588fbd6d5c 110
ansond 0:35588fbd6d5c 111 public:
ansond 0:35588fbd6d5c 112 /**
ansond 0:35588fbd6d5c 113 Default constructor
ansond 2:d4df045fa503 114 @param rx input InterruptIn instance
ansond 5:56720730382d 115 @param pc input RawSerial instance for debugging (if NULL, no debugging output will occur in the library)
ansond 0:35588fbd6d5c 116 */
ansond 4:618117fe4b04 117 GroveEarbudSensor(InterruptIn *rx,RawSerial *pc = NULL);
ansond 0:35588fbd6d5c 118
ansond 0:35588fbd6d5c 119 /**
ansond 11:30d8d0a456f3 120 constructor for internalized InterruptIn usage
ansond 11:30d8d0a456f3 121 @param rx interrupt_pin InterruptIn pin name
ansond 11:30d8d0a456f3 122 @param pc input RawSerial instance for debugging (if NULL, no debugging output will occur in the library)
ansond 11:30d8d0a456f3 123 */
ansond 11:30d8d0a456f3 124 GroveEarbudSensor(PinName interrupt_pin,RawSerial *pc = NULL);
ansond 11:30d8d0a456f3 125
ansond 11:30d8d0a456f3 126 /**
ansond 0:35588fbd6d5c 127 Default destructor
ansond 0:35588fbd6d5c 128 */
ansond 0:35588fbd6d5c 129 virtual ~GroveEarbudSensor();
ansond 0:35588fbd6d5c 130
ansond 0:35588fbd6d5c 131 /**
ansond 0:35588fbd6d5c 132 registerCallback - Register callback function
ansond 0:35588fbd6d5c 133 @param cb_fn - callback function of type GroveEarbudSensorCallback
ansond 2:d4df045fa503 134 @param cb_data - optional callback data to provide upon callback invocation (default - NULL)
ansond 0:35588fbd6d5c 135 */
ansond 0:35588fbd6d5c 136 void registerCallback(GroveEarbudSensorCallback *cb_fn,void *cb_data = NULL);
ansond 0:35588fbd6d5c 137
ansond 0:35588fbd6d5c 138 /**
ansond 0:35588fbd6d5c 139 getHeartRate - get the last sampled heartrate
ansond 0:35588fbd6d5c 140 @return heartrate - the last calculated heartrate (may also be one of HEARTRATE_OFF, HEARTRATE_MIN, or HEARTRATE_MAX)
ansond 0:35588fbd6d5c 141 */
ansond 0:35588fbd6d5c 142 float getHeartRate(void);
ansond 0:35588fbd6d5c 143
ansond 0:35588fbd6d5c 144 /**
ansond 0:35588fbd6d5c 145 interrupt() - interrupt handler for our instance - not normally invoked manually
ansond 0:35588fbd6d5c 146 */
ansond 0:35588fbd6d5c 147 void interrupt(void);
ansond 0:35588fbd6d5c 148
ansond 0:35588fbd6d5c 149 protected:
ansond 0:35588fbd6d5c 150 void initSummationArray(void);
ansond 0:35588fbd6d5c 151 void sumAndInvokeCallback(void);
ansond 0:35588fbd6d5c 152 };
ansond 0:35588fbd6d5c 153
ansond 0:35588fbd6d5c 154 #endif // _GROVE_EARBUD_SENSOR_H_
ansond 0:35588fbd6d5c 155