mraa_result_t mraa_pwm_config_percent(mraa_pwm_context dev, int ms, float percentage) { int old_dutycycle, old_period, status; if (!dev) { syslog(LOG_ERR, "pwm: config_percent: context is NULL"); return MRAA_ERROR_INVALID_HANDLE; } old_dutycycle = mraa_pwm_read_duty(dev); old_period = mraa_pwm_read_period(dev); status = mraa_pwm_period_us(dev, ms * 1000); if (status != MRAA_SUCCESS) { mraa_pwm_write_duty(dev, old_dutycycle); return status; } status = mraa_pwm_write_duty(dev, 0); if (status != MRAA_SUCCESS) { return status; } status = mraa_pwm_pulsewidth_us(dev, (ms * 1000) * percentage); if (status != MRAA_SUCCESS) { mraa_pwm_write_duty(dev, old_dutycycle); mraa_pwm_write_period(dev, old_period); return status; } return MRAA_SUCCESS; }
mraa_result_t mraa_pwm_period_us(mraa_pwm_context dev, int us) { if (us < plat->pwm_min_period || us > plat->pwm_max_period) { syslog(LOG_ERR, "pwm: period value outside platform range"); return MRAA_ERROR_INVALID_PARAMETER; } return mraa_pwm_write_period(dev, us * 1000); }
mraa_result_t mraa_pwm_period_us(mraa_pwm_context dev, int us) { int min, max; if (mraa_is_sub_platform_id(dev->chipid)) { min = plat->sub_platform->pwm_min_period; max = plat->sub_platform->pwm_max_period; } else { min = plat->pwm_min_period; max = plat->pwm_max_period; } if (us < min || us > max) { syslog(LOG_ERR, "pwm: period value outside platform range"); return MRAA_ERROR_INVALID_PARAMETER; } return mraa_pwm_write_period(dev, us * 1000); }
mraa_result_t mraa_pwm_config_percent(mraa_pwm_context dev, int ms ,float percentage) { int old_dutycycle, old_period, status; old_dutycycle = mraa_pwm_read_duty(dev); old_period = mraa_pwm_read_period(dev); status = mraa_pwm_write_duty(dev, 0); if (status != MRAA_SUCCESS) { return status; } status = mraa_pwm_period_us(dev, ms*1000); if (status != MRAA_SUCCESS) { mraa_pwm_write_duty(dev, old_dutycycle); return status; } status = mraa_pwm_pulsewidth_us(dev, (ms*1000)*percentage); if (status != MRAA_SUCCESS) { mraa_pwm_write_duty(dev, old_dutycycle); mraa_pwm_write_period(dev, old_period); return status; } return MRAA_SUCCESS; }