mirror of
https://github.com/eclipse/upm.git
synced 2025-03-24 01:10:22 +03:00
hp20x: 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:
parent
fb779ecd6a
commit
9421cdaf30
@ -26,6 +26,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "hp20x.h"
|
#include "hp20x.h"
|
||||||
|
|
||||||
@ -41,8 +42,8 @@ HP20X::HP20X(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 << "HP20X: Could not initialize i2c address. " << endl;
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||||
printError(rv);
|
": I2c.address() failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,8 +91,8 @@ bool HP20X::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;
|
||||||
}
|
}
|
||||||
@ -101,7 +102,8 @@ bool HP20X::writeCmd(uint8_t cmd)
|
|||||||
mraa::Result rv;
|
mraa::Result rv;
|
||||||
if ((rv = m_i2c.writeByte(cmd)) != mraa::SUCCESS)
|
if ((rv = m_i2c.writeByte(cmd)) != mraa::SUCCESS)
|
||||||
{
|
{
|
||||||
printError(rv);
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
|
": I2c.writeByte() failed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +119,8 @@ bool HP20X::writeReg(HP20X_REG_T reg, uint8_t data)
|
|||||||
mraa::Result rv;
|
mraa::Result rv;
|
||||||
if ((rv = m_i2c.writeReg(r, data)) != mraa::SUCCESS)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +140,8 @@ int HP20X::readData()
|
|||||||
|
|
||||||
if (!m_i2c.read(buf, 3))
|
if (!m_i2c.read(buf, 3))
|
||||||
{
|
{
|
||||||
cerr << __FUNCTION__ << ": read failed" << endl;
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
|
": I2c.read() failed");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,21 +164,13 @@ float HP20X::getTemperature()
|
|||||||
|
|
||||||
// start conversion, T only
|
// start conversion, T only
|
||||||
uint8_t cmd = CMD_ADC_CVT | (CHNL_T << CHNL_SHIFT) | (m_dsr << DSR_SHIFT);
|
uint8_t cmd = CMD_ADC_CVT | (CHNL_T << CHNL_SHIFT) | (m_dsr << DSR_SHIFT);
|
||||||
if (!writeCmd(cmd))
|
writeCmd(cmd);
|
||||||
{
|
|
||||||
cerr << __FUNCTION__ << ": writeCmd failed" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for the device to report ready
|
// wait for the device to report ready
|
||||||
waitforDeviceReady();
|
waitforDeviceReady();
|
||||||
|
|
||||||
// now read the temperature
|
// now read the temperature
|
||||||
if (!writeCmd(CMD_READ_T))
|
writeCmd(CMD_READ_T);
|
||||||
{
|
|
||||||
cerr << __FUNCTION__ << ": writeCmd failed" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ((float)readData() / 100.0);
|
return ((float)readData() / 100.0);
|
||||||
}
|
}
|
||||||
@ -186,21 +182,13 @@ float HP20X::getPressure()
|
|||||||
|
|
||||||
// start conversion, PT only
|
// start conversion, PT only
|
||||||
uint8_t cmd = CMD_ADC_CVT | (CHNL_PT << CHNL_SHIFT) | (m_dsr << DSR_SHIFT);
|
uint8_t cmd = CMD_ADC_CVT | (CHNL_PT << CHNL_SHIFT) | (m_dsr << DSR_SHIFT);
|
||||||
if (!writeCmd(cmd))
|
writeCmd(cmd);
|
||||||
{
|
|
||||||
cerr << __FUNCTION__ << ": writeCmd failed" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for the device to report ready
|
// wait for the device to report ready
|
||||||
waitforDeviceReady();
|
waitforDeviceReady();
|
||||||
|
|
||||||
// now read the pressure
|
// now read the pressure
|
||||||
if (!writeCmd(CMD_READ_P))
|
writeCmd(CMD_READ_P);
|
||||||
{
|
|
||||||
cerr << __FUNCTION__ << ": writeCmd failed" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ((float)readData() / 100.0);
|
return ((float)readData() / 100.0);
|
||||||
}
|
}
|
||||||
@ -212,21 +200,13 @@ float HP20X::getAltitude()
|
|||||||
|
|
||||||
// start conversion, PT only
|
// start conversion, PT only
|
||||||
uint8_t cmd = CMD_ADC_CVT | (CHNL_PT << CHNL_SHIFT) | (m_dsr << DSR_SHIFT);
|
uint8_t cmd = CMD_ADC_CVT | (CHNL_PT << CHNL_SHIFT) | (m_dsr << DSR_SHIFT);
|
||||||
if (!writeCmd(cmd))
|
writeCmd(cmd);
|
||||||
{
|
|
||||||
cerr << __FUNCTION__ << ": writeCmd failed" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for the device to report ready
|
// wait for the device to report ready
|
||||||
waitforDeviceReady();
|
waitforDeviceReady();
|
||||||
|
|
||||||
// now read the pressure
|
// now read the pressure
|
||||||
if (!writeCmd(CMD_READ_A))
|
writeCmd(CMD_READ_A);
|
||||||
{
|
|
||||||
cerr << __FUNCTION__ << ": writeCmd failed" << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ((float)readData() / 100.0);
|
return ((float)readData() / 100.0);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user