diff --git a/src/hp20x/hp20x.cxx b/src/hp20x/hp20x.cxx index 84301270..73ae3e25 100644 --- a/src/hp20x/hp20x.cxx +++ b/src/hp20x/hp20x.cxx @@ -26,6 +26,7 @@ #include #include #include +#include #include "hp20x.h" @@ -41,8 +42,8 @@ HP20X::HP20X(int bus, uint8_t address): mraa::Result rv; if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS) { - cerr << "HP20X: Could not initialize i2c address. " << endl; - printError(rv); + throw std::invalid_argument(std::string(__FUNCTION__) + + ": I2c.address() failed"); return; } } @@ -90,8 +91,8 @@ bool HP20X::waitforDeviceReady() retries++; } - cerr << __FUNCTION__ << ": timeout waiting for device to become ready" - << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": timeout waiting for device to become ready"); return false; } @@ -101,7 +102,8 @@ bool HP20X::writeCmd(uint8_t cmd) mraa::Result rv; if ((rv = m_i2c.writeByte(cmd)) != mraa::SUCCESS) { - printError(rv); + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeByte() failed"); return false; } @@ -117,7 +119,8 @@ bool HP20X::writeReg(HP20X_REG_T reg, uint8_t data) mraa::Result rv; if ((rv = m_i2c.writeReg(r, data)) != mraa::SUCCESS) { - cerr << __FUNCTION__ << ": writeReg failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.writeReg() failed"); return false; } @@ -137,7 +140,8 @@ int HP20X::readData() if (!m_i2c.read(buf, 3)) { - cerr << __FUNCTION__ << ": read failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.read() failed"); return 0; } @@ -160,21 +164,13 @@ float HP20X::getTemperature() // start conversion, T only uint8_t cmd = CMD_ADC_CVT | (CHNL_T << CHNL_SHIFT) | (m_dsr << DSR_SHIFT); - if (!writeCmd(cmd)) - { - cerr << __FUNCTION__ << ": writeCmd failed" << endl; - return 0; - } + writeCmd(cmd); // wait for the device to report ready waitforDeviceReady(); // now read the temperature - if (!writeCmd(CMD_READ_T)) - { - cerr << __FUNCTION__ << ": writeCmd failed" << endl; - return 0; - } + writeCmd(CMD_READ_T); return ((float)readData() / 100.0); } @@ -186,21 +182,13 @@ float HP20X::getPressure() // start conversion, PT only uint8_t cmd = CMD_ADC_CVT | (CHNL_PT << CHNL_SHIFT) | (m_dsr << DSR_SHIFT); - if (!writeCmd(cmd)) - { - cerr << __FUNCTION__ << ": writeCmd failed" << endl; - return 0; - } + writeCmd(cmd); // wait for the device to report ready waitforDeviceReady(); // now read the pressure - if (!writeCmd(CMD_READ_P)) - { - cerr << __FUNCTION__ << ": writeCmd failed" << endl; - return 0; - } + writeCmd(CMD_READ_P); return ((float)readData() / 100.0); } @@ -212,21 +200,13 @@ float HP20X::getAltitude() // start conversion, PT only uint8_t cmd = CMD_ADC_CVT | (CHNL_PT << CHNL_SHIFT) | (m_dsr << DSR_SHIFT); - if (!writeCmd(cmd)) - { - cerr << __FUNCTION__ << ": writeCmd failed" << endl; - return 0; - } + writeCmd(cmd); // wait for the device to report ready waitforDeviceReady(); // now read the pressure - if (!writeCmd(CMD_READ_A)) - { - cerr << __FUNCTION__ << ": writeCmd failed" << endl; - return 0; - } + writeCmd(CMD_READ_A); return ((float)readData() / 100.0); }