diff --git a/src/dfrph/dfrph.c b/src/dfrph/dfrph.c index 6aac1ecd..8b3ead50 100644 --- a/src/dfrph/dfrph.c +++ b/src/dfrph/dfrph.c @@ -24,6 +24,7 @@ #include #include +#include #include "dfrph.h" @@ -60,7 +61,11 @@ dfrph_context dfrph_init(int16_t pin) void dfrph_close(dfrph_context dev) { - mraa_aio_close(dev->aio); + assert(dev != NULL); + + if(dev->aio) + mraa_aio_close(dev->aio); + free(dev); } diff --git a/src/dfrph/dfrph.cxx b/src/dfrph/dfrph.cxx index 88cd1126..b75dc92e 100644 --- a/src/dfrph/dfrph.cxx +++ b/src/dfrph/dfrph.cxx @@ -26,6 +26,7 @@ #include #include "dfrph.hpp" +#include "upm_string_parser.hpp" using namespace upm; @@ -36,6 +37,63 @@ DFRPH::DFRPH(int pin, float vref) : _dev(dfrph_init(pin)) ": dfrph_init() failed, invalid pin?"); } +DFRPH::DFRPH(std::string initStr) : mraaIo(initStr) +{ + mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); + std::vector upmTokens; + + if(!mraaIo.getLeftoverStr().empty()) { + upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); + } + + _dev = (dfrph_context) malloc(sizeof(struct _dfrph_context)); + + if(!_dev) + throw std::runtime_error(std::string(__FUNCTION__) + + ": dfrph_init() failed"); + + // make sure MRAA is initialized + int mraa_rv; + if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) + { + dfrph_close(_dev); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_init() failed"); + } + + if(!descs->aios) + { + dfrph_close(_dev); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_aio_init() failed"); + } + else + { + if( !(_dev->aio = descs->aios[0]) ) + { + dfrph_close(_dev); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_aio_init() failed"); + } + } + + /* Set the ref, offset, and scale */ + _dev->m_aref = 5.0; + _dev->m_offset = 0.0; + _dev->m_scale = 1.0; + + for (std::string tok : upmTokens) { + if(tok.substr(0,10) == "setOffset:") { + float offset = std::stof(tok.substr(10)); + setOffset(offset); + } + if(tok.substr(0,9) == "setScale:") { + float scale = std::stof(tok.substr(9)); + setScale(scale); + } + } +} + DFRPH::~DFRPH() { dfrph_close(_dev); diff --git a/src/dfrph/dfrph.hpp b/src/dfrph/dfrph.hpp index d6658d51..181fdd40 100644 --- a/src/dfrph/dfrph.hpp +++ b/src/dfrph/dfrph.hpp @@ -25,8 +25,10 @@ #include #include +#include #include "dfrph.h" +#include namespace upm { /** @@ -99,6 +101,13 @@ namespace upm { */ DFRPH(int pin, float vref = 5.0); + /** + * Instantiates DFRPH object based on a given string. + * + * @param initStr string containing specific information for DFRPH initialization. + */ + DFRPH(std::string initStr); + /** * DFRPH destructor */ @@ -139,6 +148,7 @@ namespace upm { DFRPH &operator=(const DFRPH &) {return *this;} dfrph_context _dev; + mraa::MraaIo mraaIo; }; }