From 9c4a4d0ff0f218551023cbb04f5462f9f20336c1 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 10 Sep 2015 16:26:36 -0600 Subject: [PATCH] ak8975: throw exception(s) on fatal errors Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/mpu9150/ak8975.cxx | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/mpu9150/ak8975.cxx b/src/mpu9150/ak8975.cxx index cd4b5b31..7330eb37 100644 --- a/src/mpu9150/ak8975.cxx +++ b/src/mpu9150/ak8975.cxx @@ -25,6 +25,7 @@ #include #include #include +#include #include "ak8975.h" @@ -43,8 +44,8 @@ AK8975::AK8975(int bus, uint8_t address): mraa::Result rv; if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": Could not initialize i2c address. " << endl; - printError(rv); + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.address() failed"); return; } } @@ -62,13 +63,15 @@ bool AK8975::init() if (!setMode(CNTL_PWRDWN)) { - cerr << __FUNCTION__ << ": Unable to set PWRDWN mode" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": Unable to set PWRDWN mode"); return false; } if (!setMode(CNTL_FUSE_ACCESS)) { - cerr << __FUNCTION__ << ": Unable to set FUSE mode" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": Unable to set FUSE mode"); return false; } @@ -80,7 +83,8 @@ bool AK8975::init() // now, place back in power down mode if (!setMode(CNTL_PWRDWN)) { - cerr << __FUNCTION__ << ": Unable to reset PWRDWN mode" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": Unable to set reset PWRDWN mode"); return false; } @@ -92,8 +96,8 @@ bool AK8975::setMode(CNTL_MODES_T mode) mraa::Result rv; if ((rv = m_i2c.writeReg(REG_CNTL, mode)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": failed:" << endl; - printError(rv); + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -128,8 +132,8 @@ bool AK8975::waitforDeviceReady() retries++; } - cerr << __FUNCTION__ << ": timeout waiting for device to become ready" - << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": timeout waiting for device to become ready"); return false; } @@ -145,7 +149,8 @@ bool AK8975::update(bool selfTest) // First set measurement mode (take a measurement) if (!setMode(CNTL_MEASURE)) { - cerr << __FUNCTION__ << ": Unable to set MEASURE mode" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": Unable to set MEASURE mode"); return false; } } @@ -176,22 +181,24 @@ bool AK8975::selfTest() // set power down first if (!setMode(CNTL_PWRDWN)) { - cerr << __FUNCTION__ << ": Unable to set PWRDWN mode" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": Unable to set PWRDWN mode"); return false; } // enable self test bit if ((rv = m_i2c.writeReg(REG_ASTC, ASTC_SELF)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": failed to enable self test:" << endl; - printError(rv); + throw std::runtime_error(std::string(__FUNCTION__) + + ": failed to enable self test"); return false; } // now set self test mode if (!setMode(CNTL_SELFTEST)) { - cerr << __FUNCTION__ << ": Unable to set SELFTEST mode" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": Unable to set SELFTEST mode"); return false; } @@ -203,8 +210,8 @@ bool AK8975::selfTest() reg &= ~ASTC_SELF; if ((rv = m_i2c.writeReg(REG_ASTC, reg)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": failed to disable self test:" << endl; - printError(rv); + throw std::runtime_error(std::string(__FUNCTION__) + + ": failed to disable self test"); return false; }