diff --git a/src/cjq4435/cjq4435.cxx b/src/cjq4435/cjq4435.cxx index 0e04e726..0a22c415 100644 --- a/src/cjq4435/cjq4435.cxx +++ b/src/cjq4435/cjq4435.cxx @@ -27,6 +27,7 @@ #include #include "cjq4435.hpp" +#include "upm_string_parser.hpp" using namespace upm; using namespace std; @@ -39,6 +40,73 @@ CJQ4435::CJQ4435(int pin) : ": cjq4435_init() failed"); } +CJQ4435::CJQ4435(std::string initStr) : mraaIo(initStr) +{ + mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); + std::vector 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_close(m_cjq4435); diff --git a/src/cjq4435/cjq4435.hpp b/src/cjq4435/cjq4435.hpp index 0b7cf5e3..a40071d5 100644 --- a/src/cjq4435/cjq4435.hpp +++ b/src/cjq4435/cjq4435.hpp @@ -23,6 +23,7 @@ */ #pragma once +#include "mraa/initio.hpp" #include "cjq4435.h" namespace upm { @@ -63,6 +64,13 @@ namespace upm { */ 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 */ @@ -118,6 +126,7 @@ namespace upm { protected: cjq4435_context m_cjq4435; + mraa::MraaIo mraaIo; private: /* Disable implicit copy and assignment operators */