From 0b74aa68ab9ec8767d46676ddbe060130eed5623 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Fri, 22 Apr 2016 11:57:18 +0100 Subject: [PATCH] aio: Change mraa_aio_read to use int and return -1 This commit changes also the _replace function and adds exceptions to the C++ API for errors in AIO read Signed-off-by: Brendan Le Foll --- api/mraa/aio.h | 11 ++++++----- api/mraa/aio.hpp | 16 +++++++++++++--- include/mraa_adv_func.h | 2 +- src/aio/aio.c | 8 +++++--- src/firmata/firmata_mraa.c | 4 ++-- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/api/mraa/aio.h b/api/mraa/aio.h index 753c907..23c0857 100644 --- a/api/mraa/aio.h +++ b/api/mraa/aio.h @@ -59,19 +59,20 @@ typedef struct _aio* mraa_aio_context; mraa_aio_context mraa_aio_init(unsigned int pin); /** - * Read the input voltage. By default mraa will shift - * the raw value up or down to a 10 bit value. + * Read the input voltage. By default mraa will shift the raw value up or down + * to a 10 bit value. * * @param dev The AIO context - * @returns The current input voltage. + * @returns The current input voltage or -1 for error */ -unsigned int mraa_aio_read(mraa_aio_context dev); +int mraa_aio_read(mraa_aio_context dev); /** * Read the input voltage and return it as a normalized float (0.0f-1.0f). * * @param dev The AIO context - * @returns The current input voltage as a normalized float (0.0f-1.0f) + * @returns The current input voltage as a normalized float (0.0f-1.0f), error + * will be signaled by -1.0f */ float mraa_aio_read_float(mraa_aio_context dev); diff --git a/api/mraa/aio.hpp b/api/mraa/aio.hpp index 9b07854..acb1522 100644 --- a/api/mraa/aio.hpp +++ b/api/mraa/aio.hpp @@ -65,22 +65,32 @@ class Aio * Read a value from the AIO pin. By default mraa will shift * the raw value up or down to a 10 bit value. * + * @throws std::invalid_argument in case of error * @returns The current input voltage. By default, a 10bit value */ - int + unsigned int read() { - return mraa_aio_read(m_aio); + int x = mraa_aio_read(m_aio); + if (x == -1) { + throw std::invalid_argument("Unknown error in Aio::read()"); + } + return (unsigned int) x; } /** * Read a value from the AIO pin and return it as a normalized float. * + * @throws std::invalid_argument in case of error * @returns The current input voltage as a normalized float (0.0f-1.0f) */ float readFloat() { - return mraa_aio_read_float(m_aio); + float x = mraa_aio_read_float(m_aio); + if (x == -1.0f) { + throw std::invalid_argument("Unknown error in Aio::readFloat()"); + } + return x; } /** * Set the bit value which mraa will shift the raw reading diff --git a/include/mraa_adv_func.h b/include/mraa_adv_func.h index 202b165..a8eb804 100644 --- a/include/mraa_adv_func.h +++ b/include/mraa_adv_func.h @@ -75,7 +75,7 @@ typedef struct { mraa_result_t (*i2c_stop_replace) (mraa_i2c_context dev); mraa_result_t (*aio_init_internal_replace) (mraa_aio_context dev, int pin); - mraa_result_t (*aio_read_replace) (mraa_aio_context dev); + int (*aio_read_replace) (mraa_aio_context dev); mraa_result_t (*aio_get_valid_fp) (mraa_aio_context dev); mraa_result_t (*aio_init_pre) (unsigned int aio); mraa_result_t (*aio_init_post) (mraa_aio_context dev); diff --git a/src/aio/aio.c b/src/aio/aio.c index 5335b5e..8ebf7c0 100644 --- a/src/aio/aio.c +++ b/src/aio/aio.c @@ -152,7 +152,7 @@ mraa_aio_init(unsigned int aio) return dev; } -unsigned int +int mraa_aio_read(mraa_aio_context dev) { if (IS_FUNC_DEFINED(dev, aio_read_replace)) { @@ -165,7 +165,7 @@ mraa_aio_read(mraa_aio_context dev) if (dev->adc_in_fp == -1) { if (aio_get_valid_fp(dev) != MRAA_SUCCESS) { syslog(LOG_ERR, "aio: Failed to get to the device"); - return 0; + return -1; } } @@ -182,8 +182,10 @@ mraa_aio_read(mraa_aio_context dev) unsigned int analog_value = (unsigned int) strtoul(buffer, &end, 10); if (end == &buffer[0]) { syslog(LOG_ERR, "aio: Value is not a decimal number"); + return -1; } else if (errno != 0) { syslog(LOG_ERR, "aio: Errno was set"); + return -1; } if (dev->value_bit != raw_bits) { @@ -205,7 +207,7 @@ mraa_aio_read_float(mraa_aio_context dev) { if (dev == NULL) { syslog(LOG_ERR, "aio: Device not valid"); - return 0.0; + return -1.0; } float max_analog_value = (1 << dev->value_bit) - 1; diff --git a/src/firmata/firmata_mraa.c b/src/firmata/firmata_mraa.c index 55c5ca5..9e173b9 100644 --- a/src/firmata/firmata_mraa.c +++ b/src/firmata/firmata_mraa.c @@ -340,12 +340,12 @@ mraa_firmata_i2c_stop(mraa_i2c_context dev) return MRAA_SUCCESS; } -static unsigned int +static int mraa_firmata_aio_read(mraa_aio_context dev) { // careful, whilst you need to enable '0' for A0 you then need to read 14 // in t_firmata because well that makes sense doesn't it... - return (unsigned int) firmata_dev->pins[dev->channel].value; + return (int) firmata_dev->pins[dev->channel].value; } static mraa_result_t