From e8823dd74e975b0ce4178d6864d76575e1a17ae0 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 10 Sep 2015 16:03:33 -0600 Subject: [PATCH] mma7455: throw exception(s) on fatal errors Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/mma7455/mma7455.cxx | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/mma7455/mma7455.cxx b/src/mma7455/mma7455.cxx index f071fae0..0c2c5d11 100644 --- a/src/mma7455/mma7455.cxx +++ b/src/mma7455/mma7455.cxx @@ -23,6 +23,8 @@ */ #include +#include +#include #include #include #include @@ -42,9 +44,17 @@ MMA7455::MMA7455 (int bus, int devAddr) : m_i2ControlCtx(bus) { m_controlAddr = devAddr; m_bus = bus; + if ( !(m_i2ControlCtx = mraa_i2c_init(m_bus)) ) + { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_i2c_init() failed"); + return; + } + mraa::Result error = m_i2ControlCtx.address(m_controlAddr); if (error != mraa::SUCCESS) { - fprintf(stderr, "Messed up i2c bus\n"); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_address() failed"); return; } @@ -52,12 +62,14 @@ MMA7455::MMA7455 (int bus, int devAddr) : m_i2ControlCtx(bus) { data = (BIT (MMA7455_GLVL0) | BIT (MMA7455_MODE0)); error = i2cWriteReg (MMA7455_MCTL, &data, 0x1); if (error != mraa::SUCCESS) { - std::cout << "ERROR :: MMA7455 instance wan not created (Mode)" << std::endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writing mode register failed"); return; } if (mraa::SUCCESS != calibrate ()) { - std::cout << "ERROR :: MMA7455 instance wan not created (Calibrate)" << std::endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": calibrate() failed"); return; } } @@ -140,16 +152,17 @@ short *MMA7455::readData() { #endif int +<<<<<<< HEAD MMA7455::i2cReadReg (unsigned char reg, uint8_t *buffer, int len) { if (mraa::SUCCESS != m_i2ControlCtx.address(m_controlAddr)) { + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_address() failed"); return 0; } if (mraa::SUCCESS != m_i2ControlCtx.writeByte(reg)) { - return 0; - } - - if (mraa::SUCCESS != m_i2ControlCtx.address(m_controlAddr)) { + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_write_byte() failed"); return 0; } @@ -166,10 +179,14 @@ MMA7455::i2cWriteReg (unsigned char reg, uint8_t *buffer, int len) { error = m_i2ControlCtx.address (m_controlAddr); if (error != mraa::SUCCESS) { + hrow std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_address() failed"); return error; } error = m_i2ControlCtx.write (data, len + 1); if (error != mraa::SUCCESS) { + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_write() failed"); return error; }