Breath LED that turn on/off or loop the brightness smoothly.

Dependents:   BreathLed-demo EPD_GDE021A1_demo mbed_esp8266_demo NUCLEO_uart_flow_control

This breathing-led uses Ticker and PWM which consumes few CPU, and most important, you can use it to tell system hang.

Just init it with a PWM pin which connected to a LED.

BreathLed.h

Committer:
steeven
Date:
2015-04-06
Revision:
0:13c83a6b336f

File content as of revision 0:13c83a6b336f:

/*
 * BreathLed.h
 *
 *  Created on: 2015/4/6
 *      Author: steeven@gmail.com
 */

#ifndef BREATHLED_H_
#define BREATHLED_H_

#include "mbed.h"

#define BREATH_STEPS 24

namespace steeven {

/** A breath led, used for turn on/off or loop the leds slowly with PWM.
 */
class BreathLed {

public:
    /**
     * Create an BreathLed
     *
     * @param pin LED pin with PWM support
     * @param time Time for the brightness transition
     * @param hold Time for holding the brightness before next transition.
     *      Effective for loop mode.
     */
    BreathLed(PinName pin, float time = 1, float hold = 1);

    virtual ~BreathLed();

    /* Group: Access Methods */

    /**
     * start loop the led
     */
    void loop(float time = 1, float hold = 1);

    /**
     * turn on the led
     */
    void on();

    /**
     * turn off the led
     */
    void off();


protected:
    PwmOut _pin;
    int _mode; //loop, on, off
    float _time; //time of brightness transition
    float _hold; //time to hold in loop
    Ticker _ticker;

    int _off; //current direction
    int _step; //current brightness

    void step_tick();
    void hold_tick();
};
}
#endif /* BREATHLED_H_ */