DFRORP: Add string based constructor

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
Adelin Dobre 2018-10-31 13:07:23 +02:00 committed by Stefan Andritoiu
parent ff655c4eff
commit 7ee242b668
2 changed files with 81 additions and 1 deletions

View File

@ -26,6 +26,7 @@
#include <stdexcept>
#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<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_close(m_dfrorp);

View File

@ -25,12 +25,14 @@
#include <string>
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "dfrorp.h"
#include <mraa/initio.hpp>
namespace upm {
/**
@ -81,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
*/
@ -155,6 +164,7 @@ namespace upm {
protected:
// dfrorp device context
dfrorp_context m_dfrorp;
mraa::MraaIo mraaIo;
private:
/* Disable implicit copy and assignment operators */