diff --git a/src/mpr121/mpr121.cxx b/src/mpr121/mpr121.cxx index 3462f4cf..8e7f7039 100644 --- a/src/mpr121/mpr121.cxx +++ b/src/mpr121/mpr121.cxx @@ -24,6 +24,7 @@ #include #include +#include #include "mpr121.h" @@ -34,11 +35,14 @@ using namespace std; MPR121::MPR121(int bus, uint8_t address) : m_i2c(bus) { m_addr = address; - mraa::Result ret = m_i2c.address(m_addr); - if (ret != mraa::SUCCESS) - cerr << "MPR121: Could not initialize i2c bus. " << endl; + if (ret != mraa::SUCCESS) + { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_i2c_address() failed"); + return; + } m_buttonStates = 0; m_overCurrentFault = false; @@ -90,7 +94,8 @@ bool MPR121::configAN3944() uint8_t eleConf = 0x00; if ((rv = writeBytes(0x5e, &eleConf, 1)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writeBytes(0x5e) failed"); return false; } @@ -100,7 +105,8 @@ bool MPR121::configAN3944() uint8_t sectA[] = { 0x01, 0x01, 0x00, 0x00 }; if ((rv = writeBytes(0x2b, sectA, 4)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writeBytes(0x2b) failed"); return false; } @@ -110,7 +116,8 @@ bool MPR121::configAN3944() uint8_t sectB[] = { 0x01, 0x01, 0xff, 0x02 }; if ((rv = writeBytes(0x2f, sectB, 4)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writeBytes(0x2f) failed"); return false; } @@ -132,7 +139,8 @@ bool MPR121::configAN3944() 0x0f, 0x0a }; if ((rv = writeBytes(0x41, sectC, 24)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writeBytes(0x41) failed"); return false; } @@ -142,7 +150,8 @@ bool MPR121::configAN3944() uint8_t filterConf = 0x04; if ((rv = writeBytes(0x5d, &filterConf, 1)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writeBytes(0x5d) failed"); return false; } @@ -152,13 +161,15 @@ bool MPR121::configAN3944() uint8_t sectF0 = 0x0b; if ((rv = writeBytes(0x7b, §F0, 1)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writeBytes(0x7b) failed"); return false; } uint8_t sectF1[] = { 0x9c, 0x65, 0x8c }; if ((rv = writeBytes(0x7d, sectF1, 3)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writeBytes(0x7d) failed"); return false; } @@ -169,7 +180,8 @@ bool MPR121::configAN3944() eleConf = 0x8c; if ((rv = writeBytes(0x5e, &eleConf, 1)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": " << __LINE__<< ": I2C write failed." << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": writeBytes(0x5e) failed"); return false; }