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.

Files at this revision

API Documentation at this revision

Comitter:
steeven
Date:
Mon Apr 06 11:02:16 2015 +0000
Commit message:
init release;

Changed in this revision

BreathLed.cpp Show annotated file Show diff for this revision Revisions of this file
BreathLed.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 13c83a6b336f BreathLed.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BreathLed.cpp	Mon Apr 06 11:02:16 2015 +0000
@@ -0,0 +1,81 @@
+/*
+ * BreathLed.cpp
+ *
+ *  Created on: 2015/4/6
+ *      Author: steeven@gmail.com
+ */
+
+#include <BreathLed/BreathLed.h>
+
+namespace steeven {
+BreathLed::BreathLed(PinName pin, float time, float hold) :
+        _pin(pin), _ticker() {
+    _pin = 0;
+    _time = time;
+    _hold = hold;
+    _mode = 0;
+    _off = 0;
+    _step = 0;
+}
+void BreathLed::hold_tick() {
+    _ticker.attach(this, &BreathLed::step_tick, _time / BREATH_STEPS);
+}
+void BreathLed::step_tick() {
+
+    if (_off) {
+        if (_step <= 0) {
+            if (_mode == 0) { //loop
+                _off = !_off;
+                if (_hold)
+                    _ticker.attach(this, &BreathLed::hold_tick, _hold);
+            } else
+                _ticker.detach();
+            return;
+        } else {
+            _step--;
+        }
+    } else {
+        if (_step >= BREATH_STEPS) {
+            if (_mode == 0) { //loop
+                _off = !_off;
+                if (_hold)
+                    _ticker.attach(this, &BreathLed::hold_tick, _hold);
+            } else
+                _ticker.detach();
+            return;
+        } else {
+            _step++;
+        }
+    }
+
+    _pin =  (float)(1 << _step) /  (1 << BREATH_STEPS);
+}
+void BreathLed::loop(float time, float hold) {
+    _time = time;
+    _hold = hold;
+    _mode = 0;
+    _ticker.attach(this, &BreathLed::step_tick, _time / BREATH_STEPS);
+}
+
+
+void BreathLed::on() {
+    _mode = 1;
+    _off = 0;
+    if (_pin < 1)
+        _ticker.attach(this, &BreathLed::step_tick, _time / BREATH_STEPS);
+    else
+        _ticker.detach();
+}
+void BreathLed::off() {
+    _mode = 2;
+    _off = 1;
+    if (_pin > 0)
+        _ticker.attach(this, &BreathLed::step_tick, _time / BREATH_STEPS);
+    else
+        _ticker.detach();
+}
+BreathLed::~BreathLed() {
+    if (!_pin != 0)
+        _pin = 0;
+}
+}// namespace steeven
diff -r 000000000000 -r 13c83a6b336f BreathLed.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BreathLed.h	Mon Apr 06 11:02:16 2015 +0000
@@ -0,0 +1,66 @@
+/*
+ * 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_ */