Simple library for LED blinking.

Dependents:   roam_v2 finalV1 finalV1 finalv2 ... more

Revision:
3:286a364f952f
Parent:
2:190915d53c0b
diff -r 190915d53c0b -r 286a364f952f Blinker.cpp
--- 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