From 6a7add6b99fca6980cd718cff37a2d6093725e84 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Mon, 22 Sep 2014 14:22:17 +0100 Subject: [PATCH] aio: add calls for changing bit value of the read Signed-off-by: Thomas Ingleby --- api/mraa/aio.h | 18 ++++++++++++ api/mraa/aio.hpp | 18 ++++++++++++ include/mraa_internal_types.h | 1 + src/aio/aio.c | 52 ++++++++++++++++++++++++++++++----- 4 files changed, 82 insertions(+), 7 deletions(-) diff --git a/api/mraa/aio.h b/api/mraa/aio.h index e8eda8d..43ae226 100644 --- a/api/mraa/aio.h +++ b/api/mraa/aio.h @@ -74,6 +74,24 @@ uint16_t mraa_aio_read(mraa_aio_context dev); */ mraa_result_t mraa_aio_close(mraa_aio_context dev); +/** + * Set the bit value which mraa will shift the raw reading + * from the ADC to. I.e. 10bits + * @param dev the analog input context + * @param bits the bits the return from read should be i.e 10 + * + * @return mraa result type + */ +mraa_result_t mraa_aio_set_bit(mraa_aio_context dev, int bits); + +/** + * Gets the bit value mraa is shifting the analog read to. + * @param dev the analog input context + * + * @return bit value mraa is set return from the read function + */ +int mraa_aio_get_bit(mraa_aio_context dev); + #ifdef __cplusplus } #endif diff --git a/api/mraa/aio.hpp b/api/mraa/aio.hpp index 329a7e2..e136309 100644 --- a/api/mraa/aio.hpp +++ b/api/mraa/aio.hpp @@ -62,6 +62,24 @@ class Aio { // Use basic types to make swig code generation simpler return (int) mraa_aio_read(m_aio); } + /** + * Set the bit value which mraa will shift the raw reading + * from the ADC to. I.e. 10bits + * @param bits the bits the return from read should be i.e 10 + * @return mraa result type + */ + mraa_result_t set_bit(int bits) { + return mraa_aio_set_bit(m_aio, bits); + } + /** + * Gets the bit value mraa is shifting the analog read to. + * + * @return bit value mraa is set return from the read function + */ + int get_bit() { + return mraa_aio_get_bit(m_aio); + } + private: mraa_aio_context m_aio; }; diff --git a/include/mraa_internal_types.h b/include/mraa_internal_types.h index effd29c..e92c1f0 100644 --- a/include/mraa_internal_types.h +++ b/include/mraa_internal_types.h @@ -76,4 +76,5 @@ struct _pwm { struct _aio { unsigned int channel; /**< the channel as on board and ADC module */ int adc_in_fp; /**< File Pointer to raw sysfs */ + int value_bit; /**< 10 bits by default. Can be increased if board */ }; diff --git a/src/aio/aio.c b/src/aio/aio.c index aab97b7..f4d658a 100644 --- a/src/aio/aio.c +++ b/src/aio/aio.c @@ -29,8 +29,9 @@ #include "aio.h" #include "mraa_internal.h" +#define DEFAULT_BITS 10 + static int raw_bits; -static int sup_bits; static mraa_result_t aio_get_valid_fp(mraa_aio_context dev) { @@ -92,6 +93,7 @@ mraa_aio_context mraa_aio_init(unsigned int aio_channel) return NULL; } dev->channel = checked_pin; + dev->value_bit = DEFAULT_BITS; //Open valid analog input file and get the pointer. if (MRAA_SUCCESS != aio_get_valid_fp(dev)) { @@ -99,7 +101,6 @@ mraa_aio_context mraa_aio_init(unsigned int aio_channel) return NULL; } raw_bits = mraa_adc_raw_bits(); - sup_bits = mraa_adc_supported_bits(); if (advance_func->aio_init_post != NULL) { mraa_result_t ret = advance_func->aio_init_post(dev); @@ -146,13 +147,13 @@ uint16_t mraa_aio_read(mraa_aio_context dev) fprintf(stderr, "errno was set\n"); } - /* Adjust the raw analog input reading to supported resolution value*/ - if (raw_bits != sup_bits) { - if (raw_bits > sup_bits) { - shifter_value = raw_bits - sup_bits; + if (dev->value_bit != raw_bits) { + /* Adjust the raw analog input reading to supported resolution value*/ + if (raw_bits > dev->value_bit) { + shifter_value = raw_bits - dev->value_bit; analog_value = analog_value >> shifter_value; } else { - shifter_value = sup_bits - raw_bits; + shifter_value = dev->value_bit - raw_bits; analog_value = analog_value << shifter_value; } } @@ -173,3 +174,40 @@ mraa_result_t mraa_aio_close(mraa_aio_context dev) return(MRAA_SUCCESS); } + +/** Set the bits value from read. + * + * @param dev the analog input context + * @param bits the bits the return from read should be i.e 10 + * + * @return mraa result type + */ +mraa_result_t mraa_aio_set_bit(mraa_aio_context dev, int bits) +{ + if (dev == NULL) { + fprintf(stderr, "AIO Device not valid\n"); + return MRAA_ERROR_INVALID_RESOURCE; + } + if (bits < 1) { + fprintf(stderr, "AIO Device not valid\n"); + // Error Message Here. find with grep fprintf + return MRAA_ERROR_INVALID_PARAMETER; + } + dev->value_bit = bits; + return MRAA_SUCCESS; +} + +/** + * Gets the bit value mraa is shifting the analog read to. + * @param dev the analog input context + * + * @return bit value mraa is set return from the read function + */ +int mraa_aio_get_bit(mraa_aio_context dev) +{ + if (dev == NULL) { + fprintf(stderr, "AIO Device not valid\n"); + return 0; + } + return dev->value_bit; +}