iio: Improve iio channel parsing to add enabled channels
scale and other attributes have to be read individually as they vary quite alot depending on the channel. We only care/take data from scan_elements Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
@@ -38,7 +38,6 @@ typedef struct {
|
|||||||
unsigned int bytes;
|
unsigned int bytes;
|
||||||
unsigned int shift;
|
unsigned int shift;
|
||||||
unsigned int location;
|
unsigned int location;
|
||||||
float scale;
|
|
||||||
} mraa_iio_channel;
|
} mraa_iio_channel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -29,15 +29,13 @@ static void
|
|||||||
printword(uint16_t input, mraa_iio_channel *chan)
|
printword(uint16_t input, mraa_iio_channel *chan)
|
||||||
{
|
{
|
||||||
int16_t res;
|
int16_t res;
|
||||||
|
|
||||||
if (!chan->lendian) {
|
if (!chan->lendian) {
|
||||||
input = be16toh(input);
|
input = be16toh(input);
|
||||||
} else {
|
} else {
|
||||||
input = le16toh(input);
|
input = le16toh(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
// currently we don't treat scales
|
|
||||||
chan->scale = 1.0f;
|
|
||||||
|
|
||||||
input >>= chan->shift;
|
input >>= chan->shift;
|
||||||
input &= chan->mask;
|
input &= chan->mask;
|
||||||
if (chan->signedd) {
|
if (chan->signedd) {
|
||||||
@@ -45,7 +43,7 @@ printword(uint16_t input, mraa_iio_channel *chan)
|
|||||||
} else {
|
} else {
|
||||||
res = input;
|
res = input;
|
||||||
}
|
}
|
||||||
printf("chan %d == %05f\n", chan->index, ((float)res + chan->offset) * chan->scale);
|
printf(" value = %05f\n", chan->index, (float)res);
|
||||||
}
|
}
|
||||||
|
|
||||||
mraa_iio_context iio_device0;
|
mraa_iio_context iio_device0;
|
||||||
@@ -57,13 +55,14 @@ interrupt(char* data)
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
for (i; i < mraa_iio_get_channel_count(iio_device0); i++) {
|
for (i; i < mraa_iio_get_channel_count(iio_device0); i++) {
|
||||||
printf("channel bytes %d\n", channels[i].bytes);
|
if (channels[i].enabled) {
|
||||||
|
printf("channel %d - bytes %d\n", channels[i].index, channels[i].bytes);
|
||||||
switch (channels[i].bytes) {
|
switch (channels[i].bytes) {
|
||||||
case 2:
|
case 2:
|
||||||
printword(*(uint16_t *)(data + channels[i].location), &channels[i]);
|
printword(*(uint16_t *)(data + channels[i].location), &channels[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("data %s\n", (char*) data);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ mraa_iio_get_channel_data(mraa_iio_context dev)
|
|||||||
|
|
||||||
buf[(strlen(buf)-5)] = '\0';
|
buf[(strlen(buf)-5)] = '\0';
|
||||||
char* str = strdup(buf);
|
char* str = strdup(buf);
|
||||||
|
// grab the type of the buffer
|
||||||
snprintf(buf, MAX_SIZE, "%stype", str);
|
snprintf(buf, MAX_SIZE, "%stype", str);
|
||||||
fd = open(buf, O_RDONLY);
|
fd = open(buf, O_RDONLY);
|
||||||
if (fd > 0) {
|
if (fd > 0) {
|
||||||
@@ -119,8 +120,11 @@ mraa_iio_get_channel_data(mraa_iio_context dev)
|
|||||||
chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
|
chan->location = curr_bytes - curr_bytes%chan->bytes + chan->bytes;
|
||||||
}
|
}
|
||||||
curr_bytes = chan->location + chan->bytes;
|
curr_bytes = chan->location + chan->bytes;
|
||||||
if (ret < 0) {
|
|
||||||
// probably should be 5?
|
// probably should be 5?
|
||||||
|
if (ret < 0) {
|
||||||
|
// cleanup
|
||||||
|
free(str);
|
||||||
|
close(fd);
|
||||||
return MRAA_IO_SETUP_FAILURE;
|
return MRAA_IO_SETUP_FAILURE;
|
||||||
}
|
}
|
||||||
chan->signedd = (signchar == 's');
|
chan->signedd = (signchar == 's');
|
||||||
@@ -130,10 +134,21 @@ mraa_iio_get_channel_data(mraa_iio_context dev)
|
|||||||
} else {
|
} else {
|
||||||
chan->mask = (1 << chan->bits_used) - 1;
|
chan->mask = (1 << chan->bits_used) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
// todo - read scale & _en attributes
|
// grab the enable flag of channel
|
||||||
|
snprintf(buf, MAX_SIZE, "%sen", str);
|
||||||
|
fd = open(buf, O_RDONLY);
|
||||||
|
if (fd > 0) {
|
||||||
|
if (read(fd, readbuf, 2 * sizeof(char)) != 2) {
|
||||||
|
syslog(LOG_ERR, "iio: Failed to read a sensible value from sysfs");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
chan->enabled = (int) strtol(readbuf, NULL, 10);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
// clean up str var
|
||||||
|
free(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,13 +173,14 @@ mraa_init()
|
|||||||
device->num = i;
|
device->num = i;
|
||||||
snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
|
snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d/name", i);
|
||||||
fd = open(filepath, O_RDONLY);
|
fd = open(filepath, O_RDONLY);
|
||||||
if (fd != -1) {
|
if (fd > 0) {
|
||||||
len = read(fd, &name, 64);
|
len = read(fd, &name, 64);
|
||||||
if (len > 1) {
|
if (len > 1) {
|
||||||
// use strndup
|
// use strndup
|
||||||
device->name = malloc((sizeof(char) * len) + sizeof(char));
|
device->name = malloc((sizeof(char) * len) + sizeof(char));
|
||||||
strncpy(device->name, name, len);
|
strncpy(device->name, name, len);
|
||||||
}
|
}
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user