CJQ4435 MOSFET: Add string based constructor

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
Adelin Dobre 2018-07-24 16:44:44 +03:00 committed by Stefan Andritoiu
parent 3acbb3d3d7
commit 854a2b6bd8
2 changed files with 77 additions and 0 deletions

View File

@ -27,6 +27,7 @@
#include <stdexcept> #include <stdexcept>
#include "cjq4435.hpp" #include "cjq4435.hpp"
#include "upm_string_parser.hpp"
using namespace upm; using namespace upm;
using namespace std; using namespace std;
@ -39,6 +40,73 @@ CJQ4435::CJQ4435(int pin) :
": cjq4435_init() failed"); ": cjq4435_init() failed");
} }
CJQ4435::CJQ4435(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());
}
// make sure MRAA is initialized
int mraa_rv;
if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
{
printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
throw std::runtime_error(std::string(__FUNCTION__) +
": cjq4435_init() failed");
}
m_cjq4435 = (cjq4435_context)malloc(sizeof(struct _cjq4435_context));
if(!m_cjq4435) {
throw std::runtime_error(std::string(__FUNCTION__) +
": cjq4435_init() failed");
}
// zero out context
memset((void *)m_cjq4435, 0, sizeof(struct _cjq4435_context));
if(!descs->pwms) {
printf("%s: mraa_pwm_init() failed.\n", __FUNCTION__);
cjq4435_close(m_cjq4435);
throw std::runtime_error(std::string(__FUNCTION__) +
": cjq4435_init() failed");
} else {
if( !(m_cjq4435->pwm = descs->pwms[0]) ) {
printf("%s: mraa_pwm_init() failed.\n", __FUNCTION__);
cjq4435_close(m_cjq4435);
throw std::runtime_error(std::string(__FUNCTION__) +
": cjq4435_init() failed");
}
}
m_cjq4435->enabled = false;
for(std::string tok : upmTokens) {
if(tok.substr(0,12) == "setPeriodUS:") {
int us = std::stoi(tok.substr(0,12),nullptr,0);
setPeriodUS(us);
}
if(tok.substr(0,12) == "setPeriodMS:") {
int ms = std::stoi(tok.substr(0,12),nullptr,0);
setPeriodMS(ms);
}
if(tok.substr(0,17) == "setPeriodSeconds:") {
float seconds = std::stof(tok.substr(0,17));
setPeriodSeconds(seconds);
}
if(tok.substr(0,7) == "enable:") {
bool en = std::stoi(tok.substr(0,7),nullptr,0);
enable(en);
}
if(tok.substr(0,13) == "setDutyCycle:") {
float dutyCycle = std::stof(tok.substr(0,13));
setDutyCycle(dutyCycle);
}
}
}
CJQ4435::~CJQ4435() CJQ4435::~CJQ4435()
{ {
cjq4435_close(m_cjq4435); cjq4435_close(m_cjq4435);

View File

@ -23,6 +23,7 @@
*/ */
#pragma once #pragma once
#include "mraa/initio.hpp"
#include "cjq4435.h" #include "cjq4435.h"
namespace upm { namespace upm {
@ -63,6 +64,13 @@ namespace upm {
*/ */
CJQ4435(int pin); CJQ4435(int pin);
/**
* Instantiates CJQ4435 MOSFET based on a given string.
*
* @param initStr string containing specific information for CJQ4435 MOSFET.
*/
CJQ4435(std::string initStr);
/** /**
* CJQ4435 destructor * CJQ4435 destructor
*/ */
@ -118,6 +126,7 @@ namespace upm {
protected: protected:
cjq4435_context m_cjq4435; cjq4435_context m_cjq4435;
mraa::MraaIo mraaIo;
private: private:
/* Disable implicit copy and assignment operators */ /* Disable implicit copy and assignment operators */