From: Mark Ceisel Date: Mon, 1 Sep 2014 11:18:01 +0000 (+0100) Subject: pwm: add config_ms and config_percent. X-Git-Tag: v0.4.5~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9cf6cad45a36f1de5873194ab90450f9a37c1aa8;p=contrib%2Fmraa.git pwm: add config_ms and config_percent. * Allows user to set both at the same time. Will reset to previous if * period for duty cycle fails to write Signed-off-by: Thomas Ingleby --- diff --git a/api/mraa/pwm.h b/api/mraa/pwm.h index 9f32f05..bbc9049 100644 --- a/api/mraa/pwm.h +++ b/api/mraa/pwm.h @@ -166,6 +166,26 @@ mraa_result_t mraa_pwm_owner(mraa_pwm_context dev, mraa_boolean_t owner); */ mraa_result_t mraa_pwm_close(mraa_pwm_context dev); +/** + * Set Both Period and DutyCycle on a PWM context + * + * @param dev The pwm context to use + * @param period represented in ms. + * @param duty dutycycle of the pwm signal. + * @return Result of operation + */ +mraa_result_t mraa_pwm_config_ms(mraa_pwm_context dev, int period, float duty); + +/** + * Set Both Period and DutyCycle on a PWM context. Duty represented as percentage. + * + * @param dev The pwm context to use + * @param period represented in ms. + * @param duty duty percantage. i.e. 50% = 0.5f + * @return Result of operation + */ +mraa_result_t mraa_pwm_config_percent(mraa_pwm_context dev, int period, float duty); + #ifdef __cplusplus } #endif diff --git a/api/mraa/pwm.hpp b/api/mraa/pwm.hpp index 32eb87d..6762c98 100644 --- a/api/mraa/pwm.hpp +++ b/api/mraa/pwm.hpp @@ -150,6 +150,27 @@ class Pwm { else return mraa_pwm_enable(m_pwm, 0); } + /** + * Set the period and duty of a PWM object. + * + * @param period represented in ms. + * @param duty represnted in ms as float. + * @return Result of operation + */ + mraa_result_t config_ms(int period, float duty) { + return mraa_pwm_config_ms(m_pwm, period, duty); + } + /** + * Set the period and duty (percent) of a PWM object. + * + * @param period as represented in ms. + * @param duty percentage i.e. 50% = 0.5f + * @return Result of operation + */ + mraa_result_t config_percent(int period, float duty) { + return mraa_pwm_config_percent(m_pwm, period, duty); + } + private: mraa_pwm_context m_pwm; }; diff --git a/src/pwm/pwm.c b/src/pwm/pwm.c index 36df71c..b848dee 100644 --- a/src/pwm/pwm.c +++ b/src/pwm/pwm.c @@ -301,3 +301,51 @@ mraa_pwm_owner(mraa_pwm_context dev, mraa_boolean_t owner_new) dev->owner = owner_new; return MRAA_SUCCESS; } + +mraa_result_t +mraa_pwm_config_ms(mraa_pwm_context dev, int ms ,float ms_float) +{ + 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_float*1000); + 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_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; +}