DFRPH: 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:09:28 +02:00 committed by Mihai Tudor Panu
parent 1c0ed9c9bd
commit 8a2ef9a648
3 changed files with 74 additions and 1 deletions

View File

@ -24,6 +24,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "dfrph.h"
@ -60,7 +61,11 @@ dfrph_context dfrph_init(int16_t pin)
void dfrph_close(dfrph_context dev)
{
assert(dev != NULL);
if(dev->aio)
mraa_aio_close(dev->aio);
free(dev);
}

View File

@ -26,6 +26,7 @@
#include <stdexcept>
#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<std::string> 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);

View File

@ -25,8 +25,10 @@
#include <iostream>
#include <string>
#include <vector>
#include "dfrph.h"
#include <mraa/initio.hpp>
#include <interfaces/iPH.hpp>
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;
};
}