95c01ebb4b18a1e4d9dacdb0034ff675be58ad21
[contrib/mraa.git] / api / mraa / pwm.hpp
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #pragma once
26
27 #include "pwm.h"
28 #include <stdexcept>
29
30 namespace mraa {
31
32 /**
33  * @brief API to Pulse Width Modulation
34  *
35  * This file defines the PWM interface for libmraa
36  *
37  * @snippet Pwm3-cycle.cpp Interesting
38  */
39 class Pwm {
40     public:
41         /**
42          * instanciates a PWM object on a pin
43          *
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
49          */
50         Pwm(int pin, bool owner=true, int chipid=-1) {
51             if (chipid == -1) {
52                 m_pwm = mraa_pwm_init(pin);
53             }
54             else {
55                 m_pwm = mraa_pwm_init_raw(chipid, pin);
56             }
57
58             if (m_pwm == NULL) {
59                 throw std::invalid_argument("Error initialising PWM on pin");
60             }
61
62             if (!owner) {
63                 mraa_pwm_owner(m_pwm, 0);
64             }
65         }
66         /**
67          * Pwm destructor
68          */
69         ~Pwm() {
70             mraa_pwm_close(m_pwm);
71         }
72         /**
73          * Set the output duty-cycle percentage, as a float
74          *
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
78          * 1.0f
79          * @return Result of operation
80          */
81         mraa_result_t write(float percentage) {
82             return mraa_pwm_write(m_pwm, percentage);
83         }
84         /**
85          * Read the ouput duty-cycle percentage, as a float
86          *
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
90          * 1.0f
91          */
92         float read() {
93             return mraa_pwm_read(m_pwm);
94         }
95         /**
96          * Set the PWM period as seconds represented in a float
97          *
98          * @param period Period represented as a float in seconds
99          * @return Result of operation
100          */
101         mraa_result_t period(float period) {
102             return mraa_pwm_period(m_pwm, period);
103         }
104         /**
105          * Set period, milliseconds
106          *
107          * @param ms milliseconds for period
108          * @return Result of operation
109          */
110         mraa_result_t period_ms(int ms) {
111             return mraa_pwm_period_ms(m_pwm, ms);
112         }
113         /**
114          * Set period, microseconds
115          *
116          * @param us microseconds as period
117          * @return Result of operation
118          */
119         mraa_result_t period_us(int us) {
120             return mraa_pwm_period_us(m_pwm, us);
121         }
122         /**
123          * Set pulsewidth, As represnted by seconds in a (float)
124          *
125          * @param seconds The duration of a pulse
126          * @return Result of operation
127          */
128         mraa_result_t pulsewidth(float seconds) {
129             return mraa_pwm_pulsewidth(m_pwm, seconds);
130         }
131         /**
132          * Set pulsewidth, milliseconds
133          *
134          * @param ms milliseconds for pulsewidth
135          * @return Result of operation
136          */
137         mraa_result_t pulsewidth_ms(int ms) {
138             return mraa_pwm_pulsewidth_ms(m_pwm, ms);
139         }
140         /**
141          * The pulsewidth, microseconds
142          *
143          * @param us microseconds for pulsewidth
144          * @return Result of operation
145          */
146         mraa_result_t pulsewidth_us(int us) {
147             return mraa_pwm_pulsewidth_us(m_pwm, us);
148         }
149         /**
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
152          *
153          * @param enable enable status of pin
154          * @return Result of operation
155          */
156         mraa_result_t enable(bool enable) {
157             if (enable)
158                 return mraa_pwm_enable(m_pwm, 1);
159             else
160                 return mraa_pwm_enable(m_pwm, 0);
161         }
162         /**
163          * Set the period and duty of a PWM object.
164          *
165          * @param period represented in ms.
166          * @param duty represnted in ms as float.
167          * @return Result of operation
168          */
169         mraa_result_t config_ms(int period, float duty) {
170             return mraa_pwm_config_ms(m_pwm, period, duty);
171         }
172         /**
173          * Set the period and duty (percent) of a PWM object.
174          *
175          * @param period as represented in ms.
176          * @param duty percentage i.e. 50% = 0.5f
177          * @return Result of operation
178          */
179         mraa_result_t config_percent(int period, float duty) {
180             return mraa_pwm_config_percent(m_pwm, period, duty);
181         }
182
183     private:
184         mraa_pwm_context m_pwm;
185 };
186
187 }