mraa_iio_context
mraa_iio_init(int device)
{
- mraa_iio_context dev = (mraa_iio_context) calloc(1, sizeof(struct _iio));
- if (dev == NULL) {
- syslog(LOG_CRIT, "iio: Failed to allocate memory for context");
+ if (device > plat->iio_device_count) {
return NULL;
}
+ return &plat->iio_devices[device];
+}
- return dev;
+int
+mraa_iio_get_channel_count(mraa_iio_context dev)
+{
+ return 1;
}
+mraa_result_t
+mraa_iio_read(mraa_iio_context dev, int channel, const char* attribute, float* data)
+{
+ char buf[64];
+ snprintf(buf, 64, "/sys/bus/iio/devices/iio:device%d/in_voltage%d_%s", dev->num, channel, attribute);
+ int fd = open(buf, O_RDONLY);
+ if (fd != -1) {
+ int len = read(fd, &buf, 64);
+ *data = strtol(buf, NULL, 10);
+ return MRAA_SUCCESS;
+ }
+ return MRAA_ERROR_UNSPECIFIED;
+}
mraa_result_t
-mraa_iio_read(mraa_iio_context dev, uint32_t* data, int length)
+mraa_iio_write(mraa_iio_context dev, int channel, const char* attribute)
{
- return 0;
+ return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
}
mraa_result_t
static char platform_name[MAX_PLATFORM_NAME_LENGTH];
mraa_adv_func_t* advance_func;
+static int num_i2c_devices = 0;
+static int num_iio_devices = 0;
+
const char*
mraa_get_version()
{
return MRAA_ERROR_INVALID_PARAMETER;
}
+static int
+mraa_count_iio_devices(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
+{
+ switch (sb->st_mode & S_IFMT) {
+ case S_IFLNK:
+ num_iio_devices++;
+ break;
+ }
+ return 0;
+}
+
#if (defined SWIGPYTHON) || (defined SWIG)
mraa_result_t
#else
}
#endif
+ // Now detect IIO devices, linux only
+ // find how many i2c buses we have if we haven't already
+ if (num_iio_devices == 0) {
+ if (nftw("/sys/bus/iio/devices", &mraa_count_iio_devices, 20, FTW_PHYS) == -1) {
+ return MRAA_ERROR_UNSPECIFIED;
+ }
+ }
+ char name[64], filepath[64];
+ int fd, len, i;
+ plat->iio_device_count = num_iio_devices;
+ plat->iio_devices = calloc(num_iio_devices, sizeof(struct _iio));
+ struct _iio* device;
+ for (i=0; i < num_iio_devices; i++) {
+ device = &plat->iio_devices[i];
+ device->num = i;
+ snprintf(filepath, 64, "/sys/bus/iio/devices/iio:device%d", i);
+ fd = open(filepath, O_RDONLY);
+ if (fd != -1) {
+ len = read(fd, &name, 64);
+ device->name = malloc((sizeof(char) * len) + sizeof(char));
+ strncpy(device->name, name, len);
+ }
+ }
+
syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
return MRAA_SUCCESS;
}
}
}
-static int num_i2c_devices = 0;
-
static int
-mraa_count_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
+mraa_count_i2c_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb)
{
switch (sb->st_mode & S_IFMT) {
case S_IFLNK:
// find how many i2c buses we have if we haven't already
if (num_i2c_devices == 0) {
- if (nftw("/sys/class/i2c-dev/", &mraa_count_files, 20, FTW_PHYS) == -1) {
+ if (nftw("/sys/class/i2c-dev/", &mraa_count_i2c_files, 20, FTW_PHYS) == -1) {
return -1;
}
}
{
return pin_or_bus & (~MRAA_SUB_PLATFORM_MASK);
}
+
+int
+mraa_get_iio_device_count()
+{
+ return plat->iio_device_count;
+}
+
+int
+mraa_find_iio_device(const char* devicename)
+{
+ int i = 0;
+ for (i; i < plat->iio_device_count; i++) {
+#if 0
+ if (!strcmp() {
+ }
+#endif
+ }
+ return 0;
+}