BlinkThreadCallback2LED

Files at this revision

API Documentation at this revision

Comitter:
alessioburatti
Date:
Fri Nov 16 12:00:34 2018 +0000
Parent:
0:13988e5bca16
Commit message:
Done

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Nov 08 17:29:13 2018 +0000
+++ b/main.cpp	Fri Nov 16 12:00:34 2018 +0000
@@ -1,21 +1,42 @@
 #include "mbed.h"
 
-Thread thread;
 DigitalOut led1(LED1);
-volatile bool running = true;
+DigitalOut led2(LED2);
+
+Thread thread1;
+Thread thread2;
+
+bool is_thread1_done = false;
+bool is_thread2_done = true;
 
-// Blink function toggles the led in a long running loop
-void blink(DigitalOut *led) {
-    while (running) {
-        *led = !*led;
-        wait(1);
+void blink(bool * this_is_done, DigitalOut * led) {
+    is_thread1_done = false;
+    is_thread2_done = false;
+    *led = 1;
+    wait(1);
+    *led = 0;
+    *this_is_done = true;
+}
+                   
+void led1_thread(DigitalOut * led) {
+    while (true) {
+        if(is_thread2_done){
+            blink(&is_thread1_done, led);
+        }
     }
 }
 
-// Spawns a thread to run blink for 5 seconds
+void led2_thread(DigitalOut * led) {
+    while (true) {
+        if(is_thread1_done){
+            blink(&is_thread2_done, led);
+        }
+    }
+}
+
 int main() {
-    thread.start(callback(blink, &led1));
-    wait(5);
-    running = false;
-    thread.join();
+    thread1.start(callback(led1_thread, &led1));
+    thread2.start(callback(led2_thread, &led2));
+    thread1.join();
+    thread2.join();
 }