mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-30 06:34:58 +03:00 
			
		
		
		
	BMA220: Add string based cons for Accelerometer
Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
		 Adelin Dobre
					Adelin Dobre
				
			
				
					committed by
					
						 Stefan Andritoiu
						Stefan Andritoiu
					
				
			
			
				
	
			
			
			 Stefan Andritoiu
						Stefan Andritoiu
					
				
			
						parent
						
							881ab412a3
						
					
				
				
					commit
					384f4a45ea
				
			| @@ -29,13 +29,14 @@ | ||||
| #include <string.h> | ||||
|  | ||||
| #include "bma220.hpp" | ||||
| #include "upm_string_parser.hpp" | ||||
|  | ||||
| using namespace upm; | ||||
| using namespace std; | ||||
|  | ||||
|  | ||||
| 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; | ||||
|  | ||||
| @@ -46,7 +47,7 @@ BMA220::BMA220(int bus, uint8_t addr) : | ||||
|   m_accelScale = 0.0; | ||||
|  | ||||
|   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__) + | ||||
|                                ": 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() | ||||
| { | ||||
|   uninstallISR(); | ||||
|   if(mraaIo != NULL) | ||||
|     delete mraaIo; | ||||
|   else | ||||
|     delete m_i2c; | ||||
| } | ||||
|  | ||||
| void BMA220::update() | ||||
| @@ -96,13 +134,13 @@ void BMA220::updateAccelerometer() | ||||
|  | ||||
| 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) | ||||
| { | ||||
|   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__) + | ||||
|                                ": I2c.writeReg() failed"); | ||||
|   | ||||
| @@ -28,6 +28,7 @@ | ||||
| #include <mraa/common.hpp> | ||||
| #include <mraa/i2c.hpp> | ||||
| #include <mraa/gpio.hpp> | ||||
| #include <mraa/initio.hpp> | ||||
|  | ||||
| #define BMA220_I2C_BUS 0 | ||||
| #define BMA220_DEFAULT_ADDR 0x0a | ||||
| @@ -482,6 +483,13 @@ namespace upm { | ||||
|      */ | ||||
|     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 | ||||
|      */ | ||||
| @@ -798,8 +806,9 @@ namespace upm { | ||||
|     mraa::Gpio* get_gpioIntr(); | ||||
|  | ||||
|   protected: | ||||
|     mraa::I2c m_i2c; | ||||
|     mraa::I2c *m_i2c; | ||||
|     mraa::Gpio *m_gpioIntr; | ||||
|     mraa::MraaIo *mraaIo; | ||||
|     uint8_t m_addr; | ||||
|  | ||||
|     // uncompensated accelerometer values | ||||
|   | ||||
		Reference in New Issue
	
	Block a user