ADC121C021: Add string based constructor

Signed-off-by: Adelin Dobre <adelin.dobre1994@gmail.com>
This commit is contained in:
Adelin Dobre 2018-07-04 15:12:41 +03:00 committed by Stefan Andritoiu
parent f34360c8ec
commit b2c46f602e
3 changed files with 92 additions and 9 deletions

View File

@ -26,6 +26,7 @@
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include "upm_string_parser.hpp"
#include "adc121c021.hpp" #include "adc121c021.hpp"
using namespace upm; using namespace upm;
@ -57,6 +58,80 @@ ADC121C021::ADC121C021(int bus, uint8_t address, float vref)
m_vref = 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() ADC121C021::~ADC121C021()
{ {
mraa_i2c_stop(m_i2c); mraa_i2c_stop(m_i2c);
@ -131,12 +206,12 @@ bool ADC121C021::getAlertStatus()
uint8_t astatus = readByte(ADC121C021_REG_ALERT_STATUS); uint8_t astatus = readByte(ADC121C021_REG_ALERT_STATUS);
if (astatus & 0x01) if (astatus & 0x01)
m_alertLow = true; m_alertLow = true;
else else
m_alertLow = false; m_alertLow = false;
if (astatus & 0x02) if (astatus & 0x02)
m_alertHigh = true; m_alertHigh = true;
else else
m_alertHigh = false; m_alertHigh = false;
} }
@ -210,7 +285,7 @@ void ADC121C021::enableAlertPinPolarityHigh(bool enable)
void ADC121C021::setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime) void ADC121C021::setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime)
{ {
// first we // first we
// read the current config register, masking off the cycle time bits // read the current config register, masking off the cycle time bits
uint8_t val = readByte(ADC121C021_REG_CONFIG) & 0x1f; uint8_t val = readByte(ADC121C021_REG_CONFIG) & 0x1f;

View File

@ -25,6 +25,7 @@
#include <string> #include <string>
#include <mraa/i2c.h> #include <mraa/i2c.h>
#include <mraa/initio.hpp>
#define ADC121C021_I2C_BUS 0 #define ADC121C021_I2C_BUS 0
#define ADC121C021_DEFAULT_I2C_ADDR 0x55 #define ADC121C021_DEFAULT_I2C_ADDR 0x55
@ -100,6 +101,13 @@ namespace upm {
ADC121C021(int bus, uint8_t address = ADC121C021_DEFAULT_I2C_ADDR, ADC121C021(int bus, uint8_t address = ADC121C021_DEFAULT_I2C_ADDR,
float vref = ADC121C021_DEFAULT_VREF); 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 * ADC121C021 destructor
*/ */
@ -140,7 +148,7 @@ namespace upm {
uint16_t readWord(uint8_t reg); uint16_t readWord(uint8_t reg);
/** /**
* Reads the current value of conversion * Reads the current value of conversion
* *
* @return 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 * 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; }; bool alertLowTriggered() { return m_alertLow; };
/** /**
* Returns the current value of m_alertHigh. You must call * 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 * @return Current status of the alert high flag
*/ */
@ -232,7 +240,7 @@ namespace upm {
* Enables or disables the automatic conversion mode. When enabled, the * Enables or disables the automatic conversion mode. When enabled, the
* ADC samples and updates the conversion value independently. * ADC samples and updates the conversion value independently.
* It is disabled by default, so conversion is only done by * It is disabled by default, so conversion is only done by
* calling value(). * calling value().
* *
* @param cycleTime Sets the cycle time for automatic conversion * @param cycleTime Sets the cycle time for automatic conversion
*/ */
@ -304,6 +312,7 @@ namespace upm {
private: private:
mraa_i2c_context m_i2c; mraa_i2c_context m_i2c;
mraa::MraaIo mraaIo;
uint8_t m_addr; uint8_t m_addr;
float m_vref; float m_vref;
bool m_alertLow; bool m_alertLow;

View File

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