Modify the BlinkThreadCallback example so you run two threads that blink different LEDs once per second by waiting for each other
Revision 1:30693312f670, committed 2018-11-11
- Comitter:
- vicara
- Date:
- Sun Nov 11 21:19:55 2018 +0000
- Parent:
- 0:11ca69d691b7
- Commit message:
- Thread blink done with debug mode enabled;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sun Nov 11 15:55:24 2018 +0000
+++ b/main.cpp Sun Nov 11 21:19:55 2018 +0000
@@ -1,19 +1,66 @@
#include "mbed.h"
-Thread thread;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
+InterruptIn button(USER_BUTTON);
-// Blink function toggles the led in a long running loop
-void blink(DigitalOut *led) {
- *led = !*led;
- wait(1);
+//____________________________
+
+Thread thread1;
+Thread thread2;
+
+//____________________________
+
+bool is_debug_enabled = false;
+bool is_t2_done = true;
+bool is_t1_done = false;
+
+void toggle_debug_mode() {
+ is_debug_enabled = !is_debug_enabled;
}
-
-int main() {
- while(true){
- thread.start(callback(blink, &led1));
- thread.join();
- thread.start(callback(blink, &led2));
+
+void led1_thread(DigitalOut *led) {
+ if(is_debug_enabled)
+ printf("T1 STARTED\n");
+ while (true) {
+ if(is_t2_done){
+ if(is_debug_enabled)
+ printf("T1 EXECUTING\n");
+ is_t1_done = false;
+ is_t2_done = false;
+ *led = !*led;
+ wait(1);
+ *led = !*led;
+ is_t1_done = true;
+ if(is_debug_enabled)
+ printf("T1 DONE\n\n");
+ }
}
}
+
+void led2_thread(DigitalOut *led) {
+ if(is_debug_enabled)
+ printf("T2 STARTED\n");
+ while (true) {
+ if(is_t1_done){
+ if(is_debug_enabled)
+ printf("T2 EXECUTING\n");
+ is_t1_done = false;
+ is_t2_done = false;
+ *led = !*led;
+ wait(1);
+ *led = !*led;
+ is_t2_done = true;
+ if(is_debug_enabled)
+ printf("T2 DONE\n\n");
+ }
+ }
+}
+
+int main() {
+ button.rise(&toggle_debug_mode);
+ thread1.start(callback(led1_thread, &led1));
+ thread2.start(callback(led2_thread, &led2));
+ thread1.join();
+ thread2.join();
+}