2 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 * @brief API to Pulse Width Modulation
35 * This file defines the PWM interface for libmraa
37 * @snippet Pwm3-cycle.cpp Interesting
42 * instanciates a PWM object on a pin
44 * @param pin the pin number used on your board
45 * @param owner if you are the owner of the pin the destructor will
46 * @param chipid the pwmchip to use, use only in raw mode
47 * unexport the pin from sysfs, default behaviour is you are the owner
48 * if the pinmapper exported it
50 Pwm(int pin, bool owner=true, int chipid=-1) {
52 m_pwm = mraa_pwm_init(pin);
55 m_pwm = mraa_pwm_init_raw(chipid, pin);
59 throw std::invalid_argument("Error initialising PWM on pin");
63 mraa_pwm_owner(m_pwm, 0);
70 mraa_pwm_close(m_pwm);
73 * Set the output duty-cycle percentage, as a float
75 * @param percentage A floating-point value representing percentage of
76 * output. The value should lie between 0.0f (representing on 0%) and
77 * 1.0f Values above or below this range will be set at either 0.0f or
79 * @return Result of operation
81 mraa_result_t write(float percentage) {
82 return mraa_pwm_write(m_pwm, percentage);
85 * Read the ouput duty-cycle percentage, as a float
87 * @return A floating-point value representing percentage of
88 * output. The value should lie between 0.0f (representing on 0%) and
89 * 1.0f Values above or below this range will be set at either 0.0f or
93 return mraa_pwm_read(m_pwm);
96 * Set the PWM period as seconds represented in a float
98 * @param period Period represented as a float in seconds
99 * @return Result of operation
101 mraa_result_t period(float period) {
102 return mraa_pwm_period(m_pwm, period);
105 * Set period, milliseconds
107 * @param ms milliseconds for period
108 * @return Result of operation
110 mraa_result_t period_ms(int ms) {
111 return mraa_pwm_period_ms(m_pwm, ms);
114 * Set period, microseconds
116 * @param us microseconds as period
117 * @return Result of operation
119 mraa_result_t period_us(int us) {
120 return mraa_pwm_period_us(m_pwm, us);
123 * Set pulsewidth, As represnted by seconds in a (float)
125 * @param seconds The duration of a pulse
126 * @return Result of operation
128 mraa_result_t pulsewidth(float seconds) {
129 return mraa_pwm_pulsewidth(m_pwm, seconds);
132 * Set pulsewidth, milliseconds
134 * @param ms milliseconds for pulsewidth
135 * @return Result of operation
137 mraa_result_t pulsewidth_ms(int ms) {
138 return mraa_pwm_pulsewidth_ms(m_pwm, ms);
141 * The pulsewidth, microseconds
143 * @param us microseconds for pulsewidth
144 * @return Result of operation
146 mraa_result_t pulsewidth_us(int us) {
147 return mraa_pwm_pulsewidth_us(m_pwm, us);
150 * Set the enable status of the PWM pin. None zero will assume on with
151 * output being driven and 0 will disable the output
153 * @param enable enable status of pin
154 * @return Result of operation
156 mraa_result_t enable(bool enable) {
158 return mraa_pwm_enable(m_pwm, 1);
160 return mraa_pwm_enable(m_pwm, 0);
163 * Set the period and duty of a PWM object.
165 * @param period represented in ms.
166 * @param duty represnted in ms as float.
167 * @return Result of operation
169 mraa_result_t config_ms(int period, float duty) {
170 return mraa_pwm_config_ms(m_pwm, period, duty);
173 * Set the period and duty (percent) of a PWM object.
175 * @param period as represented in ms.
176 * @param duty percentage i.e. 50% = 0.5f
177 * @return Result of operation
179 mraa_result_t config_percent(int period, float duty) {
180 return mraa_pwm_config_percent(m_pwm, period, duty);
184 mraa_pwm_context m_pwm;