lab 5 part 2

Dependencies:   TSI

Revision:
5:2a9a3d74a1d8
Parent:
4:d54e74fbf82c
Child:
6:71ef35e456ab
diff -r d54e74fbf82c -r 2a9a3d74a1d8 main.cpp
--- a/main.cpp	Thu Feb 22 16:59:24 2018 +0000
+++ b/main.cpp	Tue Feb 19 09:38:31 2019 +0000
@@ -14,27 +14,31 @@
 
 Thread redThread ; // thread for red LED
 Thread greenThread ; // thread for green LED
+
+# define REDFLAG 0x01   
+# define GREENFLAG 0x02
+EventFlags signals;  // event flags for signalling; 2 used
  
 void red_thread() {  // method to run in thread
     while (true) {
-        Thread::signal_wait(0x1);
-        redLED = false ; // turn on 
-        Thread::wait(5000);
+        signals.wait_any(REDFLAG);
+        redLED = false ; // turn on
+        wait(5.0);
         redLED = true ; // turn off 
-        redThread.signal_clr(0x1) ;
-          // Signal are automatically cleared by wait_signal but
+        signals.clear(REDFLAG) ;
+          // Signal are automatically cleared by wait_any but
           // the signal might have been set again while LED on 
     }
 }
 
 void green_thread() {  // method to run in thread
     while (true) {
-        Thread::signal_wait(0x1);
+        signals.wait_any(GREENFLAG);
         greenLED = false ; // turn on 
-        Thread::wait(5000);
+        wait(5.0);
         greenLED = true ; // turn off 
-        greenThread.signal_clr(0x1) ;
-          // Signal are automatically cleared by wait_signal but
+        signals.clear(GREENFLAG) ;
+          // Signal are automatically cleared by wait_any but
           // the signal might have been set again while LED on 
     }
 }
@@ -42,8 +46,8 @@
 int main(void) {
     redLED = true ; // turn off 
     greenLED = true ; // turn off 
-    redThread.start(&red_thread) ; // start the red thread
-    greenThread.start(&green_thread) ; // start the green thread
+    redThread.start(red_thread) ; // start the red thread
+    greenThread.start(green_thread) ; // start the green thread
     
     while (true) {
         uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
@@ -51,9 +55,9 @@
                                           // Left --> low value  Right --> high value
         pc.printf("%d", d) ;  
         pc.putc(' ') ;
-        if (d == 10) redThread.signal_set(0x1) ;
-        if (d == 20) greenThread.signal_set(0x1) ;
-        Thread::wait(200);  // This polling rate is too slow - increase it
-                            // The slower rate maks it easier to output on the terminal
+        if (d == 10) signals.set(REDFLAG) ;
+        if (d == 20) signals.set(GREENFLAG) ;
+        wait(0.5);  // This polling rate is too slow - increase it
+                    // The slower rate maks it easier to output on the terminal
     }
 }
\ No newline at end of file