From 8a2ef9a648f08fccc3f740a97cfd6b7223fe7679 Mon Sep 17 00:00:00 2001 From: Adelin Dobre Date: Wed, 31 Oct 2018 13:09:28 +0200 Subject: [PATCH] DFRPH: Add string based constructor Signed-off-by: Adelin Dobre Signed-off-by: Mihai Tudor Panu --- src/dfrph/dfrph.c | 7 +++++- src/dfrph/dfrph.cxx | 58 +++++++++++++++++++++++++++++++++++++++++++++ src/dfrph/dfrph.hpp | 10 ++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) 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 1a1f15ee..337c275e 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 ff0a0e4c..032f12ae 100644 --- a/src/dfrph/dfrph.hpp +++ b/src/dfrph/dfrph.hpp @@ -25,8 +25,10 @@ #include #include +#include #include "dfrph.h" +#include #include namespace upm { @@ -100,6 +102,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 */ @@ -147,5 +156,6 @@ namespace upm { DFRPH &operator=(const DFRPH &) {return *this;} dfrph_context _dev; + mraa::MraaIo mraaIo; }; }