diff --git a/src/h3lis331dl/h3lis331dl.cxx b/src/h3lis331dl/h3lis331dl.cxx index 0934ce26..e168e029 100644 --- a/src/h3lis331dl/h3lis331dl.cxx +++ b/src/h3lis331dl/h3lis331dl.cxx @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "h3lis331dl.h" @@ -41,8 +42,8 @@ H3LIS331DL::H3LIS331DL(int bus, uint8_t address): mraa::Result rv; if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS) { - cerr << "H3LIS331DL: Could not initialize i2c address. " << endl; - printError(rv); + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.address() failed"); return; } @@ -84,7 +85,8 @@ bool H3LIS331DL::setDataRate(DR_BITS_T odr) if (m_i2c.writeReg(REG_REG1, reg1)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -100,7 +102,8 @@ bool H3LIS331DL::setPowerMode(PM_BITS_T pm) if (m_i2c.writeReg(REG_REG1, reg1)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -116,7 +119,8 @@ bool H3LIS331DL::enableAxis(uint8_t axisEnable) if (m_i2c.writeReg(REG_REG1, reg1)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -132,7 +136,8 @@ bool H3LIS331DL::setFullScale(FS_BITS_T fs) if (m_i2c.writeReg(REG_REG4, reg4)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -148,7 +153,8 @@ bool H3LIS331DL::setHPCF(HPCF_BITS_T val) if (m_i2c.writeReg(REG_REG2, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -164,7 +170,8 @@ bool H3LIS331DL::setHPM(HPM_BITS_T val) if (m_i2c.writeReg(REG_REG2, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -179,7 +186,8 @@ bool H3LIS331DL::boot() if (m_i2c.writeReg(REG_REG2, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -203,7 +211,8 @@ bool H3LIS331DL::enableHPF1(bool enable) if (m_i2c.writeReg(REG_REG2, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -221,7 +230,8 @@ bool H3LIS331DL::enableHPF2(bool enable) if (m_i2c.writeReg(REG_REG2, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -239,7 +249,8 @@ bool H3LIS331DL::enableFDS(bool enable) if (m_i2c.writeReg(REG_REG2, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -257,7 +268,8 @@ bool H3LIS331DL::setInterruptActiveLow(bool enable) if (m_i2c.writeReg(REG_REG3, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -275,7 +287,8 @@ bool H3LIS331DL::setInterruptOpenDrain(bool enable) if (m_i2c.writeReg(REG_REG3, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -293,7 +306,8 @@ bool H3LIS331DL::setInterrupt1Latch(bool enable) if (m_i2c.writeReg(REG_REG3, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -311,7 +325,8 @@ bool H3LIS331DL::setInterrupt2Latch(bool enable) if (m_i2c.writeReg(REG_REG3, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -327,7 +342,8 @@ bool H3LIS331DL::setInterrupt1PadConfig(I_CFG_BITS_T val) if (m_i2c.writeReg(REG_REG3, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -343,7 +359,8 @@ bool H3LIS331DL::setInterrupt2PadConfig(I_CFG_BITS_T val) if (m_i2c.writeReg(REG_REG3, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -362,7 +379,8 @@ bool H3LIS331DL::enableBDU(bool enable) if (m_i2c.writeReg(REG_REG4, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -380,7 +398,8 @@ bool H3LIS331DL::enableBLE(bool enable) if (m_i2c.writeReg(REG_REG4, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -398,7 +417,8 @@ bool H3LIS331DL::enableSleepToWake(bool enable) if (m_i2c.writeReg(REG_REG5, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -419,7 +439,8 @@ bool H3LIS331DL::setInterrupt1Config(uint8_t val) if (m_i2c.writeReg(REG_INT1_CFG, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -435,7 +456,8 @@ bool H3LIS331DL::setInterrupt1Source(uint8_t val) if (m_i2c.writeReg(REG_INT1_SRC, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -446,7 +468,8 @@ bool H3LIS331DL::setInterrupt1Threshold(uint8_t val) { if (m_i2c.writeReg(REG_INT1_THS, val)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -457,7 +480,8 @@ bool H3LIS331DL::setInterrupt1Duration(uint8_t val) { if (m_i2c.writeReg(REG_INT1_DUR, val)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -473,7 +497,8 @@ bool H3LIS331DL::setInterrupt2Config(uint8_t val) if (m_i2c.writeReg(REG_INT2_CFG, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -489,7 +514,8 @@ bool H3LIS331DL::setInterrupt2Source(uint8_t val) if (m_i2c.writeReg(REG_INT2_SRC, reg)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -500,7 +526,8 @@ bool H3LIS331DL::setInterrupt2Threshold(uint8_t val) { if (m_i2c.writeReg(REG_INT2_THS, val)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -511,7 +538,8 @@ bool H3LIS331DL::setInterrupt2Duration(uint8_t val) { if (m_i2c.writeReg(REG_INT2_DUR, val)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; }