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

Pulsator.cpp

Committer:
huliyang
Date:
2015-04-26
Revision:
7:7abc04b4c474
Parent:
6:5eeb1acc1c50
Child:
8:ddedf56b2eb0

File content as of revision 7:7abc04b4c474:

/* 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>

#include <Pulsator.h>

#ifndef M_PI
# define M_PI    3.14159265358979323846
#endif

#ifndef M_PI_2
# 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);

int main(void) {
    led.period(2.0).gamma(1.8) = true;
    while(1) wait(1);
}
 * \endcode
 * \see \a PwmOut, \a Ticker
 */

void Pulsator::disable(void)
{
    _enable = false;
    detach();
    out = _active_high ? 0.0f : 1.0f;
}

void Pulsator::enable(void)
{
    out.period(1.0f / 1024.0f);
    phase_2 = 0.0f;
    step();
    _enable = true;
    attach(this, &Pulsator::step, 0.5f * _period / (float)(_levels - 1));
}

//! Bit of a hack to update _delay without re-attaching the handler.
void Pulsator::reload(void)
{
    _delay = 1000000.0f * 0.5f * _period / (float)(_levels - 1);
}

void Pulsator::step(void)
{
    // sinf(phase_2)^2 == (1 - cosf(phase)) / 2
    float s = sinf(phase_2);
    float level = powf(s * s, _gamma);
    out = _active_high ? level : 1.0f - level;
    phase_2 += M_PI_2 / (float)(_levels - 1);
    if(phase_2 >= M_PI)
        phase_2 -= M_PI;
}

//! Create a \a Pulsator attached to the specified \a pin.
//! \param pin Pin to pulsate.
//! \note Some platforms can only drive certain pins with the hardware PWM.
Pulsator::Pulsator(PinName pin) : out(pin)
{
    active_high(); disable(); gamma(); period(); levels();
}

//! Enable or disable the output.
/* \note Passing \a false deactivates the output completely, rather than
    maintaining the LED brightness at the point when this is called.
    Conversely, \a true starts pulsating from the inactive state.
    Setting the curent state is a no-op. */
Pulsator& Pulsator::operator=(bool state)
{
    if(state != _enable)
        state ? enable() : disable();
    return *this;
}

//! Get the current state of the output.
Pulsator::operator bool(void)
{
    return _enable;
}

//! Should the output pin be active high (\a true), or active low (\a false)?
Pulsator& Pulsator::active_high(bool high)
{
    _active_high = high;
    if(!_enable)
        out = _active_high ? 0.0f : 1.0f;
    return *this;
}

//! Set the gamma correction for the output LED.
Pulsator& Pulsator::gamma(float power)
{
    _gamma = power;
    return *this;
}

//! Set the number (>= 2) of distinct brightness levels.
Pulsator& Pulsator::levels(int number)
{
    _levels = number;
    reload();
    return *this;
}

//! Set the duration of each cycle, in seconds.
Pulsator& Pulsator::period(float seconds)
{
    _period = seconds;
    reload();
    return *this;
}