steeven lee / BreathLed Featured

Dependents:   BreathLed-demo EPD_GDE021A1_demo mbed_esp8266_demo NUCLEO_uart_flow_control

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BreathLed.cpp Source File

BreathLed.cpp

00001 /*
00002  * BreathLed.cpp
00003  *
00004  *  Created on: 2015/4/6
00005  *      Author: steeven@gmail.com
00006  */
00007 
00008 #include <BreathLed/BreathLed.h>
00009 
00010 namespace steeven {
00011 BreathLed::BreathLed(PinName pin, float time, float hold) :
00012         _pin(pin), _ticker() {
00013     _pin = 0;
00014     _time = time;
00015     _hold = hold;
00016     _mode = 0;
00017     _off = 0;
00018     _step = 0;
00019 }
00020 void BreathLed::hold_tick() {
00021     _ticker.attach(this, &BreathLed::step_tick, _time / BREATH_STEPS);
00022 }
00023 void BreathLed::step_tick() {
00024 
00025     if (_off) {
00026         if (_step <= 0) {
00027             if (_mode == 0) { //loop
00028                 _off = !_off;
00029                 if (_hold)
00030                     _ticker.attach(this, &BreathLed::hold_tick, _hold);
00031             } else
00032                 _ticker.detach();
00033             return;
00034         } else {
00035             _step--;
00036         }
00037     } else {
00038         if (_step >= BREATH_STEPS) {
00039             if (_mode == 0) { //loop
00040                 _off = !_off;
00041                 if (_hold)
00042                     _ticker.attach(this, &BreathLed::hold_tick, _hold);
00043             } else
00044                 _ticker.detach();
00045             return;
00046         } else {
00047             _step++;
00048         }
00049     }
00050 
00051     _pin =  (float)(1 << _step) /  (1 << BREATH_STEPS);
00052 }
00053 void BreathLed::loop(float time, float hold) {
00054     _time = time;
00055     _hold = hold;
00056     _mode = 0;
00057     _ticker.attach(this, &BreathLed::step_tick, _time / BREATH_STEPS);
00058 }
00059 
00060 
00061 void BreathLed::on() {
00062     _mode = 1;
00063     _off = 0;
00064     if (_pin < 1)
00065         _ticker.attach(this, &BreathLed::step_tick, _time / BREATH_STEPS);
00066     else
00067         _ticker.detach();
00068 }
00069 void BreathLed::off() {
00070     _mode = 2;
00071     _off = 1;
00072     if (_pin > 0)
00073         _ticker.attach(this, &BreathLed::step_tick, _time / BREATH_STEPS);
00074     else
00075         _ticker.detach();
00076 }
00077 BreathLed::~BreathLed() {
00078     if (!_pin != 0)
00079         _pin = 0;
00080 }
00081 }// namespace steeven