Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PwmOut.h Source File

PwmOut.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_PWMOUT_H
00018 #define MBED_PWMOUT_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #if DEVICE_PWMOUT || defined(DOXYGEN_ONLY)
00023 #include "hal/pwmout_api.h"
00024 
00025 namespace mbed {
00026 /**
00027  * \defgroup drivers_PwmOut PwmOut class
00028  * \ingroup drivers-public-api-gpio
00029  * @{
00030  */
00031 
00032 /** A pulse-width modulation digital output
00033  *
00034  * @note Synchronization level: Interrupt safe
00035  *
00036  * Example
00037  * @code
00038  * // Gradually change the intensity of the LED.
00039  * #include "mbed.h"
00040  *
00041  * PwmOut led(LED1);
00042  *
00043  * int main() {
00044  *     while(1) {
00045  *         led = led + 0.01;
00046  *         wait(0.2);
00047  *         if(led == 1.0) {
00048  *             led = 0;
00049  *         }
00050  *     }
00051  * }
00052  * @endcode
00053  */
00054 class PwmOut {
00055 
00056 public:
00057 
00058     /** Create a PwmOut connected to the specified pin
00059      *
00060      *  @param pin PwmOut pin to connect to
00061      */
00062     PwmOut(PinName pin);
00063 
00064     /** Create a PwmOut connected to the specified pin
00065      *
00066      *  @param pinmap reference to structure which holds static pinmap.
00067      */
00068     PwmOut(const PinMap &pinmap);
00069     PwmOut(const PinMap &&) = delete; // prevent passing of temporary objects
00070 
00071     ~PwmOut();
00072 
00073     /** Set the output duty-cycle, specified as a percentage (float)
00074      *
00075      *  @param value A floating-point value representing the output duty-cycle,
00076      *    specified as a percentage. The value should lie between
00077      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
00078      *    Values outside this range will be saturated to 0.0f or 1.0f.
00079      */
00080     void write(float value);
00081 
00082     /** Return the current output duty-cycle setting, measured as a percentage (float)
00083      *
00084      *  @returns
00085      *    A floating-point value representing the current duty-cycle being output on the pin,
00086      *    measured as a percentage. The returned value will lie between
00087      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
00088      *
00089      *  @note
00090      *  This value may not match exactly the value set by a previous write().
00091      */
00092     float read();
00093 
00094     /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
00095      *
00096      *  @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
00097      *  @note
00098      *   The resolution is currently in microseconds; periods smaller than this
00099      *   will be set to zero.
00100      */
00101     void period(float seconds);
00102 
00103     /** Set the PWM period, specified in milliseconds (int), keeping the duty cycle the same.
00104      *  @param ms Change the period of a PWM signal in milliseconds without modifying the duty cycle
00105      */
00106     void period_ms(int ms);
00107 
00108     /** Set the PWM period, specified in microseconds (int), keeping the duty cycle the same.
00109      *  @param us Change the period of a PWM signal in microseconds without modifying the duty cycle
00110      */
00111     void period_us(int us);
00112 
00113     /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
00114      *  @param seconds Change the pulse width of a PWM signal specified in seconds (float)
00115      */
00116     void pulsewidth(float seconds);
00117 
00118     /** Set the PWM pulsewidth, specified in milliseconds (int), keeping the period the same.
00119      *  @param ms Change the pulse width of a PWM signal specified in milliseconds
00120      */
00121     void pulsewidth_ms(int ms);
00122 
00123     /** Set the PWM pulsewidth, specified in microseconds (int), keeping the period the same.
00124      *  @param us Change the pulse width of a PWM signal specified in microseconds
00125      */
00126     void pulsewidth_us(int us);
00127 
00128     /** Suspend PWM operation
00129      *
00130      * Control the PWM state. This is primarily intended
00131      * for temporary power-saving; This call can
00132      * allow pwm to be temporarily disabled to permit power saving without
00133      * losing device state. The subsequent function call must be PwmOut::resume
00134      * for PWM to resume; any other calls prior to resuming are undefined behavior.
00135      */
00136     void suspend();
00137 
00138     /** Resume PWM operation
00139      *
00140      * Control the PWM state. This is primarily intended
00141      * to resume PWM operations after a previous PwmOut::suspend call;
00142      * This call restores the device state prior to suspension.
00143      */
00144     void resume();
00145 
00146     /** A operator shorthand for write()
00147      *  \sa PwmOut::write()
00148      */
00149     PwmOut &operator= (float value)
00150     {
00151         // Underlying call is thread safe
00152         write(value);
00153         return *this;
00154     }
00155 
00156     /** A operator shorthand for write()
00157      * \sa PwmOut::write()
00158      */
00159     PwmOut &operator= (PwmOut &rhs)
00160     {
00161         // Underlying call is thread safe
00162         write(rhs.read());
00163         return *this;
00164     }
00165 
00166     /** An operator shorthand for read()
00167      * \sa PwmOut::read()
00168      */
00169     operator float()
00170     {
00171         // Underlying call is thread safe
00172         return read();
00173     }
00174 
00175 #if !(DOXYGEN_ONLY)
00176 protected:
00177     /** Lock deep sleep only if it is not yet locked */
00178     void lock_deep_sleep();
00179 
00180     /** Unlock deep sleep in case it is locked */
00181     void unlock_deep_sleep();
00182 
00183     /** Initialize this instance */
00184     void init();
00185 
00186     /** Power down this instance */
00187     void deinit();
00188 
00189     pwmout_t _pwm;
00190     PinName _pin;
00191     bool _deep_sleep_locked;
00192     bool _initialized;
00193     float _duty_cycle;
00194 #endif
00195 };
00196 
00197 /** @}*/
00198 
00199 } // namespace mbed
00200 
00201 #endif
00202 
00203 #endif