Skip to content

Commit

Permalink
aio: Change mraa_aio_read to use int and return -1
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
arfoll committed Apr 22, 2016
1 parent e961558 commit 0b74aa6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
11 changes: 6 additions & 5 deletions api/mraa/aio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 13 additions & 3 deletions api/mraa/aio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/mraa_adv_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/aio/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
}

Expand All @@ -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) {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/firmata/firmata_mraa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0b74aa6

Please sign in to comment.