diff --git a/api/mraa/aio.h b/api/mraa/aio.h index ee30fb0..753c907 100644 --- a/api/mraa/aio.h +++ b/api/mraa/aio.h @@ -67,6 +67,14 @@ mraa_aio_context mraa_aio_init(unsigned int pin); */ unsigned 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) + */ +float mraa_aio_read_float(mraa_aio_context dev); + /** * Close the analog input context, this will free the memory for the context * diff --git a/api/mraa/aio.hpp b/api/mraa/aio.hpp index 27c6a64..d061f9a 100644 --- a/api/mraa/aio.hpp +++ b/api/mraa/aio.hpp @@ -65,6 +65,14 @@ class Aio { int read() { return mraa_aio_read(m_aio); } + /** + * Read a value from the AIO pin and return it as a normalized float. + * + * @returns The current input voltage as a normalized float (0.0f-1.0f) + */ + float readFloat() { + return mraa_aio_read_float(m_aio); + } /** * Set the bit value which mraa will shift the raw reading * from the ADC to. I.e. 10bits diff --git a/src/aio/aio.c b/src/aio/aio.c index 7d08ccc..83914dd 100644 --- a/src/aio/aio.c +++ b/src/aio/aio.c @@ -159,6 +159,20 @@ mraa_aio_read(mraa_aio_context dev) return analog_value; } +float +mraa_aio_read_float(mraa_aio_context dev) +{ + if (dev == NULL) { + syslog(LOG_ERR, "aio: Device not valid"); + return 0.0; + } + + float max_analog_value = (1 << dev->value_bit) - 1; + unsigned int analog_value_int = mraa_aio_read(dev); + + return analog_value_int / max_analog_value; +} + mraa_result_t mraa_aio_close(mraa_aio_context dev) {