Library for the Grove Earbud Heartrate Sensor
Dependents: BLE_Police_HRM_Earbud df-2014-salesforce-hrm-k64f BLE_HeartRate_ppm emoSound ... more
GroveEarbudSensor.h
00001 /* Copyright C2014 ARM, MIT License 00002 * 00003 * Author: Doug Anson (doug.anson@arm.com) 00004 * 00005 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00006 * and associated documentation files the "Software", to deal in the Software without restriction, 00007 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00008 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in all copies or 00012 * substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00015 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00016 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00017 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00018 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00019 */ 00020 00021 #ifndef _GROVE_EARBUD_SENSOR_H_ 00022 #define _GROVE_EARBUD_SENSOR_H_ 00023 00024 00025 #include "mbed.h" 00026 00027 // ********* BEGIN Tunables ***************** 00028 00029 #define NUM_SLOTS 6 // set higher for greater accuracy (but slower callback frequency).. 00030 #define HEARTPULSE_DUTY 2000 // Change to follow your system's request. System returns error if the duty overtrips by 2 seconds. (in MS) 00031 00032 #define HEARTRATE_OFF 0 // earbud sensor is offline 00033 #define HEARTRATE_MIN 10 // min heartrate 00034 #define HEARTRATE_MAX 250 // max heartrate 00035 00036 // ********* END Tunables ***************** 00037 00038 // Callback function signature 00039 typedef void (GroveEarbudSensorCallback)(float,void *); 00040 00041 /** 00042 * GroveEarbudSensor 00043 * GroveEarbudSensor a simple API to receive heartrate telemetry from the Grove Earbud Sensor 00044 * 00045 * Based upon/Credit: http://www.seeedstudio.com/wiki/Grove_-_Ear-clip_Heart_Rate_Sensor 00046 * 00047 * Example Project: http://mbed.org/users/ansond/code/grove-earbud-sensor-sample/ 00048 * 00049 * @code 00050 * 00051 00052 #include "mbed.h" 00053 00054 // Blinky 00055 DigitalOut led(LED1); 00056 00057 // Our sensor as an InterruptIn 00058 InterruptIn sensor(D0); 00059 00060 // Grove Earbud Sensor include 00061 #include "GroveEarbudSensor.h" 00062 00063 // callback for receiving heartrate values 00064 void heartrateCallback(float heartrate,void *data) { 00065 printf("Callback: heartrate = %.1f\r\n",heartrate); 00066 } 00067 00068 int main() 00069 { 00070 // announce 00071 printf("Grove Earbud Sensor Example v1.0.0\r\n"); 00072 00073 // allocate the earbud sensor 00074 printf("Allocating earbud sensor instance...\r\n"); 00075 GroveEarbudSensor earbud(&sensor); 00076 00077 // register our callback function 00078 printf("registering callback...\r\n"); 00079 earbud.registerCallback(heartrateCallback); 00080 00081 // begin main loop 00082 printf("Beginning main loop...\r\n"); 00083 while (true) { 00084 // blink... 00085 led = !led; 00086 wait(0.5); 00087 00088 // we can also call directly 00089 //printf("Direct: heartrate = %.1f\r\n",earbud.getHeartRate()); 00090 } 00091 } 00092 00093 * @endcode 00094 * 00095 */ 00096 00097 class GroveEarbudSensor { 00098 private: 00099 RawSerial *m_pc; 00100 InterruptIn *m_rx; 00101 volatile unsigned long m_temp[NUM_SLOTS]; 00102 volatile unsigned long m_sub; 00103 volatile unsigned char m_counter; 00104 volatile bool m_data_effect; 00105 float m_heartrate; 00106 GroveEarbudSensorCallback *m_cb_fn; 00107 void *m_cb_data; 00108 Timer *m_timer; 00109 bool m_internal_interrupt_instance; 00110 00111 public: 00112 /** 00113 Default constructor 00114 @param rx input InterruptIn instance 00115 @param pc input RawSerial instance for debugging (if NULL, no debugging output will occur in the library) 00116 */ 00117 GroveEarbudSensor(InterruptIn *rx,RawSerial *pc = NULL); 00118 00119 /** 00120 constructor for internalized InterruptIn usage 00121 @param rx interrupt_pin InterruptIn pin name 00122 @param pc input RawSerial instance for debugging (if NULL, no debugging output will occur in the library) 00123 */ 00124 GroveEarbudSensor(PinName interrupt_pin,RawSerial *pc = NULL); 00125 00126 /** 00127 Default destructor 00128 */ 00129 virtual ~GroveEarbudSensor(); 00130 00131 /** 00132 registerCallback - Register callback function 00133 @param cb_fn - callback function of type GroveEarbudSensorCallback 00134 @param cb_data - optional callback data to provide upon callback invocation (default - NULL) 00135 */ 00136 void registerCallback(GroveEarbudSensorCallback *cb_fn,void *cb_data = NULL); 00137 00138 /** 00139 getHeartRate - get the last sampled heartrate 00140 @return heartrate - the last calculated heartrate (may also be one of HEARTRATE_OFF, HEARTRATE_MIN, or HEARTRATE_MAX) 00141 */ 00142 float getHeartRate(void); 00143 00144 /** 00145 interrupt() - interrupt handler for our instance - not normally invoked manually 00146 */ 00147 void interrupt(void); 00148 00149 protected: 00150 void initSummationArray(void); 00151 void sumAndInvokeCallback(void); 00152 }; 00153 00154 #endif // _GROVE_EARBUD_SENSOR_H_ 00155
Generated on Fri Jul 15 2022 13:36:17 by 1.7.2