Simple library for LED blinking.
Dependents: roam_v2 finalV1 finalV1 finalv2 ... more
Revision 3:286a364f952f, committed 2016-12-10
- Comitter:
- tbjazic
- Date:
- Sat Dec 10 14:39:42 2016 +0000
- Parent:
- 2:190915d53c0b
- Child:
- 4:abcdbfe38247
- Commit message:
- wait() functions removed.
Changed in this revision
Blinker.cpp | Show annotated file Show diff for this revision Revisions of this file |
Blinker.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/Blinker.cpp Sun Nov 15 09:12:51 2015 +0000 +++ b/Blinker.cpp Sat Dec 10 14:39:42 2016 +0000 @@ -3,13 +3,50 @@ Blinker::Blinker(PinName pin):myled(pin) { myled = 0; + i = 0; + N = 0; + period = 0; } void Blinker::blink(int n, float t) { - for (int i = 0; i < n; i++) { - myled = 1; - wait(t/2); - myled = 0; - wait(t/2); + finished = false; + almostFinished = false; + i = 0; + if (i < n) { + myled = 1; + timeout.attach(this, &Blinker::turnLedOff, t/2); + N = n; + period = t; + i++; + if (i < N) { + ticker.attach(this, &Blinker::turnLedOn, t); + } else { + almostFinished = true; + } + } else { + finished = true; } +} + +void Blinker::turnLedOff() { + myled = 0; + if (almostFinished) { + timeout.attach(this, &Blinker::done, period/2); + } +} + +void Blinker::turnLedOn() { + myled = 1; + timeout.attach(this, &Blinker::turnLedOff, period/2); + if (++i >= N) { + ticker.detach(); + almostFinished = true; + } +} +void Blinker::done() { + finished = true; +} + +bool Blinker::isFinished() { + return finished; } \ No newline at end of file
--- a/Blinker.h Sun Nov 15 09:12:51 2015 +0000 +++ b/Blinker.h Sat Dec 10 14:39:42 2016 +0000 @@ -24,13 +24,30 @@ * @endcode */ class Blinker { -private: - DigitalOut myled; + public: + /** Constructor receives a pin name that LED is connected to. */ Blinker(PinName pin); + /** Function recevies number of blinks (flashes) and a time of duration of each blink. */ void blink(int n, float t = 0.5); + /** Checks if the blinking is finished. + * @returns true if the complete process is finished, false otherwise. + */ + bool isFinished(); + +private: + + DigitalOut myled; + int i, N; + float period; + Ticker ticker; + Timeout timeout; + bool finished, almostFinished; + void turnLedOff(); + void turnLedOn(); + void done(); }; #endif \ No newline at end of file