mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DynamicPwm.h Source File

DynamicPwm.h

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 #include "mbed.h"
00027 #include "MicroBitConfig.h"
00028 
00029 #ifndef MICROBIT_DYNAMIC_PWM_H
00030 #define MICROBIT_DYNAMIC_PWM_H
00031 
00032 #define NO_PWMS 3
00033 #define MICROBIT_DEFAULT_PWM_PERIOD 20000
00034 
00035 enum PwmPersistence
00036 {
00037     PWM_PERSISTENCE_TRANSIENT = 1,
00038     PWM_PERSISTENCE_PERSISTENT = 2,
00039 };
00040 
00041 /**
00042   * Class definition for DynamicPwm.
00043   *
00044   * This class addresses a few issues found in the underlying libraries.
00045   * This provides the ability for a neat, clean swap between PWM channels.
00046   */
00047 class DynamicPwm : public PwmOut
00048 {
00049     private:
00050     static DynamicPwm* pwms[NO_PWMS];
00051     static uint8_t lastUsed;
00052     static uint16_t sharedPeriod;
00053     uint8_t flags;
00054     float lastValue;
00055 
00056 
00057 
00058     /**
00059       * An internal constructor used when allocating a new DynamicPwm instance.
00060       *
00061       * @param pin the name of the pin for the pwm to target
00062       *
00063       * @param persistance the level of persistence for this pin PWM_PERSISTENCE_PERSISTENT (can not be replaced until freed, should only be used for system services really.)
00064       *                    or PWM_PERSISTENCE_TRANSIENT (can be replaced at any point if a channel is required.)
00065       */
00066     DynamicPwm(PinName pin, PwmPersistence persistence = PWM_PERSISTENCE_TRANSIENT);
00067 
00068     public:
00069 
00070     /**
00071       * Redirects the pwm channel to point at a different pin.
00072       *
00073       * @param pin the desired pin to output a PWM wave.
00074       *
00075       * @code
00076       * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00077       * pwm->redirect(p0); // pwm is now produced on p0
00078       * @endcode
00079       */
00080     void redirect(PinName pin);
00081 
00082 
00083     /**
00084       * Creates a new DynamicPwm instance, or reuses an existing instance that
00085       * has a persistence level of PWM_PERSISTENCE_TRANSIENT.
00086       *
00087       * @param pin the name of the pin for the pwm to target
00088       *
00089       * @param persistance the level of persistence for this pin PWM_PERSISTENCE_PERSISTENT (can not be replaced until freed, should only be used for system services really.)
00090       *                    or PWM_PERSISTENCE_TRANSIENT (can be replaced at any point if a channel is required.)
00091       *
00092       * @return a pointer to the first available free pwm channel - or the first one that can be reallocated. If
00093       *         no channels are available, NULL is returned.
00094       *
00095       * @code
00096       * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00097       * @endcode
00098       */
00099     static DynamicPwm* allocate(PinName pin, PwmPersistence persistence = PWM_PERSISTENCE_TRANSIENT);
00100 
00101     /**
00102       * Frees this DynamicPwm instance for reuse.
00103       *
00104       * @code
00105       * DynamicPwm* pwm = DynamicPwm::allocate();
00106       * pwm->release();
00107       * @endcode
00108       */
00109     void release();
00110 
00111     /**
00112       * A lightweight wrapper around the super class' write in order to capture the value
00113       *
00114       * @param value the duty cycle percentage in floating point format.
00115       *
00116       * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if value is out of range
00117       *
00118       * @code
00119       * DynamicPwm* pwm = DynamicPwm::allocate();
00120       * pwm->write(0.5);
00121       * @endcode
00122       */
00123     int write(float value);
00124 
00125     /**
00126       * Retreives the PinName associated with this DynamicPwm instance.
00127       *
00128       * @code
00129       * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00130       *
00131       * // returns the PinName n.
00132       * pwm->getPinName();
00133       * @endcode
00134       *
00135       * @note This should be used to check that the DynamicPwm instance has not
00136       *       been reallocated for use in another part of a program.
00137       */
00138     PinName getPinName();
00139 
00140     /**
00141       * Retreives the last value that has been written to this DynamicPwm instance.
00142       * in the range 0 - 1023 inclusive.
00143       *
00144       * @code
00145       * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00146       * pwm->write(0.5);
00147       *
00148       * // will return 512.
00149       * pwm->getValue();
00150       * @endcode
00151       */
00152     int getValue();
00153 
00154     /**
00155       * Retreives the current period in use by the entire PWM module in microseconds.
00156       *
00157       * Example:
00158       * @code
00159       * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00160       * pwm->getPeriod();
00161       * @endcode
00162       */
00163     int getPeriodUs();
00164 
00165     /**
00166       * Retreives the current period in use by the entire PWM module in milliseconds.
00167       *
00168       * Example:
00169       * @code
00170       * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00171       * pwm->setPeriodUs(20000);
00172       *
00173       * // will return 20000
00174       * pwm->getPeriod();
00175       * @endcode
00176       */
00177     int getPeriod();
00178 
00179     /**
00180       * Sets the period used by the WHOLE PWM module.
00181       *
00182       * @param period the desired period in microseconds.
00183       *
00184       * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if period is out of range
00185       *
00186       * Example:
00187       * @code
00188       * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00189       *
00190       * // period now is 20ms
00191       * pwm->setPeriodUs(20000);
00192       * @endcode
00193       *
00194       * @note Any changes to the period will AFFECT ALL CHANNELS.
00195       */
00196     int setPeriodUs(int period);
00197 
00198     /**
00199       * Sets the period used by the WHOLE PWM module. Any changes to the period will AFFECT ALL CHANNELS.
00200       *
00201       * @param period the desired period in milliseconds.
00202       *
00203       * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER if period is out of range
00204       *
00205       * Example:
00206       * @code
00207       * DynamicPwm* pwm = DynamicPwm::allocate(PinName n);
00208       *
00209       * // period now is 20ms
00210       * pwm->setPeriod(20);
00211       * @endcode
00212       */
00213       int setPeriod(int period);
00214 };
00215 
00216 #endif