BMX055/BMI055/BMC150: Add string based cons

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
Adelin Dobre 2018-07-31 18:43:16 +03:00 committed by Stefan Andritoiu
parent 252feac5c7
commit 555da249a2
6 changed files with 242 additions and 0 deletions

View File

@ -29,6 +29,7 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include "upm_string_parser.hpp"
#include "bmc150.hpp" #include "bmc150.hpp"
using namespace upm; using namespace upm;
@ -55,6 +56,71 @@ BMC150::BMC150(int accelBus, int accelAddr, int accelCS,
m_mag->init(); m_mag->init();
} }
BMC150::BMC150(std::string initStr)
{
int accelBus = BMC150_DEFAULT_BUS;
int accelAddr = BMC150_DEFAULT_ACC_ADDR;
int accelCS = -1;
int magBus = BMC150_DEFAULT_BUS;
int magAddr = BMC150_DEFAULT_MAG_ADDR;
int magCS = -1;
std::vector<std::string> upmTokens;
upmTokens = UpmStringParser::parse(initStr);
for(std::string tok : upmTokens)
{
if(tok.substr(0, 9) == "accelBus:")
accelBus = std::stoi(tok.substr(9), nullptr, 0);
if(tok.substr(0, 10) == "accelAddr:")
accelAddr = std::stoi(tok.substr(10), nullptr, 0);
if(tok.substr(0, 8) == "accelCS:")
accelCS = std::stoi(tok.substr(8), nullptr, 0);
if(tok.substr(0, 7) == "magBus:")
magBus = std::stoi(tok.substr(7), nullptr, 0);
if(tok.substr(0, 8) == "magAddr:")
magAddr = std::stoi(tok.substr(8), nullptr, 0);
if(tok.substr(0, 6) == "magCS:")
magCS = std::stoi(tok.substr(6), nullptr, 0);
}
// if -1 is supplied as a bus for any of these, we will not
// instantiate them
if (accelBus >= 0)
m_accel = new BMA250E(accelBus, accelAddr, accelCS);
if (magBus >= 0)
m_mag = new BMM150(magBus, magAddr, magCS);
// now initialize them...
if (m_accel)
m_accel->init();
if (m_mag)
m_mag->init();
std::string::size_type sz, prev_sz;
for(std::string tok : upmTokens)
{
if(tok.substr(0, 18) == "initAccelerometer:")
{
BMA250E_POWER_MODE_T pwr = (BMA250E_POWER_MODE_T)std::stoi(tok.substr(18), &sz, 0);
tok = tok.substr(18);
prev_sz = sz;
BMA250E_RANGE_T range = (BMA250E_RANGE_T)std::stoi(tok.substr(prev_sz + 1), &sz, 0);
tok = tok.substr(prev_sz + 1);
BMA250E_BW_T bw = (BMA250E_BW_T)std::stoi(tok.substr(sz + 1), nullptr, 0);
initAccelerometer(pwr, range, bw);
}
if(tok.substr(0, 17) == "initMagnetometer:")
{
BMM150_USAGE_PRESETS_T usage = (BMM150_USAGE_PRESETS_T)std::stoi(tok.substr(17), nullptr, 0);
initMagnetometer(usage);
}
}
}
BMC150::~BMC150() BMC150::~BMC150()
{ {
if (m_accel) if (m_accel)

View File

@ -100,6 +100,13 @@ namespace upm {
int magAddr=BMC150_DEFAULT_MAG_ADDR, int magAddr=BMC150_DEFAULT_MAG_ADDR,
int magCS=-1); int magCS=-1);
/**
* Instantiates bmc150 6-axis Ecompass based on a given string.
*
* @param initStr string containing specific information for bmc150 initialization.
*/
BMC150(std::string initStr);
/** /**
* BMC150 Destructor. * BMC150 Destructor.
*/ */

View File

@ -30,6 +30,7 @@
#include <string> #include <string>
#include "bmi055.hpp" #include "bmi055.hpp"
#include "upm_string_parser.hpp"
using namespace upm; using namespace upm;
using namespace std; using namespace std;
@ -55,6 +56,77 @@ BMI055::BMI055(int accelBus, int accelAddr, int accelCS,
m_gyro->init(); m_gyro->init();
} }
BMI055::BMI055(std::string initStr) : m_accel(0), m_gyro(0)
{
int accelBus = BMA250E_DEFAULT_I2C_BUS;
int accelAddr = BMA250E_DEFAULT_ADDR;
int accelCS = -1;
int gyroBus = BMG160_DEFAULT_I2C_BUS;
int gyroAddr = BMG160_DEFAULT_ADDR;
int gyroCS = -1;
std::vector<std::string> upmTokens;
upmTokens = UpmStringParser::parse(initStr);
for(std::string tok : upmTokens)
{
if(tok.substr(0, 9) == "accelBus:")
accelBus = std::stoi(tok.substr(9), nullptr, 0);
if(tok.substr(0, 10) == "accelAddr:")
accelAddr = std::stoi(tok.substr(10), nullptr, 0);
if(tok.substr(0, 8) == "accelCS:")
accelCS = std::stoi(tok.substr(8), nullptr, 0);
if(tok.substr(0, 8) == "gyroBus:")
gyroBus = std::stoi(tok.substr(8), nullptr, 0);
if(tok.substr(0, 9) == "gyroAddr:")
gyroAddr = std::stoi(tok.substr(9), nullptr, 0);
if(tok.substr(0, 7) == "gyroCS:")
gyroCS = std::stoi(tok.substr(7), nullptr, 0);
}
// if -1 is supplied as a bus for any of these, we will not
// instantiate them
if (accelBus >= 0)
m_accel = new BMA250E(accelBus, accelAddr, accelCS);
if (gyroBus >= 0)
m_gyro = new BMG160(gyroBus, gyroAddr, gyroCS);
// now initialize them...
if (m_accel)
m_accel->init();
if (m_gyro)
m_gyro->init();
std::string::size_type sz, prev_sz;
for(std::string tok : upmTokens)
{
if(tok.substr(0, 18) == "initAccelerometer:")
{
BMA250E_POWER_MODE_T pwr = (BMA250E_POWER_MODE_T)std::stoi(tok.substr(18), &sz, 0);
tok = tok.substr(18);
prev_sz = sz;
BMA250E_RANGE_T range = (BMA250E_RANGE_T)std::stoi(tok.substr(prev_sz + 1), &sz, 0);
tok = tok.substr(prev_sz + 1);
BMA250E_BW_T bw = (BMA250E_BW_T)std::stoi(tok.substr(sz + 1), nullptr, 0);
initAccelerometer(pwr, range, bw);
}
if(tok.substr(0, 14) == "initGyroscope:")
{
BMG160_POWER_MODE_T pwr = (BMG160_POWER_MODE_T)std::stoi(tok.substr(14), &sz, 0);
tok = tok.substr(14);
prev_sz = sz;
BMG160_RANGE_T range = (BMG160_RANGE_T)std::stoi(tok.substr(prev_sz + 1), &sz, 0);
tok = tok.substr(prev_sz + 1);
BMG160_BW_T bw = (BMG160_BW_T)std::stoi(tok.substr(sz + 1), nullptr, 0);
initGyroscope(pwr, range, bw);
}
}
}
BMI055::~BMI055() BMI055::~BMI055()
{ {
if (m_accel) if (m_accel)

View File

@ -94,6 +94,13 @@ namespace upm {
int gyroAddr=BMG160_DEFAULT_ADDR, int gyroAddr=BMG160_DEFAULT_ADDR,
int gyroCS=-1); int gyroCS=-1);
/**
* Instantiates bmi055 based on a given string.
*
* @param initStr string containing specific information for bmi055 initialization.
*/
BMI055(std::string initStr);
/** /**
* BMI055 Destructor. * BMI055 Destructor.
*/ */

View File

@ -30,6 +30,7 @@
#include <string> #include <string>
#include "bmx055.hpp" #include "bmx055.hpp"
#include "upm_string_parser.hpp"
using namespace upm; using namespace upm;
using namespace std; using namespace std;
@ -52,6 +53,88 @@ BMX055::BMX055(int accelBus, int accelAddr, int accelCS,
m_mag = new BMM150(magBus, magAddr, magCS); m_mag = new BMM150(magBus, magAddr, magCS);
} }
BMX055::BMX055(std::string initStr) :m_accel(0), m_gyro(0), m_mag(0)
{
int accelBus = BMA250E_DEFAULT_I2C_BUS;
int accelAddr = BMA250E_DEFAULT_ADDR;
int accelCS = -1;
int gyroBus = BMG160_DEFAULT_I2C_BUS;
int gyroAddr = BMG160_DEFAULT_ADDR;
int gyroCS = -1;
int magBus = BMM150_DEFAULT_I2C_BUS;
int magAddr = BMX055_DEFAULT_MAG_I2C_ADDR;
int magCS = -1;
std::vector<std::string> upmTokens;
upmTokens = UpmStringParser::parse(initStr);
for(std::string tok : upmTokens)
{
if(tok.substr(0, 9) == "accelBus:")
accelBus = std::stoi(tok.substr(9), nullptr, 0);
if(tok.substr(0, 10) == "accelAddr:")
accelAddr = std::stoi(tok.substr(10), nullptr, 0);
if(tok.substr(0, 8) == "accelCS:")
accelCS = std::stoi(tok.substr(8), nullptr, 0);
if(tok.substr(0, 8) == "gyroBus:")
gyroBus = std::stoi(tok.substr(8), nullptr, 0);
if(tok.substr(0, 9) == "gyroAddr:")
gyroAddr = std::stoi(tok.substr(9), nullptr, 0);
if(tok.substr(0, 7) == "gyroCS:")
gyroCS = std::stoi(tok.substr(7), nullptr, 0);
if(tok.substr(0, 7) == "magBus:")
magBus = std::stoi(tok.substr(7), nullptr, 0);
if(tok.substr(0, 8) == "magAddr:")
magAddr = std::stoi(tok.substr(8), nullptr, 0);
if(tok.substr(0, 6) == "magCS:")
magCS = std::stoi(tok.substr(6), nullptr, 0);
}
// if -1 is supplied as a bus for any of these, we will not
// instantiate them
if (accelBus >= 0)
m_accel = new BMA250E(accelBus, accelAddr, accelCS);
if (gyroBus >= 0)
m_gyro = new BMG160(gyroBus, gyroAddr, gyroCS);
if (magBus >= 0)
m_mag = new BMM150(magBus, magAddr, magCS);
std::string::size_type sz, prev_sz;
for(std::string tok : upmTokens)
{
if(tok.substr(0, 18) == "initAccelerometer:")
{
BMA250E_POWER_MODE_T pwr = (BMA250E_POWER_MODE_T)std::stoi(tok.substr(18), &sz, 0);
tok = tok.substr(18);
prev_sz = sz;
BMA250E_RANGE_T range = (BMA250E_RANGE_T)std::stoi(tok.substr(prev_sz + 1), &sz, 0);
tok = tok.substr(prev_sz + 1);
BMA250E_BW_T bw = (BMA250E_BW_T)std::stoi(tok.substr(sz + 1), nullptr, 0);
initAccelerometer(pwr, range, bw);
}
if(tok.substr(0, 14) == "initGyroscope:")
{
BMG160_POWER_MODE_T pwr = (BMG160_POWER_MODE_T)std::stoi(tok.substr(14), &sz, 0);
tok = tok.substr(14);
prev_sz = sz;
BMG160_RANGE_T range = (BMG160_RANGE_T)std::stoi(tok.substr(prev_sz + 1), &sz, 0);
tok = tok.substr(prev_sz + 1);
BMG160_BW_T bw = (BMG160_BW_T)std::stoi(tok.substr(sz + 1), nullptr, 0);
initGyroscope(pwr, range, bw);
}
if(tok.substr(0, 17) == "initMagnetometer:")
{
BMM150_USAGE_PRESETS_T usage = (BMM150_USAGE_PRESETS_T)std::stoi(tok.substr(17), nullptr, 0);
initMagnetometer(usage);
}
}
}
BMX055::~BMX055() BMX055::~BMX055()
{ {
if (m_accel) if (m_accel)

View File

@ -114,6 +114,13 @@ namespace upm {
int magAddr=BMX055_DEFAULT_MAG_I2C_ADDR, int magAddr=BMX055_DEFAULT_MAG_I2C_ADDR,
int magCS=-1); int magCS=-1);
/**
* Instantiates BMX055 based on a given string.
*
* @param initStr string containing specific information for BMX055 initialization.
*/
BMX055(std::string initStr);
/** /**
* BMX055 Destructor. * BMX055 Destructor.
*/ */