From 50961c6020c2aa432f5eaccedbdcfb649c933c96 Mon Sep 17 00:00:00 2001 From: Adelin Dobre Date: Mon, 9 Jul 2018 14:56:49 +0300 Subject: [PATCH] ADS1X15: Add string based constructor Signed-off-by: Adelin Dobre Signed-off-by: Mihai Tudor Panu --- src/ads1x15/ads1015.cxx | 41 ++++++++++++++++ src/ads1x15/ads1015.hpp | 7 +++ src/ads1x15/ads1115.cxx | 8 ++++ src/ads1x15/ads1115.hpp | 7 +++ src/ads1x15/ads1x15.cxx | 100 +++++++++++++++++++++++++++++++++------- src/ads1x15/ads1x15.hpp | 14 ++++++ 6 files changed, 161 insertions(+), 16 deletions(-) diff --git a/src/ads1x15/ads1015.cxx b/src/ads1x15/ads1015.cxx index 40fefdae..5a478c97 100644 --- a/src/ads1x15/ads1015.cxx +++ b/src/ads1x15/ads1015.cxx @@ -26,6 +26,7 @@ #include "ads1015.hpp" #include "mraa/i2c.hpp" +#include "upm_string_parser.hpp" using namespace upm; @@ -55,6 +56,46 @@ ADS1015::ADS1015(int bus, uint8_t address, float vref) : ADS1X15(bus, address) { setGain(GAIN_SIXTEEN); } +ADS1015::ADS1015(std::string initStr) : ADS1X15(initStr) +{ + float vref; + m_name = "ADS1015"; + m_conversionDelay = ADS1015_CONVERSIONDELAY; + m_bitShift = 4; + ADS1X15::getCurrentConfig(); + + std::string leftoverString = ADS1X15::getLeftoverStr(); + std::vector upmTokens; + + if(!leftoverString.empty()) + { + upmTokens = UpmStringParser::parse(mraaIo->getLeftoverStr()); + } + for (std::string tok : upmTokens) + { + if(tok.substr(0,5) == "vref:") + { + vref = std::stof(tok.substr(5)); + } + } + + if (vref < 0.0 || vref > 6.144) + UPM_THROW("vref out of range"); + else if (vref > 4.096) + setGain(GAIN_TWOTHIRDS); + else if (vref > 2.048) + setGain(GAIN_ONE); + else if (vref > 1.024) + setGain(GAIN_TWO); + else if (vref > 0.512) + setGain(GAIN_FOUR); + else if (vref > 0.256) + setGain(GAIN_EIGHT); + else + setGain(GAIN_SIXTEEN); +} + + ADS1015::~ADS1015(){}; const char* diff --git a/src/ads1x15/ads1015.hpp b/src/ads1x15/ads1015.hpp index 66e52ab6..bb42cabe 100644 --- a/src/ads1x15/ads1015.hpp +++ b/src/ads1x15/ads1015.hpp @@ -126,6 +126,13 @@ namespace upm { */ ADS1015 (int bus, uint8_t address = 0x48, float vref = ADS1015_VREF); + /** + * Instantiates ADS1015 converter based on a given string. + * + * @param initStr string containing specific information for ADS1015 converter initialization. + */ + ADS1015(std::string initStr); + /** * ADS1X15 destructor */ diff --git a/src/ads1x15/ads1115.cxx b/src/ads1x15/ads1115.cxx index a26aba46..720c1405 100644 --- a/src/ads1x15/ads1115.cxx +++ b/src/ads1x15/ads1115.cxx @@ -22,6 +22,7 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "ads1115.hpp" +#include "upm_string_parser.hpp" using namespace upm; @@ -32,6 +33,13 @@ ADS1115::ADS1115(int bus, uint8_t address) : ADS1X15(bus, address) { ADS1X15::getCurrentConfig(); } +ADS1115::ADS1115(std::string initStr) : ADS1X15(initStr) { + m_name = "ADS1115"; + m_conversionDelay = ADS1115_CONVERSIONDELAY; + m_bitShift = 0; + ADS1X15::getCurrentConfig(); +} + ADS1115::~ADS1115(){}; void diff --git a/src/ads1x15/ads1115.hpp b/src/ads1x15/ads1115.hpp index d4329d83..8eacb699 100644 --- a/src/ads1x15/ads1115.hpp +++ b/src/ads1x15/ads1115.hpp @@ -123,6 +123,13 @@ namespace upm { */ ADS1115 (int bus, uint8_t address = 0x48); + /** + * Instantiates ADS1115 converter based on a given string. + * + * @param initStr string containing specific information for ADS1115 converter initialization. + */ + ADS1115(std::string initStr); + /** * ADS1X15 destructor */ diff --git a/src/ads1x15/ads1x15.cxx b/src/ads1x15/ads1x15.cxx index 47701b2a..94111860 100644 --- a/src/ads1x15/ads1x15.cxx +++ b/src/ads1x15/ads1x15.cxx @@ -25,6 +25,7 @@ #include "ads1x15.hpp" #include "mraa/i2c.hpp" +#include "upm_string_parser.hpp" #include #include @@ -33,26 +34,88 @@ using namespace upm; ADS1X15::ADS1X15(int bus, uint8_t address){ - if(!(i2c = new mraa::I2c(bus))){ - throw std::invalid_argument(std::string(__FUNCTION__) +": I2c.init() failed"); - return; - } + if(!(i2c = new mraa::I2c(bus))){ + throw std::invalid_argument(std::string(__FUNCTION__) +": I2c.init() failed"); + return; + } - if((i2c->address(address) != mraa::SUCCESS)){ - throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.address() failed"); - return; - } + if((i2c->address(address) != mraa::SUCCESS)){ + throw std::runtime_error(std::string(__FUNCTION__) + ": I2c.address() failed"); + return; + } + + if(i2c->frequency( mraa::I2C_FAST) != mraa::SUCCESS){ + syslog(LOG_WARNING, "%s: I2c.frequency(I2C_FAST) failed, using default speed", std::string(__FUNCTION__).c_str()); + } + //Will be reset by sub class. + m_bitShift = 0; + m_conversionDelay = .001; + m_config_reg = 0x0000; - if(i2c->frequency( mraa::I2C_FAST) != mraa::SUCCESS){ - syslog(LOG_WARNING, "%s: I2c.frequency(I2C_FAST) failed, using default speed", std::string(__FUNCTION__).c_str()); - } - //Will be reset by sub class. - m_bitShift = 0; - m_conversionDelay = .001; - m_config_reg = 0x0000; } -ADS1X15::~ADS1X15(){} +ADS1X15::ADS1X15(std::string initStr) +{ + mraaIo = new mraa::MraaIo(initStr); + if(mraaIo == NULL) + { + throw std::invalid_argument(std::string(__FUNCTION__) + ": Failed to allocate memory for internal member"); + } + + if(!mraaIo->i2cs.empty()) + { + i2c = &mraaIo->i2cs[0]; + } + else + { + throw std::invalid_argument(std::string(__FUNCTION__) +": I2c.init() failed"); + } + + if(i2c->frequency( mraa::I2C_FAST) != mraa::SUCCESS){ + syslog(LOG_WARNING, "%s: I2c.frequency(I2C_FAST) failed, using default speed", std::string(__FUNCTION__).c_str()); + } + //Will be reset by sub class. + m_bitShift = 0; + m_conversionDelay = .001; + m_config_reg = 0x0000; + + std::vector upmTokens; + + if(!mraaIo->getLeftoverStr().empty()) { + upmTokens = UpmStringParser::parse(mraaIo->getLeftoverStr()); + } + + std::string::size_type sz; + + for (std::string tok : upmTokens) { + if(tok.substr(0,12) == "setCompMode:") { + bool mode = std::stoi(tok.substr(12),nullptr,0); + setCompMode(mode); + } + if(tok.substr(0,11) == "setCompPol:") { + bool mode = std::stoi(tok.substr(11),nullptr,0); + setCompPol(mode); + } + if(tok.substr(0,13) == "setCompLatch:") { + bool mode = std::stoi(tok.substr(13),nullptr,0); + setCompLatch(mode); + } + if(tok.substr(0,14) == "setContinuous:") { + bool mode = std::stoi(tok.substr(14),nullptr,0); + setContinuous(mode); + } + if(tok.substr(0,21) == "updateConfigRegister:") { + uint16_t update = std::stoi(tok.substr(21),&sz,0); + tok = tok.substr(21); + bool read = std::stoi(tok.substr(sz+1),nullptr,0); + updateConfigRegister(update,read); + } + } +} + +ADS1X15::~ADS1X15(){ + delete mraaIo; +} float ADS1X15::getSample(ADSMUXMODE mode){ @@ -185,4 +248,9 @@ ADS1X15::swapWord(uint16_t value){ return ((res & 0xFF) << 8) | ((res >> 8 ) & 0xFF); } +std::string +ADS1X15::getLeftoverStr(){ + return mraaIo->getLeftoverStr(); +} + diff --git a/src/ads1x15/ads1x15.hpp b/src/ads1x15/ads1x15.hpp index 69f8412c..f65e38ef 100644 --- a/src/ads1x15/ads1x15.hpp +++ b/src/ads1x15/ads1x15.hpp @@ -26,6 +26,7 @@ #include #include +#include namespace mraa {class I2c;} @@ -216,6 +217,13 @@ namespace upm { */ ADS1X15(int bus, uint8_t address); + /** + * Instantiates ADS1X15 family controlers based on a given string. + * + * @param initStr string containing specific information for ADS1X15 family controlers initialization. + */ + ADS1X15(std::string initStr); + /** * ADS1X15 destructor */ @@ -406,6 +414,11 @@ namespace upm { */ void setThresh(ADSTHRESH reg = THRESH_DEFAULT , float value = 0.0); + /** + * Get upm parameters for converter initialization + */ + std::string getLeftoverStr(); + protected: std::string m_name; float m_conversionDelay; @@ -419,6 +432,7 @@ namespace upm { void updateConfigRegister(uint16_t update, bool read = false); uint16_t swapWord(uint16_t value); + mraa::MraaIo* mraaIo = NULL; mraa::I2c* i2c; };}