Use the hardware PwmOut to pulsate an LED (or something else), with selectable active high/low, customisable intensity function, gamma correction, and number of brightness levels.
Diff: Pulsator.cpp
- Revision:
- 1:bcddb9898625
- Parent:
- 0:dda7f6f55dc1
- Child:
- 2:e351afc2a3a8
--- a/Pulsator.cpp Fri Apr 24 03:56:51 2015 +0000 +++ b/Pulsator.cpp Fri Apr 24 05:14:06 2015 +0000 @@ -1,3 +1,21 @@ +/* Copyright (c) 2015 Liyang HU, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + #include <math.h> #include <mbed.h> @@ -11,6 +29,22 @@ # define M_PI_2 1.57079632679489661923 #endif +/*! \class Pulsator + * \brief Pulsate an LED using hardware \a PwmOut and \a Ticker. + * \code +#include <mbed.h> +#include <Pulsator.h> + +Pulsator led(LED1, 2.0, false); + +int main(void) { + led = true; + while(1) wait(1); +} + * \endcode + * \see \a PwmOut, \a Ticker + */ + void Pulsator::step(void) { // sinf(phase_2)^2 == (1 - cosf(phase)) / 2 @@ -36,12 +70,23 @@ out = active_high ? 0.0 : 1.0; } +/*! Create a \a Pulsator object. + * \param pin Pin to pulsate. Some platforms can only drive certain pins with the hardware PWM. + * \param period Duration of each cycle, in seconds. + * \param active_high Should the output pin be active high (\a true), or active low (\a false)? + * \param gamma Gamma correction for the output LED. + * \param levels Number of distinct brightness levels: a minimum of 2. + */ Pulsator::Pulsator(PinName pin, float period, bool active_high, float gamma, int levels) : out(pin), period(period), active_high(active_high), gamma(gamma), levels(levels) { disable(); } + +/*! Enable or disable the output. + * \param state Pulsate the output? Passing \a false deactivates the output. + */ Pulsator& Pulsator::operator=(bool state) { state ? enable() : disable();