Make static allocation for mraaIo variable

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-08-02 15:32:42 +03:00 committed by Mihai Tudor Panu
parent 90167f6ac1
commit 9869b31a3e
6 changed files with 51 additions and 43 deletions

View File

@ -88,6 +88,11 @@
using namespace upm; using namespace upm;
static bool operator!(mraa::MraaIo &mraaIo)
{
return mraaIo.getMraaDescriptors() == NULL;
}
Adxl345::Adxl345(int bus) : m_i2c(new mraa::I2c(bus)) Adxl345::Adxl345(int bus) : m_i2c(new mraa::I2c(bus))
{ {
//init bus and reset chip //init bus and reset chip
@ -123,17 +128,11 @@ Adxl345::Adxl345(int bus) : m_i2c(new mraa::I2c(bus))
Adxl345::update(); Adxl345::update();
} }
Adxl345::Adxl345(std::string initStr) : mraaIo(new mraa::MraaIo(initStr)) Adxl345::Adxl345(std::string initStr) : mraaIo(initStr)
{ {
if(mraaIo == NULL) if(!mraaIo.i2cs.empty())
{ {
throw std::invalid_argument(std::string(__FUNCTION__) + m_i2c = &mraaIo.i2cs[0];
": Failed to allocate memory for internal member");
}
if(!mraaIo->i2cs.empty())
{
m_i2c = &mraaIo->i2cs[0];
} }
else else
{ {
@ -143,7 +142,7 @@ Adxl345::Adxl345(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
m_buffer[0] = ADXL345_POWER_CTL; m_buffer[0] = ADXL345_POWER_CTL;
m_buffer[1] = ADXL345_POWER_ON; m_buffer[1] = ADXL345_POWER_ON;
if( m_i2c->write(m_buffer, 2) != mraa::SUCCESS){ if( m_i2c->write(m_buffer, 2) != mraa::SUCCESS ){
throw std::runtime_error(std::string(__FUNCTION__) + throw std::runtime_error(std::string(__FUNCTION__) +
": i2c.write() control register failed"); ": i2c.write() control register failed");
return; return;
@ -169,9 +168,7 @@ Adxl345::Adxl345(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
Adxl345::~Adxl345() Adxl345::~Adxl345()
{ {
if(mraaIo != NULL) if(!mraaIo)
delete mraaIo;
else
delete m_i2c; delete m_i2c;
} }

View File

@ -125,7 +125,7 @@ private:
int16_t m_rawaccel[3]; int16_t m_rawaccel[3];
uint8_t m_buffer[READ_BUFFER_LENGTH]; uint8_t m_buffer[READ_BUFFER_LENGTH];
mraa::I2c* m_i2c = NULL; mraa::I2c* m_i2c = NULL;
mraa::MraaIo* mraaIo = NULL; mraa::MraaIo mraaIo;
}; };
} }

View File

@ -30,6 +30,11 @@
using namespace std; using namespace std;
using namespace upm; using namespace upm;
static bool operator!(mraa::MraaIo &mraaIo)
{
return mraaIo.getMraaDescriptors() == NULL;
}
ADXRS610::ADXRS610(int dPin, int tPin, float aref) : ADXRS610::ADXRS610(int dPin, int tPin, float aref) :
m_aioData(new mraa::Aio(dPin)), m_aioTemp(new mraa::Aio(tPin)) m_aioData(new mraa::Aio(dPin)), m_aioTemp(new mraa::Aio(tPin))
{ {
@ -43,17 +48,12 @@ ADXRS610::ADXRS610(int dPin, int tPin, float aref) :
m_centerVolts = aref / 2.0; m_centerVolts = aref / 2.0;
} }
ADXRS610::ADXRS610(std::string initStr) : mraaIo(new mraa::MraaIo(initStr)) ADXRS610::ADXRS610(std::string initStr) : mraaIo(initStr)
{ {
if(mraaIo == NULL) if(!mraaIo.aios.empty())
{ {
throw std::invalid_argument(std::string(__FUNCTION__) + m_aioData = &mraaIo.aios[0];
": Failed to allocate memory for internal member"); m_aioTemp = &mraaIo.aios[1];
}
if(!mraaIo->aios.empty())
{
m_aioData = &mraaIo->aios[0];
m_aioTemp = &mraaIo->aios[1];
} }
else else
{ {
@ -63,8 +63,8 @@ ADXRS610::ADXRS610(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
std::vector<std::string> upmTokens; std::vector<std::string> upmTokens;
if (!mraaIo->getLeftoverStr().empty()) { if (!mraaIo.getLeftoverStr().empty()) {
upmTokens = UpmStringParser::parse(mraaIo->getLeftoverStr()); upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
} }
// ADC resolution of data and temp should be the same... // ADC resolution of data and temp should be the same...
@ -84,9 +84,7 @@ ADXRS610::ADXRS610(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
ADXRS610::~ADXRS610() ADXRS610::~ADXRS610()
{ {
if(mraaIo != NULL) if(!mraaIo)
delete mraaIo;
else
{ {
delete m_aioData; delete m_aioData;
delete m_aioTemp; delete m_aioTemp;

View File

@ -177,7 +177,7 @@ namespace upm {
protected: protected:
mraa::Aio* m_aioData = NULL; mraa::Aio* m_aioData = NULL;
mraa::Aio* m_aioTemp = NULL; mraa::Aio* m_aioTemp = NULL;
mraa::MraaIo* mraaIo = NULL; mraa::MraaIo mraaIo;
private: private:
float m_aref; float m_aref;

View File

@ -34,6 +34,10 @@
using namespace upm; using namespace upm;
using namespace std; using namespace std;
static bool operator!(mraa::MraaIo &mraaIo)
{
return mraaIo.getMraaDescriptors() == NULL;
}
BMA220::BMA220(int bus, uint8_t addr) : BMA220::BMA220(int bus, uint8_t addr) :
m_i2c(new mraa::I2c(bus)), m_gpioIntr(0) m_i2c(new mraa::I2c(bus)), m_gpioIntr(0)
@ -66,7 +70,7 @@ BMA220::BMA220(int bus, uint8_t addr) :
} }
} }
BMA220::BMA220(std::string initStr) : mraaIo(new mraa::MraaIo(initStr)) BMA220::BMA220(std::string initStr) : mraaIo(initStr)
{ {
m_accelX = 0.0; m_accelX = 0.0;
m_accelY = 0.0; m_accelY = 0.0;
@ -74,13 +78,8 @@ BMA220::BMA220(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
m_accelScale = 0.0; m_accelScale = 0.0;
if(mraaIo == NULL) { if(!mraaIo.i2cs.empty()) {
throw std::invalid_argument(std::string(__FUNCTION__) + m_i2c = &mraaIo.i2cs[0];
": Failed to allocate memory for internal member");
}
if(!mraaIo->i2cs.empty()) {
m_i2c = &mraaIo->i2cs[0];
} }
else { else {
throw std::invalid_argument(std::string(__FUNCTION__) + throw std::invalid_argument(std::string(__FUNCTION__) +
@ -98,8 +97,8 @@ BMA220::BMA220(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
} }
std::vector<std::string> upmTokens; std::vector<std::string> upmTokens;
if(!mraaIo->getLeftoverStr().empty()) { if(!mraaIo.getLeftoverStr().empty()) {
upmTokens = UpmStringParser::parse(mraaIo->getLeftoverStr()); upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
} }
std::string::size_type sz; std::string::size_type sz;
@ -139,9 +138,7 @@ BMA220::BMA220(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
BMA220::~BMA220() BMA220::~BMA220()
{ {
uninstallISR(); uninstallISR();
if(mraaIo != NULL) if(!mraaIo)
delete mraaIo;
else
delete m_i2c; delete m_i2c;
} }

View File

@ -181,6 +181,22 @@ namespace upm {
/** /**
* REG_TAP_CONFIG bits * REG_TAP_CONFIG bits
*/ */
typedef enum {
TAP_CONFIG_DUR0 = 0x01,
TAP_CONFIG_DUR1 = 0x02,
TAP_CONFIG_DUR2 = 0x04,
_TAP_CONFIG_DUR_MASK = 7,
_TAP_CONFIG_DUR_SHIFT = 0,
TAP_CONFIG_THRESH0 = 0x08,
TAP_CONFIG_THRESH1 = 0x10,
TAP_CONFIG_THRESH2 = 0x20,
TAP_CONFIG_THRESH3 = 0x40,
_TAP_CONFIG_THRESH_MASK = 15,
_TAP_CONFIG_THRESH_SHIFT = 3,
TAP_CONFIG_FILTER = 0x80
} TAP_CONFIG_BITS_T;
/** /**
* REG_SLOPE_CONFIG bits * REG_SLOPE_CONFIG bits
@ -801,7 +817,7 @@ namespace upm {
protected: protected:
mraa::I2c *m_i2c; mraa::I2c *m_i2c;
mraa::Gpio *m_gpioIntr; mraa::Gpio *m_gpioIntr;
mraa::MraaIo *mraaIo; mraa::MraaIo mraaIo;
uint8_t m_addr; uint8_t m_addr;
// uncompensated accelerometer values // uncompensated accelerometer values