Simple library for LED blinking.

Dependents:   roam_v2 finalV1 finalV1 finalv2 ... more

Committer:
tbjazic
Date:
Thu Dec 15 10:56:25 2016 +0000
Revision:
4:abcdbfe38247
Parent:
3:286a364f952f
Insignificant.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tbjazic 0:c9a302c4bed9 1 #include "Blinker.h"
tbjazic 0:c9a302c4bed9 2 #include "mbed.h"
tbjazic 0:c9a302c4bed9 3
tbjazic 0:c9a302c4bed9 4 Blinker::Blinker(PinName pin):myled(pin) {
tbjazic 0:c9a302c4bed9 5 myled = 0;
tbjazic 3:286a364f952f 6 i = 0;
tbjazic 3:286a364f952f 7 N = 0;
tbjazic 3:286a364f952f 8 period = 0;
tbjazic 0:c9a302c4bed9 9 }
tbjazic 0:c9a302c4bed9 10
tbjazic 0:c9a302c4bed9 11 void Blinker::blink(int n, float t) {
tbjazic 3:286a364f952f 12 finished = false;
tbjazic 3:286a364f952f 13 almostFinished = false;
tbjazic 3:286a364f952f 14 i = 0;
tbjazic 3:286a364f952f 15 if (i < n) {
tbjazic 3:286a364f952f 16 myled = 1;
tbjazic 3:286a364f952f 17 timeout.attach(this, &Blinker::turnLedOff, t/2);
tbjazic 3:286a364f952f 18 N = n;
tbjazic 3:286a364f952f 19 period = t;
tbjazic 3:286a364f952f 20 i++;
tbjazic 3:286a364f952f 21 if (i < N) {
tbjazic 3:286a364f952f 22 ticker.attach(this, &Blinker::turnLedOn, t);
tbjazic 3:286a364f952f 23 } else {
tbjazic 3:286a364f952f 24 almostFinished = true;
tbjazic 3:286a364f952f 25 }
tbjazic 3:286a364f952f 26 } else {
tbjazic 3:286a364f952f 27 finished = true;
tbjazic 0:c9a302c4bed9 28 }
tbjazic 3:286a364f952f 29 }
tbjazic 3:286a364f952f 30
tbjazic 3:286a364f952f 31 void Blinker::turnLedOff() {
tbjazic 3:286a364f952f 32 myled = 0;
tbjazic 3:286a364f952f 33 if (almostFinished) {
tbjazic 3:286a364f952f 34 timeout.attach(this, &Blinker::done, period/2);
tbjazic 3:286a364f952f 35 }
tbjazic 3:286a364f952f 36 }
tbjazic 3:286a364f952f 37
tbjazic 3:286a364f952f 38 void Blinker::turnLedOn() {
tbjazic 3:286a364f952f 39 myled = 1;
tbjazic 3:286a364f952f 40 timeout.attach(this, &Blinker::turnLedOff, period/2);
tbjazic 3:286a364f952f 41 if (++i >= N) {
tbjazic 3:286a364f952f 42 ticker.detach();
tbjazic 3:286a364f952f 43 almostFinished = true;
tbjazic 3:286a364f952f 44 }
tbjazic 3:286a364f952f 45 }
tbjazic 3:286a364f952f 46 void Blinker::done() {
tbjazic 3:286a364f952f 47 finished = true;
tbjazic 3:286a364f952f 48 }
tbjazic 3:286a364f952f 49
tbjazic 3:286a364f952f 50 bool Blinker::isFinished() {
tbjazic 3:286a364f952f 51 return finished;
tbjazic 0:c9a302c4bed9 52 }