From 427dd6e851cfb39b8cc4c9506b901b9414329251 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Wed, 9 Sep 2015 12:02:56 -0600 Subject: [PATCH] mma7660: throw exception(s) on fatal errors Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/mma7660/mma7660.cxx | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/mma7660/mma7660.cxx b/src/mma7660/mma7660.cxx index 5928a463..777f79c7 100644 --- a/src/mma7660/mma7660.cxx +++ b/src/mma7660/mma7660.cxx @@ -23,6 +23,7 @@ */ #include +#include #include #include "mma7660.h" @@ -39,7 +40,8 @@ MMA7660::MMA7660(int bus, uint8_t address) // setup our i2c link if ( !(m_i2c = mraa_i2c_init(bus)) ) { - cerr << "MMA7660: mraa_i2c_init() failed." << endl; + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_i2c_init() failed"); return; } @@ -47,8 +49,8 @@ MMA7660::MMA7660(int bus, uint8_t address) if ( (rv = mraa_i2c_address(m_i2c, m_addr)) != MRAA_SUCCESS) { - cerr << "MMA7660: Could not initialize i2c bus. " << endl; - mraa_result_print(rv); + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_i2c_address() failed"); return; } } @@ -128,7 +130,8 @@ int MMA7660::getVerifiedAxis(MMA7660_REG_T axis) if (axis > 2) { - cerr << __FUNCTION__ << ": axis must be 0, 1, or 2." << endl; + throw std::out_of_range(std::string(__FUNCTION__) + + ": axis must be 0, 1, or 2."); return 0; } @@ -161,7 +164,6 @@ uint8_t MMA7660::getVerifiedTilt() // check alert bit } while (val & 0x40); - // shift the sign bit over, and compensate return val; } @@ -218,7 +220,8 @@ void MMA7660::installISR(int pin, void (*isr)(void *), void *arg) if ( !(m_gpio = mraa_gpio_init(pin)) ) { - cerr << __FUNCTION__ << ": mraa_gpio_init() failed" << endl; + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_gpio_init() failed, invalid pin?"); return; }