From 3969af2b244f250245ae14e7f090fdd2d94e9858 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Mon, 16 May 2016 12:31:45 +0100 Subject: [PATCH] iio.c: Fix the location mapping in iio Go through channel array after getting byte mapping to correctly map channel location. Doing it at the same time as mapping the indexes is unreliable depending on filename etc... Signed-off-by: Brendan Le Foll --- src/iio/iio.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/iio/iio.c b/src/iio/iio.c index fd2c7f6..e38e307 100644 --- a/src/iio/iio.c +++ b/src/iio/iio.c @@ -83,6 +83,7 @@ mraa_iio_get_channel_data(mraa_iio_context dev) int padint = 0; int curr_bytes = 0; char shortbuf, signchar; + int i = 0; dev->datasize = 0; @@ -129,12 +130,6 @@ mraa_iio_get_channel_data(mraa_iio_context dev) ret = sscanf(readbuf, "%ce:%c%u/%u>>%u", &shortbuf, &signchar, &chan->bits_used, &padint, &chan->shift); chan->bytes = padint / 8; - if (curr_bytes % chan->bytes == 0) { - chan->location = curr_bytes; - } else { - chan->location = curr_bytes - curr_bytes % chan->bytes + chan->bytes; - } - curr_bytes = chan->location + chan->bytes; // probably should be 5? if (ret < 0) { // cleanup @@ -175,6 +170,18 @@ mraa_iio_get_channel_data(mraa_iio_context dev) } closedir(dir); + // channel location has to be done in channel index order so do it afetr we + // have grabbed all the correct info + for (i = 0; i < dev->chan_num; i++) { + chan = &dev->channels[i]; + if (curr_bytes % chan->bytes == 0) { + chan->location = curr_bytes; + } else { + chan->location = curr_bytes - curr_bytes % chan->bytes + chan->bytes; + } + curr_bytes = chan->location + chan->bytes; + } + return MRAA_SUCCESS; }