diff --git a/src/adc121c021/adc121c021.cxx b/src/adc121c021/adc121c021.cxx index cea682ba..c3b73dc6 100644 --- a/src/adc121c021/adc121c021.cxx +++ b/src/adc121c021/adc121c021.cxx @@ -26,6 +26,7 @@ #include #include +#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 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); @@ -131,12 +206,12 @@ bool ADC121C021::getAlertStatus() uint8_t astatus = readByte(ADC121C021_REG_ALERT_STATUS); if (astatus & 0x01) m_alertLow = true; - else + else m_alertLow = false; if (astatus & 0x02) m_alertHigh = true; - else + else m_alertHigh = false; } @@ -210,7 +285,7 @@ void ADC121C021::enableAlertPinPolarityHigh(bool enable) void ADC121C021::setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime) { - // first we + // first we // read the current config register, masking off the cycle time bits uint8_t val = readByte(ADC121C021_REG_CONFIG) & 0x1f; diff --git a/src/adc121c021/adc121c021.hpp b/src/adc121c021/adc121c021.hpp index 7a8ff6fe..1edb0cc5 100644 --- a/src/adc121c021/adc121c021.hpp +++ b/src/adc121c021/adc121c021.hpp @@ -25,6 +25,7 @@ #include #include +#include #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 */ @@ -140,7 +148,7 @@ namespace upm { uint16_t readWord(uint8_t reg); /** - * Reads the current value of conversion + * Reads the current value of conversion * * @return Current value of conversion */ @@ -166,15 +174,15 @@ namespace upm { /** * Returns the current value of m_alertLow. You must call - * getAlertStatus() to update this value. + * getAlertStatus() to update this value. * - * @return Current status of the alert low flag + * @return Current status of the alert low flag */ bool alertLowTriggered() { return m_alertLow; }; /** * Returns the current value of m_alertHigh. You must call - * getAlertStatus() to update this value. + * getAlertStatus() to update this value. * * @return Current status of the alert high flag */ @@ -232,7 +240,7 @@ namespace upm { * Enables or disables the automatic conversion mode. When enabled, the * ADC samples and updates the conversion value independently. * It is disabled by default, so conversion is only done by - * calling value(). + * calling value(). * * @param cycleTime Sets the cycle time for automatic conversion */ @@ -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; diff --git a/src/ldt0028/ldt0028.hpp b/src/ldt0028/ldt0028.hpp index 3e9d58a3..ac10a350 100644 --- a/src/ldt0028/ldt0028.hpp +++ b/src/ldt0028/ldt0028.hpp @@ -26,7 +26,6 @@ #include #include #include -#include namespace upm {