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.
Diff: Pulsator.cpp
- Revision:
- 7:7abc04b4c474
- Parent:
- 6:5eeb1acc1c50
- Child:
- 8:ddedf56b2eb0
diff -r 5eeb1acc1c50 -r 7abc04b4c474 Pulsator.cpp --- a/Pulsator.cpp Sun Apr 26 01:39:38 2015 +0000 +++ b/Pulsator.cpp Sun Apr 26 01:43:31 2015 +0000 @@ -49,22 +49,22 @@ { _enable = false; detach(); - out = _active_high ? 0.0 : 1.0; + out = _active_high ? 0.0f : 1.0f; } void Pulsator::enable(void) { - out.period(1.0 / 1024.0); - phase_2 = 0.0; + out.period(1.0f / 1024.0f); + phase_2 = 0.0f; step(); _enable = true; - attach(this, &Pulsator::step, 0.5 * _period / (float)(_levels - 1)); + 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.0 * 0.5 * _period / (float)(_levels - 1); + _delay = 1000000.0f * 0.5f * _period / (float)(_levels - 1); } void Pulsator::step(void) @@ -72,7 +72,7 @@ // sinf(phase_2)^2 == (1 - cosf(phase)) / 2 float s = sinf(phase_2); float level = powf(s * s, _gamma); - out = _active_high ? level : 1.0 - level; + out = _active_high ? level : 1.0f - level; phase_2 += M_PI_2 / (float)(_levels - 1); if(phase_2 >= M_PI) phase_2 -= M_PI; @@ -109,7 +109,7 @@ { _active_high = high; if(!_enable) - out = _active_high ? 0.0 : 1.0; + out = _active_high ? 0.0f : 1.0f; return *this; }