1 /* 2 * Author: Jon Trulson <jtrulson (at) ics.com> 3 * Copyright (c) 2014 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 #include <iostream> 26 #include <string> 27 #include <stdexcept> 28 29 #include "groveehr.h" 30 31 using namespace upm; 32 using namespace std; 33 34 GroveEHR::GroveEHR(int pin) 35 { 36 if ( !(m_gpio = mraa_gpio_init(pin)) ) 37 { 38 throw std::invalid_argument(std::string(__FUNCTION__) + 39 ": mraa_aio_init() failed, invalid pin?"); 40 return; 41 } 42 43 mraa_gpio_dir(m_gpio, MRAA_GPIO_IN); 44 45 initClock(); 46 m_beatCounter = 0; 47 } 48 49 GroveEHR::~GroveEHR() 50 { 51 mraa_gpio_close(m_gpio); 52 } 53 54 void GroveEHR::initClock() 55 { 56 gettimeofday(&m_startTime, NULL); 57 } 58 59 uint32_t GroveEHR::getMillis() 60 { 61 struct timeval elapsed, now; 62 uint32_t elapse; 63 64 // get current time 65 gettimeofday(&now, NULL); 66 67 // compute the delta since m_startTime 68 if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 ) 69 { 70 elapsed.tv_usec += 1000000; 71 elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1; 72 } 73 else 74 { 75 elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec; 76 } 77 78 elapse = (uint32_t)((elapsed.tv_sec * 1000) + (elapsed.tv_usec / 1000)); 79 80 // never return 0 81 if (elapse == 0) 82 elapse = 1; 83 84 return elapse; 85 } 86 87 void GroveEHR::clearBeatCounter() 88 { 89 m_beatCounter = 0; 90 } 91 92 void GroveEHR::startBeatCounter() 93 { 94 // install our interrupt handler 95 mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING, 96 &beatISR, this); 97 } 98 99 void GroveEHR::stopBeatCounter() 100 { 101 // remove the interrupt handler 102 mraa_gpio_isr_exit(m_gpio); 103 } 104 105 uint32_t GroveEHR::beatCounter() 106 { 107 return m_beatCounter; 108 } 109 110 void GroveEHR::beatISR(void *ctx) 111 { 112 upm::GroveEHR *This = (upm::GroveEHR *)ctx; 113 This->m_beatCounter++; 114 } 115 116 int GroveEHR::heartRate() 117 { 118 uint32_t millis = getMillis(); 119 uint32_t beats = beatCounter(); 120 121 float heartRate = 0; 122 // wait at least 5 seconds before attempting to compute the 123 // heart rate 124 if (millis > 5000) 125 { 126 heartRate = (float(beats) / (float(millis) / 1000.0)) * 60.0; 127 } 128 129 return int(heartRate); 130 } 131