diff --git a/src/dfrorp/dfrorp.cxx b/src/dfrorp/dfrorp.cxx index ece3c65f..9a0c4d2b 100644 --- a/src/dfrorp/dfrorp.cxx +++ b/src/dfrorp/dfrorp.cxx @@ -26,6 +26,7 @@ #include #include "dfrorp.hpp" +#include "upm_string_parser.hpp" using namespace upm; using namespace std; @@ -38,6 +39,75 @@ DFRORP::DFRORP(int apin, float a_ref) : + ": dfrorp_init() failed"); } +DFRORP::DFRORP(std::string initStr) : mraaIo(initStr) +{ + mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); + std::vector upmTokens; + + if(!mraaIo.getLeftoverStr().empty()) { + upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); + } + + m_dfrorp = (dfrorp_context)malloc(sizeof(struct _dfrorp_context)); + + if (!m_dfrorp) + throw std::runtime_error(string(__FUNCTION__) + + ": dfrorp_init() failed"); + + memset((void *)m_dfrorp, 0, sizeof(struct _dfrorp_context)); + + m_dfrorp->aio = NULL; + + m_dfrorp->offset = 0.0; + m_dfrorp->scale = 1.0; + + // make sure MRAA is initialized + int mraa_rv; + if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) + { + dfrorp_close(m_dfrorp); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_init() failed"); + } + + if(!descs->aios) + { + dfrorp_close(m_dfrorp); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_aio_init() failed"); + } + else + { + if( !(m_dfrorp->aio = descs->aios[0]) ) + { + dfrorp_close(m_dfrorp); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_aio_init() failed"); + } + } + + // set our analog resolution + m_dfrorp->a_res = (float)(1 << mraa_aio_get_bit(m_dfrorp->aio)) - 1; + + for (std::string tok : upmTokens) { + if(tok.substr(0,6) == "a_ref:") + m_dfrorp->a_ref = std::stof(tok.substr(6)); + 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); + } + if(tok.substr(0,21) == "setCalibrationOffset:") { + float offset = std::stof(tok.substr(21)); + setCalibrationOffset(offset); + } + } + +} + DFRORP::~DFRORP() { dfrorp_close(m_dfrorp); diff --git a/src/dfrorp/dfrorp.hpp b/src/dfrorp/dfrorp.hpp index 7fd7d58d..00ea285d 100644 --- a/src/dfrorp/dfrorp.hpp +++ b/src/dfrorp/dfrorp.hpp @@ -25,12 +25,13 @@ #include #include - +#include #include #include #include #include "dfrorp.h" +#include #include namespace upm { @@ -82,6 +83,13 @@ namespace upm { */ DFRORP(int apin, float a_ref=5.0); + /** + * Instantiates DFRobot object based on a given string. + * + * @param initStr string containing specific information for DFRobot initialization. + */ + DFRORP(std::string initStr); + /** * DFRORP object destructor */ @@ -164,6 +172,7 @@ namespace upm { protected: // dfrorp device context dfrorp_context m_dfrorp; + mraa::MraaIo mraaIo; private: /* Disable implicit copy and assignment operators */