ak8975: throw exception(s) on fatal errors

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson 2015-09-10 16:26:36 -06:00 committed by Mihai Tudor Panu
parent 5cc8c90251
commit 9c4a4d0ff0

View File

@ -25,6 +25,7 @@
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <stdexcept>
#include "ak8975.h" #include "ak8975.h"
@ -43,8 +44,8 @@ AK8975::AK8975(int bus, uint8_t address):
mraa::Result rv; mraa::Result rv;
if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS) if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS)
{ {
cerr << __FUNCTION__ << ": Could not initialize i2c address. " << endl; throw std::runtime_error(std::string(__FUNCTION__) +
printError(rv); ": I2c.address() failed");
return; return;
} }
} }
@ -62,13 +63,15 @@ bool AK8975::init()
if (!setMode(CNTL_PWRDWN)) if (!setMode(CNTL_PWRDWN))
{ {
cerr << __FUNCTION__ << ": Unable to set PWRDWN mode" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to set PWRDWN mode");
return false; return false;
} }
if (!setMode(CNTL_FUSE_ACCESS)) if (!setMode(CNTL_FUSE_ACCESS))
{ {
cerr << __FUNCTION__ << ": Unable to set FUSE mode" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to set FUSE mode");
return false; return false;
} }
@ -80,7 +83,8 @@ bool AK8975::init()
// now, place back in power down mode // now, place back in power down mode
if (!setMode(CNTL_PWRDWN)) if (!setMode(CNTL_PWRDWN))
{ {
cerr << __FUNCTION__ << ": Unable to reset PWRDWN mode" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to set reset PWRDWN mode");
return false; return false;
} }
@ -92,8 +96,8 @@ bool AK8975::setMode(CNTL_MODES_T mode)
mraa::Result rv; mraa::Result rv;
if ((rv = m_i2c.writeReg(REG_CNTL, mode)) != mraa::SUCCESS) if ((rv = m_i2c.writeReg(REG_CNTL, mode)) != mraa::SUCCESS)
{ {
cerr << __FUNCTION__ << ": failed:" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
printError(rv); ": I2c.writeReg() failed");
return false; return false;
} }
@ -128,8 +132,8 @@ bool AK8975::waitforDeviceReady()
retries++; retries++;
} }
cerr << __FUNCTION__ << ": timeout waiting for device to become ready" throw std::runtime_error(std::string(__FUNCTION__) +
<< endl; ": timeout waiting for device to become ready");
return false; return false;
} }
@ -145,7 +149,8 @@ bool AK8975::update(bool selfTest)
// First set measurement mode (take a measurement) // First set measurement mode (take a measurement)
if (!setMode(CNTL_MEASURE)) if (!setMode(CNTL_MEASURE))
{ {
cerr << __FUNCTION__ << ": Unable to set MEASURE mode" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to set MEASURE mode");
return false; return false;
} }
} }
@ -176,22 +181,24 @@ bool AK8975::selfTest()
// set power down first // set power down first
if (!setMode(CNTL_PWRDWN)) if (!setMode(CNTL_PWRDWN))
{ {
cerr << __FUNCTION__ << ": Unable to set PWRDWN mode" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to set PWRDWN mode");
return false; return false;
} }
// enable self test bit // enable self test bit
if ((rv = m_i2c.writeReg(REG_ASTC, ASTC_SELF)) != mraa::SUCCESS) if ((rv = m_i2c.writeReg(REG_ASTC, ASTC_SELF)) != mraa::SUCCESS)
{ {
cerr << __FUNCTION__ << ": failed to enable self test:" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
printError(rv); ": failed to enable self test");
return false; return false;
} }
// now set self test mode // now set self test mode
if (!setMode(CNTL_SELFTEST)) if (!setMode(CNTL_SELFTEST))
{ {
cerr << __FUNCTION__ << ": Unable to set SELFTEST mode" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": Unable to set SELFTEST mode");
return false; return false;
} }
@ -203,8 +210,8 @@ bool AK8975::selfTest()
reg &= ~ASTC_SELF; reg &= ~ASTC_SELF;
if ((rv = m_i2c.writeReg(REG_ASTC, reg)) != mraa::SUCCESS) if ((rv = m_i2c.writeReg(REG_ASTC, reg)) != mraa::SUCCESS)
{ {
cerr << __FUNCTION__ << ": failed to disable self test:" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
printError(rv); ": failed to disable self test");
return false; return false;
} }