]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/api/PwmOut.h
Merge commit '5a0132f1c1c9a14fd2941f0a5e29bbf5e31da20c' into master-core-pull
[max/tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / api / PwmOut.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_PWMOUT_H
17 #define MBED_PWMOUT_H
18
19 #include "platform.h"
20
21 #if DEVICE_PWMOUT
22 #include "pwmout_api.h"
23
24 namespace mbed {
25
26 /** A pulse-width modulation digital output
27  *
28  * Example
29  * @code
30  * // Fade a led on.
31  * #include "mbed.h"
32  *
33  * PwmOut led(LED1);
34  *
35  * int main() {
36  *     while(1) {
37  *         led = led + 0.01;
38  *         wait(0.2);
39  *         if(led == 1.0) {
40  *             led = 0;
41  *         }
42  *     }
43  * }
44  * @endcode
45  *
46  * @note
47  *  On the LPC1768 and LPC2368, the PWMs all share the same
48  *  period - if you change the period for one, you change it for all.
49  *  Although routines that change the period maintain the duty cycle
50  *  for its PWM, all other PWMs will require their duty cycle to be
51  *  refreshed.
52  */
53 class PwmOut {
54
55 public:
56
57     /** Create a PwmOut connected to the specified pin
58      *
59      *  @param pin PwmOut pin to connect to
60      */
61     PwmOut(PinName pin) {
62         pwmout_init(&_pwm, pin);
63     }
64
65     /** Set the ouput duty-cycle, specified as a percentage (float)
66      *
67      *  @param value A floating-point value representing the output duty-cycle,
68      *    specified as a percentage. The value should lie between
69      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
70      *    Values outside this range will be saturated to 0.0f or 1.0f.
71      */
72     void write(float value) {
73         pwmout_write(&_pwm, value);
74     }
75
76     /** Return the current output duty-cycle setting, measured as a percentage (float)
77      *
78      *  @returns
79      *    A floating-point value representing the current duty-cycle being output on the pin,
80      *    measured as a percentage. The returned value will lie between
81      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
82      *
83      *  @note
84      *  This value may not match exactly the value set by a previous <write>.
85      */
86     float read() {
87         return pwmout_read(&_pwm);
88     }
89
90     /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
91      *
92      *  @note
93      *   The resolution is currently in microseconds; periods smaller than this
94      *   will be set to zero.
95      */
96     void period(float seconds) {
97         pwmout_period(&_pwm, seconds);
98     }
99
100     /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
101      */
102     void period_ms(int ms) {
103         pwmout_period_ms(&_pwm, ms);
104     }
105
106     /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
107      */
108     void period_us(int us) {
109         pwmout_period_us(&_pwm, us);
110     }
111
112     /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
113      */
114     void pulsewidth(float seconds) {
115         pwmout_pulsewidth(&_pwm, seconds);
116     }
117
118     /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
119      */
120     void pulsewidth_ms(int ms) {
121         pwmout_pulsewidth_ms(&_pwm, ms);
122     }
123
124     /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
125      */
126     void pulsewidth_us(int us) {
127         pwmout_pulsewidth_us(&_pwm, us);
128     }
129
130 #ifdef MBED_OPERATORS
131     /** A operator shorthand for write()
132      */
133     PwmOut& operator= (float value) {
134         write(value);
135         return *this;
136     }
137
138     PwmOut& operator= (PwmOut& rhs) {
139         write(rhs.read());
140         return *this;
141     }
142
143     /** An operator shorthand for read()
144      */
145     operator float() {
146         return read();
147     }
148 #endif
149
150 protected:
151     pwmout_t _pwm;
152 };
153
154 } // namespace mbed
155
156 #endif
157
158 #endif