diff --git a/src/adc121c021/adc121c021.cxx b/src/adc121c021/adc121c021.cxx index 5c5438a4..41808b9a 100644 --- a/src/adc121c021/adc121c021.cxx +++ b/src/adc121c021/adc121c021.cxx @@ -79,12 +79,20 @@ mraa_result_t ADC121C021::writeWord(uint8_t reg, uint16_t word) uint8_t ADC121C021::readByte(uint8_t reg) { - return mraa_i2c_read_byte_data(m_i2c, reg); + int val = mraa_i2c_read_byte_data(m_i2c, reg); + if (val != -1) { + return (uint8_t) val; + } + return 0; } uint16_t ADC121C021::readWord(uint8_t reg) { - uint16_t val = mraa_i2c_read_word_data(m_i2c, reg); + int x = mraa_i2c_read_word_data(m_i2c, reg); + if (x == -1) { + return 0; + } + uint16_t val = (uint16_t) x; uint8_t b1; // The value returned is in the wrong byte order, so we need to swap them diff --git a/src/ecs1030/ecs1030.cxx b/src/ecs1030/ecs1030.cxx index 4db49fab..90dd60a7 100644 --- a/src/ecs1030/ecs1030.cxx +++ b/src/ecs1030/ecs1030.cxx @@ -59,6 +59,8 @@ ECS1030::getCurrency_A () { for (int i = 0; i < NUMBER_OF_SAMPLES; i++) { sensorValue = mraa_aio_read (m_dataPinCtx); + if (sensorValue == -1) throw std::runtime_error(std::string(__FUNCTION__) + + ": Failed to do an aio read."); volt = (VOLT_M * sensorValue) - 2.5; volt = volt * volt; rms = rms + volt; @@ -77,6 +79,8 @@ ECS1030::getCurrency_B () { for (int i = 0; i < NUMBER_OF_SAMPLES; i++) { m_lastSample = m_sample; m_sample = mraa_aio_read (m_dataPinCtx); + if (m_sample == -1) throw std::runtime_error(std::string(__FUNCTION__) + + ": Failed to do an aio read."); m_lastFilter = m_filteredSample; m_filteredSample = 0.996 * (m_lastFilter + m_sample - m_lastSample); sumCurrency += (m_filteredSample * m_filteredSample); diff --git a/src/enc03r/enc03r.cxx b/src/enc03r/enc03r.cxx index ee2da1ea..7dc4f65f 100644 --- a/src/enc03r/enc03r.cxx +++ b/src/enc03r/enc03r.cxx @@ -51,7 +51,10 @@ ENC03R::~ENC03R() unsigned int ENC03R::value() { - return mraa_aio_read(m_aio); + int x = mraa_aio_read(m_aio); + if (x == -1) throw std::out_of_range(std::string(__FUNCTION__) + + ": Failed to do an aio read."); + return (unsigned int) x; } void ENC03R::calibrate(unsigned int samples) @@ -62,6 +65,8 @@ void ENC03R::calibrate(unsigned int samples) for (int i=0; i hiVal) + if (val == -1) { + return -1; + } + if (val > hiVal) { hiVal = val; + } } return hiVal; diff --git a/src/ta12200/ta12200.hpp b/src/ta12200/ta12200.hpp index b3561d48..cc025e0c 100644 --- a/src/ta12200/ta12200.hpp +++ b/src/ta12200/ta12200.hpp @@ -89,9 +89,9 @@ namespace upm { /** * Gets the conversion value from the sensor * - * @return Highest value obtained over 1 second of measuring + * @return Highest value obtained over 1 second of measuring or -1 if error */ - unsigned int highestValue(); + int highestValue(); /** * Computes the measured voltage