maa: change complex C++ write calls to CamelCase for spi & i2c
[contrib/mraa.git] / api / maa / 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
29 namespace maa {
30
31 /**
32  * @brief C++ API to Pulse Width Modulation
33  *
34  * This file defines the PWM C++ interface for libmaa
35  *
36  * @snippet Pwm3-cycle.cpp Interesting
37  */
38 class Pwm {
39     public:
40         /**
41          * instanciates a PWM object on a pin
42          *
43          * @param pin the pin number used on your board
44          * @param chipid the pwmchip to use, use only in raw mode
45          * @param owner if you are the owner of the pin the destructor will
46          * unexport the pin from sysfs, default behaviour is you are the owner
47          * if the pinmapper exported it
48          */
49         Pwm(int pin, int chipid=-1, bool owner = true) {
50             if (chipid == -1)
51                 m_pwm = maa_pwm_init(pin);
52             else
53                 m_pwm = maa_pwm_init_raw(pin, chipid);
54             if (!owner)
55                 maa_pwm_owner(m_pwm, 0);
56         }
57         /**
58          * Pwm destructor
59          */
60         ~Pwm() {
61             maa_pwm_close(m_pwm);
62         }
63         /**
64          * Set the output duty-cycle percentage, as a float
65          *
66          * @param percentage A floating-point value representing percentage of
67          * output. The value should lie between 0.0f (representing on 0%) and
68          * 1.0f Values above or below this range will be set at either 0.0f or
69          * 1.0f
70          * @return Result of operation
71          */
72         maa_result_t write(float percentage) {
73             return maa_pwm_write(m_pwm, percentage);
74         }
75         /**
76          * Read the ouput duty-cycle percentage, as a float
77          *
78          * @return A floating-point value representing percentage of
79          * output. The value should lie between 0.0f (representing on 0%) and
80          * 1.0f Values above or below this range will be set at either 0.0f or
81          * 1.0f
82          */
83         float read() {
84             return maa_pwm_read(m_pwm);
85         }
86         /**
87          * Set the PWM period as seconds represented in a float
88          *
89          * @param period Period represented as a float in seconds
90          * @return Result of operation
91          */
92         maa_result_t period(float period) {
93             return maa_pwm_period(m_pwm, period);
94         }
95         /**
96          * Set period, milliseconds
97          *
98          * @param ms milliseconds for period
99          * @return Result of operation
100          */
101         maa_result_t period_ms(int ms) {
102             return maa_pwm_period_ms(m_pwm, ms);
103         }
104         /**
105          * Set period, microseconds
106          *
107          * @param us microseconds as period
108          * @return Result of operation
109          */
110         maa_result_t period_us(int us) {
111             return maa_pwm_period_us(m_pwm, us);
112         }
113         /**
114          * Set pulsewidth, As represnted by seconds in a (float)
115          *
116          * @param seconds The duration of a pulse
117          * @return Result of operation
118          */
119         maa_result_t pulsewidth(float seconds) {
120             return maa_pwm_pulsewidth(m_pwm, seconds);
121         }
122         /**
123          * Set pulsewidth, milliseconds
124          *
125          * @param ms milliseconds for pulsewidth
126          * @return Result of operation
127          */
128         maa_result_t pulsewidth_ms(int ms) {
129             return maa_pwm_pulsewidth_ms(m_pwm, ms);
130         }
131         /**
132          * The pulsewidth, microseconds
133          *
134          * @param us microseconds for pulsewidth
135          * @return Result of operation
136          */
137         maa_result_t pulsewidth_us(int us) {
138             return maa_pwm_pulsewidth_us(m_pwm, us);
139         }
140         /**
141          * Set the enable status of the PWM pin. None zero will assume on with
142          * output being driven and 0 will disable the output
143          *
144          * @param enable enable status of pin
145          * @return Result of operation
146          */
147         maa_result_t enable(bool enable) {
148             if (enable)
149                 return maa_pwm_enable(m_pwm, 1);
150             else
151                 return maa_pwm_enable(m_pwm, 0);
152         }
153     private:
154         maa_pwm_context m_pwm;
155 };
156
157 }