ADC121C021: Add string based constructor

Signed-off-by: Adelin Dobre <adelin.dobre1994@gmail.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Adelin Dobre 2018-07-04 15:12:41 +03:00 committed by Mihai Tudor Panu
parent 550e48e806
commit aa87b14126
3 changed files with 92 additions and 9 deletions

View File

@ -26,6 +26,7 @@
#include <string>
#include <stdexcept>
#include "upm_string_parser.hpp"
#include "adc121c021.hpp"
using namespace upm;
@ -57,6 +58,80 @@ ADC121C021::ADC121C021(int bus, uint8_t address, float vref)
m_vref = vref;
}
ADC121C021::ADC121C021(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());
}
if(!descs->i2cs)
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_init() failed");
}
else
{
if( !(m_i2c = descs->i2cs[0]) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_i2c_init() failed");
}
}
std::string::size_type sz;
for (std::string tok : upmTokens) {
if(tok.substr(0, 5) == "vref:") {
m_vref = std::stof(tok.substr(5));
}
if(tok.substr(0,10) == "writeByte:") {
uint8_t reg = std::stoi(tok.substr(10),&sz,0);
tok = tok.substr(10);
uint8_t byte = std::stoi(tok.substr(sz+1),nullptr,0);
writeByte(reg, byte);
}
if(tok.substr(0,10) == "writeWord:") {
uint8_t reg = std::stoi(tok.substr(10),&sz,0);
tok = tok.substr(10);
uint16_t word = std::stoi(tok.substr(sz+1),nullptr,0);
writeWord(reg, word);
}
if(tok.substr(0,16) == "enableAlertFlag:") {
bool enable = std::stoi(tok.substr(16),nullptr,0);
enableAlertFlag(enable);
}
if(tok.substr(0,15) == "enableAlertPin:") {
bool enable = std::stoi(tok.substr(15),nullptr,0);
enableAlertPin(enable);
}
if(tok.substr(0,16) == "enableAlertHold:") {
bool enable = std::stoi(tok.substr(16),nullptr,0);
enableAlertHold(enable);
}
if(tok.substr(0,27) == "enableAlertPinPolarityHigh:") {
bool enable = std::stoi(tok.substr(27),nullptr,0);
enableAlertPinPolarityHigh(enable);
}
if(tok.substr(0,17) == "setAlertLowLimit:") {
uint16_t limit = std::stoi(tok.substr(17),nullptr,0);
setAlertLowLimit(limit);
}
if(tok.substr(0,18) == "setAlertHighLimit:") {
uint16_t limit = std::stoi(tok.substr(18),nullptr,0);
setAlertHighLimit(limit);
}
if(tok.substr(0,14) == "setHysteresis:") {
uint16_t limit = std::stoi(tok.substr(14),nullptr,0);
setHysteresis(limit);
}
}
}
ADC121C021::~ADC121C021()
{
mraa_i2c_stop(m_i2c);

View File

@ -25,6 +25,7 @@
#include <string>
#include <mraa/i2c.h>
#include <mraa/initio.hpp>
#define ADC121C021_I2C_BUS 0
#define ADC121C021_DEFAULT_I2C_ADDR 0x55
@ -100,6 +101,13 @@ namespace upm {
ADC121C021(int bus, uint8_t address = ADC121C021_DEFAULT_I2C_ADDR,
float vref = ADC121C021_DEFAULT_VREF);
/**
* Instantiates ADC121C021 Converter based on a given string.
*
* @param initStr string containing specific information for ADC121C021 initialization.
*/
ADC121C021(std::string initStr);
/**
* ADC121C021 destructor
*/
@ -304,6 +312,7 @@ namespace upm {
private:
mraa_i2c_context m_i2c;
mraa::MraaIo mraaIo;
uint8_t m_addr;
float m_vref;
bool m_alertLow;

View File

@ -26,7 +26,6 @@
#include <string>
#include <mraa/aio.h>
#include <mraa/initio.hpp>
#include <mraa/initio.h>
namespace upm {