ADS1X15: Add string based constructor

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Adelin Dobre 2018-07-09 14:56:49 +03:00 committed by Mihai Tudor Panu
parent 82edcec766
commit 50961c6020
6 changed files with 161 additions and 16 deletions

View File

@ -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<std::string> 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*

View File

@ -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
*/

View File

@ -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

View File

@ -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
*/

View File

@ -25,6 +25,7 @@
#include "ads1x15.hpp"
#include "mraa/i2c.hpp"
#include "upm_string_parser.hpp"
#include <unistd.h>
#include <syslog.h>
@ -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<std::string> 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();
}

View File

@ -26,6 +26,7 @@
#include <iostream>
#include <string>
#include <mraa/initio.hpp>
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;
};}