mirror of
https://github.com/eclipse/upm.git
synced 2025-07-26 21:51:16 +03:00
Fix some issues for string based constructors
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:

committed by
Mihai Tudor Panu

parent
8f11061de3
commit
2975bae075
@ -58,7 +58,7 @@ ADS1015::ADS1015(int bus, uint8_t address, float vref) : ADS1X15(bus, address) {
|
||||
|
||||
ADS1015::ADS1015(std::string initStr) : ADS1X15(initStr)
|
||||
{
|
||||
float vref;
|
||||
float vref = ADS1015_VREF;
|
||||
m_name = "ADS1015";
|
||||
m_conversionDelay = ADS1015_CONVERSIONDELAY;
|
||||
m_bitShift = 4;
|
||||
@ -67,14 +67,11 @@ ADS1015::ADS1015(std::string initStr) : ADS1X15(initStr)
|
||||
std::string leftoverString = ADS1X15::getLeftoverStr();
|
||||
std::vector<std::string> upmTokens;
|
||||
|
||||
if(!leftoverString.empty())
|
||||
{
|
||||
upmTokens = UpmStringParser::parse(mraaIo->getLeftoverStr());
|
||||
if(!leftoverString.empty()) {
|
||||
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
|
||||
}
|
||||
for (std::string tok : upmTokens)
|
||||
{
|
||||
if(tok.substr(0,5) == "vref:")
|
||||
{
|
||||
for (std::string tok : upmTokens) {
|
||||
if(tok.substr(0, 5) == "vref:") {
|
||||
vref = std::stof(tok.substr(5));
|
||||
}
|
||||
}
|
||||
|
@ -32,19 +32,24 @@
|
||||
|
||||
using namespace upm;
|
||||
|
||||
ADS1X15::ADS1X15(int bus, uint8_t address){
|
||||
static bool operator!(mraa::MraaIo &mraaIo)
|
||||
{
|
||||
return mraaIo.getMraaDescriptors() == NULL;
|
||||
}
|
||||
|
||||
if(!(i2c = new mraa::I2c(bus))){
|
||||
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->address(address) != mraa::SUCCESS)){
|
||||
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){
|
||||
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.
|
||||
@ -54,24 +59,16 @@ ADS1X15::ADS1X15(int bus, uint8_t address){
|
||||
|
||||
}
|
||||
|
||||
ADS1X15::ADS1X15(std::string initStr)
|
||||
ADS1X15::ADS1X15(std::string initStr) : mraaIo(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(!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){
|
||||
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.
|
||||
@ -81,40 +78,41 @@ ADS1X15::ADS1X15(std::string initStr)
|
||||
|
||||
std::vector<std::string> upmTokens;
|
||||
|
||||
if(!mraaIo->getLeftoverStr().empty()) {
|
||||
upmTokens = UpmStringParser::parse(mraaIo->getLeftoverStr());
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
if(tok.substr(0, 14) == "setContinuous:") {
|
||||
bool mode = std::stoi(tok.substr(14), nullptr, 0);
|
||||
setContinuous(mode);
|
||||
}
|
||||
if(tok.substr(0,21) == "updateConfigRegister:") {
|
||||
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);
|
||||
bool read = std::stoi(tok.substr(sz+1), nullptr, 0);
|
||||
updateConfigRegister(update, read);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ADS1X15::~ADS1X15(){
|
||||
delete mraaIo;
|
||||
ADS1X15::~ADS1X15() {
|
||||
if(!mraaIo)
|
||||
delete i2c;
|
||||
}
|
||||
|
||||
float
|
||||
@ -250,7 +248,7 @@ ADS1X15::swapWord(uint16_t value){
|
||||
|
||||
std::string
|
||||
ADS1X15::getLeftoverStr(){
|
||||
return mraaIo->getLeftoverStr();
|
||||
return mraaIo.getLeftoverStr();
|
||||
}
|
||||
|
||||
|
||||
|
@ -432,7 +432,7 @@ namespace upm {
|
||||
void updateConfigRegister(uint16_t update, bool read = false);
|
||||
uint16_t swapWord(uint16_t value);
|
||||
|
||||
mraa::MraaIo* mraaIo = NULL;
|
||||
mraa::MraaIo mraaIo;
|
||||
mraa::I2c* i2c;
|
||||
|
||||
};}
|
||||
|
Reference in New Issue
Block a user