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.

Dependents:   RedButton

Committer:
huliyang
Date:
Fri Apr 24 05:14:06 2015 +0000
Revision:
1:bcddb9898625
Parent:
0:dda7f6f55dc1
Child:
2:e351afc2a3a8
Doxygenate

Who changed what in which revision?

UserRevisionLine numberNew contents of line
huliyang 1:bcddb9898625 1 /* Copyright (c) 2015 Liyang HU, MIT License
huliyang 1:bcddb9898625 2 *
huliyang 1:bcddb9898625 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
huliyang 1:bcddb9898625 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
huliyang 1:bcddb9898625 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
huliyang 1:bcddb9898625 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
huliyang 1:bcddb9898625 7 * furnished to do so, subject to the following conditions:
huliyang 1:bcddb9898625 8 *
huliyang 1:bcddb9898625 9 * The above copyright notice and this permission notice shall be included in all copies or
huliyang 1:bcddb9898625 10 * substantial portions of the Software.
huliyang 1:bcddb9898625 11 *
huliyang 1:bcddb9898625 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
huliyang 1:bcddb9898625 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
huliyang 1:bcddb9898625 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
huliyang 1:bcddb9898625 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
huliyang 1:bcddb9898625 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
huliyang 1:bcddb9898625 17 */
huliyang 1:bcddb9898625 18
huliyang 0:dda7f6f55dc1 19 #include <math.h>
huliyang 0:dda7f6f55dc1 20 #include <mbed.h>
huliyang 0:dda7f6f55dc1 21
huliyang 0:dda7f6f55dc1 22 #include <Pulsator.h>
huliyang 0:dda7f6f55dc1 23
huliyang 0:dda7f6f55dc1 24 #ifndef M_PI
huliyang 0:dda7f6f55dc1 25 # define M_PI 3.14159265358979323846
huliyang 0:dda7f6f55dc1 26 #endif
huliyang 0:dda7f6f55dc1 27
huliyang 0:dda7f6f55dc1 28 #ifndef M_PI_2
huliyang 0:dda7f6f55dc1 29 # define M_PI_2 1.57079632679489661923
huliyang 0:dda7f6f55dc1 30 #endif
huliyang 0:dda7f6f55dc1 31
huliyang 1:bcddb9898625 32 /*! \class Pulsator
huliyang 1:bcddb9898625 33 * \brief Pulsate an LED using hardware \a PwmOut and \a Ticker.
huliyang 1:bcddb9898625 34 * \code
huliyang 1:bcddb9898625 35 #include <mbed.h>
huliyang 1:bcddb9898625 36 #include <Pulsator.h>
huliyang 1:bcddb9898625 37
huliyang 1:bcddb9898625 38 Pulsator led(LED1, 2.0, false);
huliyang 1:bcddb9898625 39
huliyang 1:bcddb9898625 40 int main(void) {
huliyang 1:bcddb9898625 41 led = true;
huliyang 1:bcddb9898625 42 while(1) wait(1);
huliyang 1:bcddb9898625 43 }
huliyang 1:bcddb9898625 44 * \endcode
huliyang 1:bcddb9898625 45 * \see \a PwmOut, \a Ticker
huliyang 1:bcddb9898625 46 */
huliyang 1:bcddb9898625 47
huliyang 0:dda7f6f55dc1 48 void Pulsator::step(void)
huliyang 0:dda7f6f55dc1 49 {
huliyang 0:dda7f6f55dc1 50 // sinf(phase_2)^2 == (1 - cosf(phase)) / 2
huliyang 0:dda7f6f55dc1 51 float s = sinf(phase_2);
huliyang 0:dda7f6f55dc1 52 float level = powf(s * s, gamma);
huliyang 0:dda7f6f55dc1 53 out = active_high ? level : 1.0 - level;
huliyang 0:dda7f6f55dc1 54 phase_2 += M_PI_2 / (float)(levels - 1);
huliyang 0:dda7f6f55dc1 55 if(phase_2 >= M_PI)
huliyang 0:dda7f6f55dc1 56 phase_2 = 0.0;
huliyang 0:dda7f6f55dc1 57 }
huliyang 0:dda7f6f55dc1 58
huliyang 0:dda7f6f55dc1 59 void Pulsator::enable(void)
huliyang 0:dda7f6f55dc1 60 {
huliyang 0:dda7f6f55dc1 61 out.period(1.0 / 1024.0);
huliyang 0:dda7f6f55dc1 62 phase_2 = 0.0;
huliyang 0:dda7f6f55dc1 63 step();
huliyang 0:dda7f6f55dc1 64 ticker.attach(this, &Pulsator::step, 0.5 * period / levels);
huliyang 0:dda7f6f55dc1 65 }
huliyang 0:dda7f6f55dc1 66
huliyang 0:dda7f6f55dc1 67 void Pulsator::disable(void)
huliyang 0:dda7f6f55dc1 68 {
huliyang 0:dda7f6f55dc1 69 ticker.detach();
huliyang 0:dda7f6f55dc1 70 out = active_high ? 0.0 : 1.0;
huliyang 0:dda7f6f55dc1 71 }
huliyang 0:dda7f6f55dc1 72
huliyang 1:bcddb9898625 73 /*! Create a \a Pulsator object.
huliyang 1:bcddb9898625 74 * \param pin Pin to pulsate. Some platforms can only drive certain pins with the hardware PWM.
huliyang 1:bcddb9898625 75 * \param period Duration of each cycle, in seconds.
huliyang 1:bcddb9898625 76 * \param active_high Should the output pin be active high (\a true), or active low (\a false)?
huliyang 1:bcddb9898625 77 * \param gamma Gamma correction for the output LED.
huliyang 1:bcddb9898625 78 * \param levels Number of distinct brightness levels: a minimum of 2.
huliyang 1:bcddb9898625 79 */
huliyang 0:dda7f6f55dc1 80 Pulsator::Pulsator(PinName pin, float period, bool active_high, float gamma, int levels)
huliyang 0:dda7f6f55dc1 81 : out(pin), period(period), active_high(active_high), gamma(gamma), levels(levels)
huliyang 0:dda7f6f55dc1 82 {
huliyang 0:dda7f6f55dc1 83 disable();
huliyang 0:dda7f6f55dc1 84 }
huliyang 0:dda7f6f55dc1 85
huliyang 1:bcddb9898625 86
huliyang 1:bcddb9898625 87 /*! Enable or disable the output.
huliyang 1:bcddb9898625 88 * \param state Pulsate the output? Passing \a false deactivates the output.
huliyang 1:bcddb9898625 89 */
huliyang 0:dda7f6f55dc1 90 Pulsator& Pulsator::operator=(bool state)
huliyang 0:dda7f6f55dc1 91 {
huliyang 0:dda7f6f55dc1 92 state ? enable() : disable();
huliyang 0:dda7f6f55dc1 93 return *this;
huliyang 0:dda7f6f55dc1 94 }