DFRORP: Add string based constructor

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Adelin Dobre 2018-10-31 13:07:23 +02:00 committed by Mihai Tudor Panu
parent fe3dcc01b2
commit 1c0ed9c9bd
2 changed files with 80 additions and 1 deletions

View File

@ -26,6 +26,7 @@
#include <stdexcept> #include <stdexcept>
#include "dfrorp.hpp" #include "dfrorp.hpp"
#include "upm_string_parser.hpp"
using namespace upm; using namespace upm;
using namespace std; using namespace std;
@ -38,6 +39,75 @@ DFRORP::DFRORP(int apin, float a_ref) :
+ ": dfrorp_init() failed"); + ": dfrorp_init() failed");
} }
DFRORP::DFRORP(std::string initStr) : mraaIo(initStr)
{
mraa_io_descriptor* descs = mraaIo.getMraaDescriptors();
std::vector<std::string> 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::~DFRORP()
{ {
dfrorp_close(m_dfrorp); dfrorp_close(m_dfrorp);

View File

@ -25,12 +25,13 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <vector>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include "dfrorp.h" #include "dfrorp.h"
#include <mraa/initio.hpp>
#include <interfaces/iOrp.hpp> #include <interfaces/iOrp.hpp>
namespace upm { namespace upm {
@ -82,6 +83,13 @@ namespace upm {
*/ */
DFRORP(int apin, float a_ref=5.0); 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 * DFRORP object destructor
*/ */
@ -164,6 +172,7 @@ namespace upm {
protected: protected:
// dfrorp device context // dfrorp device context
dfrorp_context m_dfrorp; dfrorp_context m_dfrorp;
mraa::MraaIo mraaIo;
private: private:
/* Disable implicit copy and assignment operators */ /* Disable implicit copy and assignment operators */