From da49741527310d67051ce5208cbfc5d5dfe3132f Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Wed, 9 Sep 2015 15:19:30 -0600 Subject: [PATCH] adxl345: throw exception(s) on fatal errors Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/adxl345/adxl345.cxx | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/adxl345/adxl345.cxx b/src/adxl345/adxl345.cxx index 9aedd52c..3503d64c 100644 --- a/src/adxl345/adxl345.cxx +++ b/src/adxl345/adxl345.cxx @@ -23,6 +23,8 @@ */ #include +#include +#include #include #include "math.h" #include "adxl345.h" @@ -87,16 +89,34 @@ using namespace upm; Adxl345::Adxl345(int bus) : m_i2c(bus) { - //reset chip - m_i2c.address(ADXL345_I2C_ADDR); + //init bus and reset chip + if ( m_i2c.address(ADXL345_I2C_ADDR) != mraa::SUCCESS ){ + throw std::invalid_argument(std::string(__FUNCTION__) + + ": i2c.address() failed"); + return; + } + m_buffer[0] = ADXL345_POWER_CTL; m_buffer[1] = ADXL345_POWER_ON; - m_i2c.write(m_buffer, 2); + if( m_i2c.write(m_buffer, 2) != mraa::SUCCESS){ + throw std::runtime_error(std::string(__FUNCTION__) + + ": i2c.write() control register failed"); + return; + } + + if ( m_i2c.address(ADXL345_I2C_ADDR) != mraa::SUCCESS ){ + throw std::invalid_argument(std::string(__FUNCTION__) + + ": i2c.address() failed"); + return; + } - m_i2c.address(ADXL345_I2C_ADDR); m_buffer[0] = ADXL345_DATA_FORMAT; m_buffer[1] = ADXL345_16G | ADXL345_FULL_RES; - m_i2c.write(m_buffer, 2); + if( m_i2c.write(m_buffer, 2) != mraa::SUCCESS){ + throw std::runtime_error(std::string(__FUNCTION__) + + ": i2c.write() mode register failed"); + return; + } //2.5V sensitivity is 256 LSB/g = 0.00390625 g/bit //3.3V x and y sensitivity is 265 LSB/g = 0.003773584 g/bit, z is the same