Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

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-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_PWMOUT_H
00017 #define MBED_PWMOUT_H
00018 
00019 #include "platform.h"
00020 
00021 #if DEVICE_PWMOUT
00022 #include "pwmout_api.h"
00023 
00024 namespace mbed {
00025 
00026 /** A pulse-width modulation digital output
00027  *
00028  * Example
00029  * @code
00030  * // Fade a led on.
00031  * #include "mbed.h"
00032  *
00033  * PwmOut led(LED1);
00034  *
00035  * int main() {
00036  *     while(1) {
00037  *         led = led + 0.01;
00038  *         wait(0.2);
00039  *         if(led == 1.0) {
00040  *             led = 0;
00041  *         }
00042  *     }
00043  * }
00044  * @endcode
00045  *
00046  * @note
00047  *  On the LPC1768 and LPC2368, the PWMs all share the same
00048  *  period - if you change the period for one, you change it for all.
00049  *  Although routines that change the period maintain the duty cycle
00050  *  for its PWM, all other PWMs will require their duty cycle to be
00051  *  refreshed.
00052  */
00053 class PwmOut {
00054 
00055 public:
00056 
00057     /** Create a PwmOut connected to the specified pin
00058      *
00059      *  @param pin PwmOut pin to connect to
00060      */
00061     PwmOut(PinName pin) {
00062         pwmout_init(&_pwm, pin);
00063     }
00064     
00065     ~PwmOut() {
00066         pwmout_free(&_pwm);
00067     }
00068 
00069     /** Set the ouput duty-cycle, specified as a percentage (float)
00070      *
00071      *  @param value A floating-point value representing the output duty-cycle,
00072      *    specified as a percentage. The value should lie between
00073      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
00074      *    Values outside this range will be saturated to 0.0f or 1.0f.
00075      */
00076     void write(float value) {
00077         pwmout_write(&_pwm, value);
00078     }
00079 
00080     /** Return the current output duty-cycle setting, measured as a percentage (float)
00081      *
00082      *  @returns
00083      *    A floating-point value representing the current duty-cycle being output on the pin,
00084      *    measured as a percentage. The returned value will lie between
00085      *    0.0f (representing on 0%) and 1.0f (representing on 100%).
00086      *
00087      *  @note
00088      *  This value may not match exactly the value set by a previous <write>.
00089      */
00090     float read() {
00091         return pwmout_read(&_pwm);
00092     }
00093 
00094     /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
00095      *
00096      *  @note
00097      *   The resolution is currently in microseconds; periods smaller than this
00098      *   will be set to zero.
00099      */
00100     void period(float seconds) {
00101         pwmout_period(&_pwm, seconds);
00102     }
00103 
00104     /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
00105      */
00106     void period_ms(int ms) {
00107         pwmout_period_ms(&_pwm, ms);
00108     }
00109 
00110     /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
00111      */
00112     void period_us(int us) {
00113         pwmout_period_us(&_pwm, us);
00114     }
00115 
00116     /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
00117      */
00118     void pulsewidth(float seconds) {
00119         pwmout_pulsewidth(&_pwm, seconds);
00120     }
00121 
00122     /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
00123      */
00124     void pulsewidth_ms(int ms) {
00125         pwmout_pulsewidth_ms(&_pwm, ms);
00126     }
00127 
00128     /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
00129      */
00130     void pulsewidth_us(int us) {
00131         pwmout_pulsewidth_us(&_pwm, us);
00132     }
00133 
00134 #ifdef MBED_OPERATORS
00135     /** A operator shorthand for write()
00136      */
00137     PwmOut& operator= (float value) {
00138         write(value);
00139         return *this;
00140     }
00141 
00142     PwmOut& operator= (PwmOut& rhs) {
00143         write(rhs.read());
00144         return *this;
00145     }
00146 
00147     /** An operator shorthand for read()
00148      */
00149     operator float() {
00150         return read();
00151     }
00152 #endif
00153 
00154 protected:
00155     pwmout_t _pwm;
00156 };
00157 
00158 } // namespace mbed
00159 
00160 #endif
00161 
00162 #endif