BISS0001: Add string based constructor

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
Adelin Dobre 2018-07-16 12:36:30 +03:00 committed by Stefan Andritoiu
parent 384f4a45ea
commit d3198ef0ca
2 changed files with 45 additions and 0 deletions

View File

@ -27,6 +27,7 @@
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include "upm_string_parser.hpp"
#include "biss0001.hpp" #include "biss0001.hpp"
using namespace upm; using namespace upm;
@ -39,6 +40,41 @@ BISS0001::BISS0001(unsigned int pin) :
": biss0001_init() failed"); ": biss0001_init() failed");
} }
BISS0001::BISS0001(std::string initStr) : mraaIo(initStr)
{
mraa_io_descriptor* descs = mraaIo.getMraaDescriptors();
m_biss0001 = (biss0001_context)malloc(sizeof(struct _biss0001_context));
if(!m_biss0001)
throw std::runtime_error(std::string(__FUNCTION__) +
": biss0001_init() failed");
m_biss0001->gpio = NULL;
// make sure MRAA is initialized
int mraa_rv;
if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) {
biss0001_close(m_biss0001);
throw std::runtime_error(std::string(__FUNCTION__) +
": biss0001_init() failed");
}
// initialize the MRAA context
if(!descs->gpios) {
throw std::runtime_error(std::string(__FUNCTION__) +
": mraa_gpio_init() failed");
}
else {
if( !(m_biss0001->gpio = descs->gpios[0]) ) {
biss0001_close(m_biss0001);
throw std::runtime_error(std::string(__FUNCTION__) +
": mraa_gpio_init() failed");
}
}
mraa_gpio_dir(m_biss0001->gpio, MRAA_GPIO_IN);
}
BISS0001::~BISS0001() BISS0001::~BISS0001()
{ {
biss0001_close(m_biss0001); biss0001_close(m_biss0001);

View File

@ -25,6 +25,7 @@
#pragma once #pragma once
#include <biss0001.h> #include <biss0001.h>
#include <mraa/initio.hpp>
namespace upm { namespace upm {
/** /**
@ -66,6 +67,13 @@ namespace upm {
*/ */
BISS0001(unsigned int pin); BISS0001(unsigned int pin);
/**
* Instantiates BISS0001 Motion Sensor object based on a given string.
*
* @param initStr string containing specific information for BISS0001 initialization.
*/
BISS0001(std::string initStr);
/** /**
* BISS0001 destructor * BISS0001 destructor
*/ */
@ -93,6 +101,7 @@ namespace upm {
BISS0001 &operator=(const BISS0001&) = delete; BISS0001 &operator=(const BISS0001&) = delete;
biss0001_context m_biss0001; biss0001_context m_biss0001;
mraa::MraaIo mraaIo;
}; };
} }