From 5cc8c9025176bfc17a52ad9516dc8978b282eb07 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 10 Sep 2015 16:20:13 -0600 Subject: [PATCH] mpu9150: throw exception(s) on fatal errors Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/mpu9150/mpu60x0.cxx | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/mpu9150/mpu60x0.cxx b/src/mpu9150/mpu60x0.cxx index 2bff5984..e22e78a1 100644 --- a/src/mpu9150/mpu60x0.cxx +++ b/src/mpu9150/mpu60x0.cxx @@ -53,8 +53,8 @@ MPU60X0::MPU60X0(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; } } @@ -69,7 +69,8 @@ bool MPU60X0::init() // first, take the device out of sleep mode if (!setSleep(false)) { - cerr << __FUNCTION__ << ": Unable to wake up device" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": Unable to wake up device"); return false; } @@ -77,7 +78,8 @@ bool MPU60X0::init() // internal clock for stability if (!setClockSource(PLL_XG)) { - cerr << __FUNCTION__ << ": Unable to set clock source" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": Unable to set clock source"); return false; } @@ -149,8 +151,13 @@ bool MPU60X0::writeReg(uint8_t reg, uint8_t val) mraa::Result rv; if ((rv = m_i2c.writeReg(reg, val)) != mraa::SUCCESS) { +<<<<<<< HEAD cerr << __FUNCTION__ << ": failed:" << endl; printError(rv); +======= + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); +>>>>>>> 9378f17... mpu9150: throw exception(s) on fatal errors return false; } @@ -215,7 +222,8 @@ bool MPU60X0::setGyroscopeScale(FS_SEL_T scale) default: // should never occur, but... m_gyroScale = 1.0; // set a safe, though incorrect value - cerr << __FUNCTION__ << ": internal error, unsupported scale" << endl; + throw std::logic_error(string(__FUNCTION__) + + ": internal error, unsupported scale"); break; } @@ -257,7 +265,8 @@ bool MPU60X0::setAccelerometerScale(AFS_SEL_T scale) default: // should never occur, but... m_accelScale = 1.0; // set a safe, though incorrect value - cerr << __FUNCTION__ << ": internal error, unsupported scale" << endl; + throw std::logic_error(string(__FUNCTION__) + + ": internal error, unsupported scale"); break; }