static mraa_aio_context mraa_aio_init_internal(mraa_adv_func_t* func_table, int aio) { mraa_aio_context dev = calloc(1, sizeof(struct _aio)); if (dev == NULL) { return NULL; } dev->advance_func = func_table; if (IS_FUNC_DEFINED(dev, aio_init_internal_replace)) { if (dev->advance_func->aio_init_internal_replace(dev, aio) == MRAA_SUCCESS) { return dev; } free(dev); return NULL; } // Open valid analog input file and get the pointer. if (MRAA_SUCCESS != aio_get_valid_fp(dev)) { free(dev); return NULL; } return dev; }
int mraa_aio_read(mraa_aio_context dev) { if (dev == NULL) { syslog(LOG_ERR, "aio: read: context is invalid"); return -1; } if (IS_FUNC_DEFINED(dev, aio_read_replace)) { return dev->advance_func->aio_read_replace(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"); return -1; } } lseek(dev->adc_in_fp, 0, SEEK_SET); if (read(dev->adc_in_fp, buffer, sizeof(buffer)) < 1) { syslog(LOG_ERR, "aio: Failed to read a sensible value"); } // force NULL termination of string buffer[16] = '\0'; lseek(dev->adc_in_fp, 0, SEEK_SET); errno = 0; char* end; unsigned int analog_value = (unsigned int) strtoul(buffer, &end, 10); if (end == &buffer[0]) { syslog(LOG_ERR, "aio: Value is not a decimal number"); return -1; } else if (errno != 0) { syslog(LOG_ERR, "aio: Errno was set"); return -1; } /* Adjust the raw analog input reading to supported resolution value*/ if (raw_bits < dev->value_bit) { analog_value = analog_value << shifter_value; } else { analog_value = analog_value >> shifter_value; } return analog_value; }
unsigned int mraa_aio_read(mraa_aio_context dev) { char buffer[17]; unsigned int shifter_value = 0; if (dev->adc_in_fp == -1) { if (aio_get_valid_fp(dev) != MRAA_SUCCESS) { syslog(LOG_ERR, "aio: Failed to get to the device"); return 0; } } lseek(dev->adc_in_fp, 0, SEEK_SET); if (read(dev->adc_in_fp, buffer, sizeof(buffer)) < 1) { syslog(LOG_ERR, "aio: Failed to read a sensible value"); } // force NULL termination of string buffer[16] = '\0'; lseek(dev->adc_in_fp, 0, SEEK_SET); errno = 0; char* end; unsigned int analog_value = (unsigned int) strtoul(buffer, &end, 10); if (end == &buffer[0]) { syslog(LOG_ERR, "aio: Value is not a decimal number"); } else if (errno != 0) { syslog(LOG_ERR, "aio: Errno was set"); } 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 {
mraa_aio_context mraa_aio_init(unsigned int aio) { if (plat == NULL) { syslog(LOG_ERR, "aio: Platform not initialised"); return NULL; } if (mraa_is_sub_platform_id(aio)) { syslog(LOG_NOTICE, "aio: Using sub platform is not supported"); return NULL; } // Create ADC device connected to specified channel mraa_aio_context dev = mraa_aio_init_internal(plat->adv_func); if (dev == NULL) { syslog(LOG_ERR, "aio: Insufficient memory for specified input channel %d", aio); return NULL; } int pin = aio + plat->gpio_count; dev->channel = plat->pins[pin].aio.pinmap; dev->value_bit = DEFAULT_BITS; if (IS_FUNC_DEFINED(dev, aio_init_pre)) { mraa_result_t pre_ret = (dev->advance_func->aio_init_pre(aio)); if (pre_ret != MRAA_SUCCESS) { free(dev); return NULL; } } if (aio > plat->aio_count) { syslog(LOG_ERR, "aio: requested channel out of range"); free(dev); return NULL; } if (plat->pins[pin].capabilites.aio != 1) { syslog(LOG_ERR, "aio: pin uncapable of aio"); free(dev); return NULL; } if (plat->pins[pin].aio.mux_total > 0) { if (mraa_setup_mux_mapped(plat->pins[pin].aio) != MRAA_SUCCESS) { free(dev); syslog(LOG_ERR, "aio: unable to setup multiplexers for pin"); return NULL; } } // Open valid analog input file and get the pointer. if (MRAA_SUCCESS != aio_get_valid_fp(dev)) { free(dev); return NULL; } raw_bits = mraa_adc_raw_bits(); if (IS_FUNC_DEFINED(dev, aio_init_post)) { mraa_result_t ret = dev->advance_func->aio_init_post(dev); if (ret != MRAA_SUCCESS) { free(dev); return NULL; } } return dev; }