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:
Fri Sep 26 02:32:53 2014 +0000
Revision:
4:618117fe4b04
Parent:
0:35588fbd6d5c
Child:
6:40b1e50a1d15
Child:
9:2256cd8dca35
switched Serial to RawSerial

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 #include "GroveEarbudSensor.h"
ansond 0:35588fbd6d5c 22
ansond 0:35588fbd6d5c 23 // Console logging
ansond 0:35588fbd6d5c 24 #define LOG_CONSOLE(...) { if (this->m_pc != NULL) this->m_pc->printf(__VA_ARGS__); }
ansond 0:35588fbd6d5c 25
ansond 0:35588fbd6d5c 26 // Our instance
ansond 0:35588fbd6d5c 27 GroveEarbudSensor *_grove_earbud_sensor_instance = NULL;
ansond 0:35588fbd6d5c 28
ansond 0:35588fbd6d5c 29 // interrupt function
ansond 0:35588fbd6d5c 30 void __grove_earbud_sensor_interrupt() { if (_grove_earbud_sensor_instance != NULL) _grove_earbud_sensor_instance->interrupt(); }
ansond 0:35588fbd6d5c 31
ansond 0:35588fbd6d5c 32 // constructor
ansond 4:618117fe4b04 33 GroveEarbudSensor::GroveEarbudSensor(InterruptIn *rx,RawSerial *pc) {
ansond 0:35588fbd6d5c 34 _grove_earbud_sensor_instance = this;
ansond 0:35588fbd6d5c 35 this->m_rx = rx;
ansond 0:35588fbd6d5c 36 this->m_pc = pc;
ansond 0:35588fbd6d5c 37 this->m_sub = 0;
ansond 0:35588fbd6d5c 38 this->m_counter = 0;
ansond 0:35588fbd6d5c 39 this->m_data_effect = true;
ansond 0:35588fbd6d5c 40 this->m_cb_fn = NULL;
ansond 0:35588fbd6d5c 41 this->m_cb_data = NULL;
ansond 0:35588fbd6d5c 42 this->m_heartrate = HEARTRATE_OFF;
ansond 0:35588fbd6d5c 43 this->m_timer = new Timer();
ansond 0:35588fbd6d5c 44
ansond 0:35588fbd6d5c 45 // register the interrupt handler
ansond 0:35588fbd6d5c 46 if (this->m_rx != NULL) this->m_rx->rise(&__grove_earbud_sensor_interrupt);
ansond 0:35588fbd6d5c 47
ansond 0:35588fbd6d5c 48 // start the timer and initialize the summation array
ansond 0:35588fbd6d5c 49 if (this->m_timer != NULL) {
ansond 0:35588fbd6d5c 50 // start the timer
ansond 0:35588fbd6d5c 51 this->m_timer->start();
ansond 0:35588fbd6d5c 52
ansond 0:35588fbd6d5c 53 // initialize the summation array
ansond 0:35588fbd6d5c 54 this->initSummationArray();
ansond 0:35588fbd6d5c 55 }
ansond 0:35588fbd6d5c 56 }
ansond 0:35588fbd6d5c 57
ansond 0:35588fbd6d5c 58 // destructor
ansond 0:35588fbd6d5c 59 GroveEarbudSensor::~GroveEarbudSensor() {
ansond 0:35588fbd6d5c 60 if (this->m_timer != NULL) delete this->m_timer;
ansond 0:35588fbd6d5c 61 }
ansond 0:35588fbd6d5c 62
ansond 0:35588fbd6d5c 63 // initialize the summation array
ansond 0:35588fbd6d5c 64 void GroveEarbudSensor::initSummationArray(void) {
ansond 0:35588fbd6d5c 65 for(int i=0;i<(NUM_SLOTS-1);++i) this->m_temp[i]=0;
ansond 0:35588fbd6d5c 66 this->m_temp[NUM_SLOTS-1] = this->m_timer->read_ms();
ansond 0:35588fbd6d5c 67 }
ansond 0:35588fbd6d5c 68
ansond 0:35588fbd6d5c 69 // register callback
ansond 0:35588fbd6d5c 70 void GroveEarbudSensor::registerCallback(GroveEarbudSensorCallback *cb_fn,void *cb_data) {
ansond 0:35588fbd6d5c 71 this->m_cb_fn = cb_fn;
ansond 0:35588fbd6d5c 72 this->m_cb_data = cb_data;
ansond 0:35588fbd6d5c 73 }
ansond 0:35588fbd6d5c 74
ansond 0:35588fbd6d5c 75 // get the current heartrate
ansond 0:35588fbd6d5c 76 float GroveEarbudSensor::getHeartRate(void) { return this->m_heartrate; }
ansond 0:35588fbd6d5c 77
ansond 0:35588fbd6d5c 78 // summation method + internal callback to fire any registered callback fns
ansond 0:35588fbd6d5c 79 void GroveEarbudSensor::sumAndInvokeCallback(void) {
ansond 0:35588fbd6d5c 80 if(this->m_data_effect) {
ansond 0:35588fbd6d5c 81 // summation
ansond 0:35588fbd6d5c 82 int tmp = 60 * (NUM_SLOTS-1) * 1000;
ansond 0:35588fbd6d5c 83 this->m_heartrate = tmp/(this->m_temp[NUM_SLOTS-1]-this->m_temp[0]);
ansond 0:35588fbd6d5c 84
ansond 0:35588fbd6d5c 85 // DEBUG/Log
ansond 4:618117fe4b04 86 if (this->m_heartrate > 0) LOG_CONSOLE("heartrate: %d bpm\r\n",this->m_heartrate);
ansond 0:35588fbd6d5c 87
ansond 0:35588fbd6d5c 88 // invoke any callbacks we might have
ansond 0:35588fbd6d5c 89 if (this->m_cb_fn != NULL) {
ansond 0:35588fbd6d5c 90 // invoke the callback
ansond 0:35588fbd6d5c 91 LOG_CONSOLE("invoking callback with heartrate = %d bpm\r\n",this->m_heartrate);
ansond 0:35588fbd6d5c 92 (*this->m_cb_fn)(this->m_heartrate,this->m_cb_data);
ansond 0:35588fbd6d5c 93 }
ansond 0:35588fbd6d5c 94 }
ansond 0:35588fbd6d5c 95 this->m_data_effect = 1; //sign bit
ansond 0:35588fbd6d5c 96 }
ansond 0:35588fbd6d5c 97
ansond 0:35588fbd6d5c 98 // interrupt() method for earbud
ansond 0:35588fbd6d5c 99 void GroveEarbudSensor::interrupt() {
ansond 0:35588fbd6d5c 100 this->m_temp[this->m_counter] = this->m_timer->read_ms();
ansond 0:35588fbd6d5c 101 switch(this->m_counter) {
ansond 0:35588fbd6d5c 102 case 0:
ansond 0:35588fbd6d5c 103 this->m_sub=this->m_temp[this->m_counter]-this->m_temp[NUM_SLOTS-1];
ansond 0:35588fbd6d5c 104 break;
ansond 0:35588fbd6d5c 105 default:
ansond 0:35588fbd6d5c 106 this->m_sub=this->m_temp[this->m_counter]-this->m_temp[this->m_counter-1];
ansond 0:35588fbd6d5c 107 break;
ansond 0:35588fbd6d5c 108 }
ansond 0:35588fbd6d5c 109 if(this->m_sub > HEARTPULSE_DUTY) {
ansond 0:35588fbd6d5c 110 this->m_data_effect = 0; //sign bit
ansond 0:35588fbd6d5c 111 this->m_counter = 0;
ansond 0:35588fbd6d5c 112 LOG_CONSOLE("heartrate measure error. Restarting timer..\r\n");
ansond 0:35588fbd6d5c 113 this->initSummationArray();
ansond 0:35588fbd6d5c 114 this->m_timer->stop();
ansond 0:35588fbd6d5c 115 this->m_timer->start();
ansond 0:35588fbd6d5c 116 }
ansond 0:35588fbd6d5c 117 if (this->m_counter == (NUM_SLOTS-1) && this->m_data_effect) {
ansond 0:35588fbd6d5c 118 this->m_counter = 0;
ansond 0:35588fbd6d5c 119 this->sumAndInvokeCallback();
ansond 0:35588fbd6d5c 120 }
ansond 0:35588fbd6d5c 121 else if(this->m_counter != (NUM_SLOTS-1) && this->m_data_effect) {
ansond 0:35588fbd6d5c 122 this->m_counter++;
ansond 0:35588fbd6d5c 123 }
ansond 0:35588fbd6d5c 124 else {
ansond 0:35588fbd6d5c 125 this->m_counter = 0;
ansond 0:35588fbd6d5c 126 this->m_data_effect = 1;
ansond 0:35588fbd6d5c 127 }
ansond 0:35588fbd6d5c 128 }
ansond 0:35588fbd6d5c 129