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 "types.hpp"
29 #include <stdexcept>
30 
31 namespace mraa
32 {
33 
34 /**
35  * @brief API to Pulse Width Modulation
36  *
37  * This file defines the PWM interface for libmraa
38  *
39  * @snippet Pwm3-cycle.cpp Interesting
40  */
41 class Pwm
42 {
43   public:
44     /**
45      * instanciates a PWM object on a pin
46      *
47      * @param pin the pin number used on your board
48      * @param owner if you are the owner of the pin the destructor will
49      * @param chipid the pwmchip to use, use only in raw mode
50      * unexport the pin from sysfs, default behaviour is you are the owner
51      * if the pinmapper exported it
52      */
Pwm(int pin,bool owner=true,int chipid=-1)53     Pwm(int pin, bool owner = true, int chipid = -1)
54     {
55         if (chipid == -1) {
56             m_pwm = mraa_pwm_init(pin);
57         } else {
58             m_pwm = mraa_pwm_init_raw(chipid, pin);
59         }
60 
61         if (m_pwm == NULL) {
62             throw std::invalid_argument("Error initialising PWM on pin");
63         }
64 
65         if (!owner) {
66             mraa_pwm_owner(m_pwm, 0);
67         }
68     }
69     /**
70      * Pwm destructor
71      */
~Pwm()72     ~Pwm()
73     {
74         mraa_pwm_close(m_pwm);
75     }
76     /**
77      * Set the output duty-cycle percentage, as a float
78      *
79      * @param percentage A floating-point value representing percentage of
80      * output. The value should lie between 0.0f (representing on 0%) and
81      * 1.0f Values above or below this range will be set at either 0.0f or
82      * 1.0f
83      * @return Result of operation
84      */
85     Result
write(float percentage)86     write(float percentage)
87     {
88         return (Result) mraa_pwm_write(m_pwm, percentage);
89     }
90     /**
91      * Read the ouput duty-cycle percentage, as a float
92      *
93      * @return A floating-point value representing percentage of
94      * output. The value should lie between 0.0f (representing on 0%) and
95      * 1.0f Values above or below this range will be set at either 0.0f or
96      * 1.0f
97      */
98     float
read()99     read()
100     {
101         return mraa_pwm_read(m_pwm);
102     }
103     /**
104      * Set the PWM period as seconds represented in a float
105      *
106      * @param period Period represented as a float in seconds
107      * @return Result of operation
108      */
109     Result
period(float period)110     period(float period)
111     {
112         return (Result) mraa_pwm_period(m_pwm, period);
113     }
114     /**
115      * Set period, milliseconds
116      *
117      * @param ms milliseconds for period
118      * @return Result of operation
119      */
120     Result
period_ms(int ms)121     period_ms(int ms)
122     {
123         return (Result) mraa_pwm_period_ms(m_pwm, ms);
124     }
125     /**
126      * Set period, microseconds
127      *
128      * @param us microseconds as period
129      * @return Result of operation
130      */
131     Result
period_us(int us)132     period_us(int us)
133     {
134         return (Result) mraa_pwm_period_us(m_pwm, us);
135     }
136     /**
137      * Set pulsewidth, As represnted by seconds in a (float)
138      *
139      * @param seconds The duration of a pulse
140      * @return Result of operation
141      */
142     Result
pulsewidth(float seconds)143     pulsewidth(float seconds)
144     {
145         return (Result) mraa_pwm_pulsewidth(m_pwm, seconds);
146     }
147     /**
148      * Set pulsewidth, milliseconds
149      *
150      * @param ms milliseconds for pulsewidth
151      * @return Result of operation
152      */
153     Result
pulsewidth_ms(int ms)154     pulsewidth_ms(int ms)
155     {
156         return (Result) mraa_pwm_pulsewidth_ms(m_pwm, ms);
157     }
158     /**
159      * The pulsewidth, microseconds
160      *
161      * @param us microseconds for pulsewidth
162      * @return Result of operation
163      */
164     Result
pulsewidth_us(int us)165     pulsewidth_us(int us)
166     {
167         return (Result) mraa_pwm_pulsewidth_us(m_pwm, us);
168     }
169     /**
170      * Set the enable status of the PWM pin. None zero will assume on with
171      * output being driven and 0 will disable the output
172      *
173      * @param enable enable status of pin
174      * @return Result of operation
175      */
176     Result
enable(bool enable)177     enable(bool enable)
178     {
179         if (enable)
180             return (Result) mraa_pwm_enable(m_pwm, 1);
181         else
182             return (Result) mraa_pwm_enable(m_pwm, 0);
183     }
184     /**
185      * Set the period and duty of a PWM object.
186      *
187      * @param period represented in ms.
188      * @param duty represnted in ms as float.
189      * @return Result of operation
190      */
191     Result
config_ms(int period,float duty)192     config_ms(int period, float duty)
193     {
194         return (Result) mraa_pwm_config_ms(m_pwm, period, duty);
195     }
196     /**
197      * Set the period and duty (percent) of a PWM object.
198      *
199      * @param period as represented in ms.
200      * @param duty percentage i.e. 50% = 0.5f
201      * @return Result of operation
202      */
203     Result
config_percent(int period,float duty)204     config_percent(int period, float duty)
205     {
206         return (Result) mraa_pwm_config_percent(m_pwm, period, duty);
207     }
208     /**
209      * Get the maximum pwm period in us
210      *
211      * @return max pwm in us
212      */
213     int
max_period()214     max_period()
215     {
216         return mraa_pwm_get_max_period();
217     }
218     /**
219      * Get the minimum pwm period in us
220      *
221      * @return min pwm in us
222      */
223     int
min_period()224     min_period()
225     {
226         return mraa_pwm_get_min_period();
227     }
228 
229   private:
230     mraa_pwm_context m_pwm;
231 };
232 }
233