EHR: Add string based constructor

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
Adelin Dobre 2018-11-01 18:47:39 +02:00 committed by Stefan Andritoiu
parent 4d90e3009d
commit 0e4560d021
2 changed files with 34 additions and 8 deletions

View File

@ -46,6 +46,23 @@ EHR::EHR(int pin)
m_beatCounter = 0; m_beatCounter = 0;
} }
EHR::EHR(std::string initStr) : mraaIo(initStr)
{
mraa_io_descriptor* descs = mraaIo.getMraaDescriptors();
if(!descs->gpios) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_gpio_init() failed, invalid pin?");
} else {
m_gpio = descs->gpios[0];
}
mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
initClock();
m_beatCounter = 0;
}
EHR::~EHR() EHR::~EHR()
{ {
mraa_gpio_close(m_gpio); mraa_gpio_close(m_gpio);
@ -65,12 +82,12 @@ uint32_t EHR::getMillis()
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
// compute the delta since m_startTime // compute the delta since m_startTime
if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 ) if( (elapsed.tv_usec = now.tv_usec - m_startTime.tv_usec) < 0 )
{ {
elapsed.tv_usec += 1000000; elapsed.tv_usec += 1000000;
elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1; elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec - 1;
} }
else else
{ {
elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec; elapsed.tv_sec = now.tv_sec - m_startTime.tv_sec;
} }
@ -92,7 +109,7 @@ void EHR::clearBeatCounter()
void EHR::startBeatCounter() void EHR::startBeatCounter()
{ {
// install our interrupt handler // install our interrupt handler
mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING, mraa_gpio_isr(m_gpio, MRAA_GPIO_EDGE_RISING,
&beatISR, this); &beatISR, this);
} }
@ -117,7 +134,7 @@ int EHR::heartRate()
{ {
uint32_t millis = getMillis(); uint32_t millis = getMillis();
uint32_t beats = beatCounter(); uint32_t beats = beatCounter();
float heartRate = 0; float heartRate = 0;
// wait at least 5 seconds before attempting to compute the // wait at least 5 seconds before attempting to compute the
// heart rate // heart rate

View File

@ -27,6 +27,7 @@
#include <stdint.h> #include <stdint.h>
#include <sys/time.h> #include <sys/time.h>
#include <mraa/gpio.h> #include <mraa/gpio.h>
#include <mraa/initio.hpp>
namespace upm { namespace upm {
/** /**
@ -46,9 +47,9 @@ namespace upm {
* @brief API for the Ear-clip Heart Rate Sensor * @brief API for the Ear-clip Heart Rate Sensor
* *
* UPM module for the ear-clip heart rate sensor. It is used to measure your * UPM module for the ear-clip heart rate sensor. It is used to measure your
* heart rate. * heart rate.
* *
* @image html ehr.jpg * @image html ehr.jpg
* @snippet ehr.cxx Interesting * @snippet ehr.cxx Interesting
*/ */
class EHR { class EHR {
@ -59,6 +60,13 @@ namespace upm {
* @param pin Digital pin to use * @param pin Digital pin to use
*/ */
EHR(int pin); EHR(int pin);
/**
* Instantiates EHR Heart Rate sensor based on a given string.
*
* @param initStr string containing specific information for EHR initialization.
*/
EHR(std::string initStr);
/** /**
* EHR destructor * EHR destructor
*/ */
@ -116,10 +124,11 @@ namespace upm {
* *
*/ */
static void beatISR(void *ctx); static void beatISR(void *ctx);
volatile uint32_t m_beatCounter; volatile uint32_t m_beatCounter;
struct timeval m_startTime; struct timeval m_startTime;
mraa_gpio_context m_gpio; mraa_gpio_context m_gpio;
mraa::MraaIo mraaIo;
}; };
} }