mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
BMA220: Add string based cons for Accelerometer
Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
parent
881ab412a3
commit
384f4a45ea
@ -29,13 +29,14 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "bma220.hpp"
|
#include "bma220.hpp"
|
||||||
|
#include "upm_string_parser.hpp"
|
||||||
|
|
||||||
using namespace upm;
|
using namespace upm;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
BMA220::BMA220(int bus, uint8_t addr) :
|
BMA220::BMA220(int bus, uint8_t addr) :
|
||||||
m_i2c(bus), m_gpioIntr(0)
|
m_i2c(new mraa::I2c(bus)), m_gpioIntr(0)
|
||||||
{
|
{
|
||||||
m_addr = addr;
|
m_addr = addr;
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ BMA220::BMA220(int bus, uint8_t addr) :
|
|||||||
m_accelScale = 0.0;
|
m_accelScale = 0.0;
|
||||||
|
|
||||||
mraa::Result rv;
|
mraa::Result rv;
|
||||||
if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS)
|
if ( (rv = m_i2c->address(m_addr)) != mraa::SUCCESS)
|
||||||
{
|
{
|
||||||
throw std::runtime_error(string(__FUNCTION__) +
|
throw std::runtime_error(string(__FUNCTION__) +
|
||||||
": I2c.address() failed");
|
": I2c.address() failed");
|
||||||
@ -65,9 +66,46 @@ BMA220::BMA220(int bus, uint8_t addr) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BMA220::BMA220(std::string initStr) : mraaIo(new mraa::MraaIo(initStr))
|
||||||
|
{
|
||||||
|
m_accelX = 0.0;
|
||||||
|
m_accelY = 0.0;
|
||||||
|
m_accelZ = 0.0;
|
||||||
|
|
||||||
|
m_accelScale = 0.0;
|
||||||
|
|
||||||
|
if(mraaIo == NULL) {
|
||||||
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||||
|
": Failed to allocate memory for internal member");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!mraaIo->i2cs.empty()) {
|
||||||
|
m_i2c = &mraaIo->i2cs[0];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||||
|
": mraa_i2c_init() failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init the accelerometer
|
||||||
|
enableAxes(true, true, true);
|
||||||
|
|
||||||
|
// set scaling rate
|
||||||
|
if (!setAccelerometerScale(FSL_RANGE_2G))
|
||||||
|
{
|
||||||
|
throw std::runtime_error(string(__FUNCTION__) +
|
||||||
|
": Unable to set accel scale");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BMA220::~BMA220()
|
BMA220::~BMA220()
|
||||||
{
|
{
|
||||||
uninstallISR();
|
uninstallISR();
|
||||||
|
if(mraaIo != NULL)
|
||||||
|
delete mraaIo;
|
||||||
|
else
|
||||||
|
delete m_i2c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BMA220::update()
|
void BMA220::update()
|
||||||
@ -96,13 +134,13 @@ void BMA220::updateAccelerometer()
|
|||||||
|
|
||||||
uint8_t BMA220::readReg(uint8_t reg)
|
uint8_t BMA220::readReg(uint8_t reg)
|
||||||
{
|
{
|
||||||
return m_i2c.readReg(reg);
|
return m_i2c->readReg(reg);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BMA220::writeReg(uint8_t reg, uint8_t val)
|
bool BMA220::writeReg(uint8_t reg, uint8_t val)
|
||||||
{
|
{
|
||||||
mraa::Result rv;
|
mraa::Result rv;
|
||||||
if ((rv = m_i2c.writeReg(reg, val)) != mraa::SUCCESS)
|
if ((rv = m_i2c->writeReg(reg, val)) != mraa::SUCCESS)
|
||||||
{
|
{
|
||||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
": I2c.writeReg() failed");
|
": I2c.writeReg() failed");
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
#include <mraa/common.hpp>
|
#include <mraa/common.hpp>
|
||||||
#include <mraa/i2c.hpp>
|
#include <mraa/i2c.hpp>
|
||||||
#include <mraa/gpio.hpp>
|
#include <mraa/gpio.hpp>
|
||||||
|
#include <mraa/initio.hpp>
|
||||||
|
|
||||||
#define BMA220_I2C_BUS 0
|
#define BMA220_I2C_BUS 0
|
||||||
#define BMA220_DEFAULT_ADDR 0x0a
|
#define BMA220_DEFAULT_ADDR 0x0a
|
||||||
@ -482,6 +483,13 @@ namespace upm {
|
|||||||
*/
|
*/
|
||||||
BMA220(int bus=BMA220_I2C_BUS, uint8_t addr=BMA220_DEFAULT_ADDR);
|
BMA220(int bus=BMA220_I2C_BUS, uint8_t addr=BMA220_DEFAULT_ADDR);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates BMA220 Accelerometer object based on a given string.
|
||||||
|
*
|
||||||
|
* @param initStr string containing specific information for BMA220 initialization.
|
||||||
|
*/
|
||||||
|
BMA220(std::string initStr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BMA220 Destructor
|
* BMA220 Destructor
|
||||||
*/
|
*/
|
||||||
@ -798,8 +806,9 @@ namespace upm {
|
|||||||
mraa::Gpio* get_gpioIntr();
|
mraa::Gpio* get_gpioIntr();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
mraa::I2c m_i2c;
|
mraa::I2c *m_i2c;
|
||||||
mraa::Gpio *m_gpioIntr;
|
mraa::Gpio *m_gpioIntr;
|
||||||
|
mraa::MraaIo *mraaIo;
|
||||||
uint8_t m_addr;
|
uint8_t m_addr;
|
||||||
|
|
||||||
// uncompensated accelerometer values
|
// uncompensated accelerometer values
|
||||||
|
Loading…
x
Reference in New Issue
Block a user