From e51c5f3018adb76ddf45dbc39ed70e8de44efd6b Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Mon, 25 Apr 2016 14:29:41 +0100 Subject: [PATCH] This is a combination of 2 commits. aio: mraa_aio_read (v1.0.0) can now return -1, treat that in sensors using it Adds alot of exceptions if the aio read goes wrong Signed-off-by: Brendan Le Foll Signed-off-by: Noel Eck --- src/adc121c021/adc121c021.cxx | 12 ++++++++++-- src/ecs1030/ecs1030.cxx | 4 ++++ src/enc03r/enc03r.cxx | 7 ++++++- src/gp2y0a/gp2y0a.cxx | 2 ++ src/grove/grove.cxx | 6 +++++- src/groveemg/groveemg.cxx | 2 ++ src/grovegsr/grovegsr.cxx | 2 ++ src/groveo2/groveo2.cxx | 1 + src/grovevdiv/grovevdiv.cxx | 1 + src/guvas12d/guvas12d.cxx | 1 + src/ina132/ina132.cxx | 1 + src/joystick12/joystick12.cxx | 2 ++ src/maxsonarez/maxsonarez.cxx | 3 +++ src/mic/mic.cxx | 6 +++++- src/otp538u/otp538u.cxx | 9 +++++++++ src/ta12200/ta12200.cxx | 12 ++++++++---- src/ta12200/ta12200.hpp | 4 ++-- 17 files changed, 64 insertions(+), 11 deletions(-) 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