Permalink
Browse files

aio: fix wrong max_analog_value and improve floating point value prec…

…ision

Signed-off-by: Nicola Lunghi <nicola.lunghi@emutex.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
  • Loading branch information...
Nicola Lunghi authored and arfoll committed Jan 4, 2018
1 parent 54c9c1a commit 64fe50ffa8c1947372a2773c4b1a0b55add82a65
Showing with 6 additions and 6 deletions.
  1. +6 −6 src/aio/aio.c
View
@@ -34,6 +34,7 @@
static int raw_bits;
static unsigned int shifter_value;
static float max_analog_value;
static mraa_result_t
aio_get_valid_fp(mraa_aio_context dev)
@@ -164,8 +165,10 @@ mraa_aio_init(unsigned int aio)
if (raw_bits < dev->value_bit) {
shifter_value = dev->value_bit - raw_bits;
max_analog_value = ((1 << raw_bits) - 1) << shifter_value;
} else {
shifter_value = raw_bits - dev->value_bit;
max_analog_value = ((1 << raw_bits) - 1) >> shifter_value;
}
return dev;
@@ -184,7 +187,6 @@ mraa_aio_read(mraa_aio_context dev)
}
char buffer[17];
if (dev->adc_in_fp == -1) {
if (aio_get_valid_fp(dev) != MRAA_SUCCESS) {
syslog(LOG_ERR, "aio: Failed to get to the device");
@@ -212,10 +214,10 @@ mraa_aio_read(mraa_aio_context dev)
}
/* Adjust the raw analog input reading to supported resolution value*/
if (raw_bits > dev->value_bit) {
analog_value = analog_value >> shifter_value;
if (raw_bits < dev->value_bit) {
analog_value = analog_value << shifter_value;
} else {
analog_value = analog_value << shifter_value;
analog_value = analog_value >> shifter_value;
}
return analog_value;
@@ -224,8 +226,6 @@ mraa_aio_read(mraa_aio_context dev)
float
mraa_aio_read_float(mraa_aio_context dev)
{
const float max_analog_value = ((1 << raw_bits) - 1) << (shifter_value);
if (dev == NULL) {
syslog(LOG_ERR, "aio: Device not valid");
return -1.0;

0 comments on commit 64fe50f

Please sign in to comment.