Lab6 part 1

Revision:
6:8b0ca28c88a4
Parent:
3:32a940251192
Child:
7:e49685bca7c7
--- a/main.cpp	Tue Feb 27 10:29:30 2018 +0000
+++ b/main.cpp	Wed Feb 27 15:54:07 2019 +0000
@@ -1,5 +1,4 @@
 #include "mbed.h"
-#include "rtos.h"
 #include "wdt.h"
 
 // Sample program using the Watchdog
@@ -15,6 +14,8 @@
 DigitalOut led1(PTC12, OFF);
 DigitalOut led2(PTC13, OFF);
 
+Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
+
 // This ticker is used to feed the watch dog
 Ticker tick;
 
@@ -23,6 +24,13 @@
 Thread threadLED1 ; // thread LED1
 Thread threadLED2 ; // thread LED2
 
+# define ON1 0x01   
+# define ON2 0x02
+# define OFF1 0x04   
+# define OFF2 0x08
+EventFlags signals;  // event flags for signalling
+
+
 // ------------Fault Injection Button-------------
 //  Wait while the button is down
 //     Use this to simulate a STUCK fault
@@ -35,13 +43,11 @@
 //   Turn LED1 on/off in response to signals 
 // -----------------------------------------------
 void led1_thread() {  // method to run in thread
-    osEvent evt ;
+    int evt ;
     while (true) {
-        evt = Thread::signal_wait(0x0); // wait for any signal
-        if (evt.status == osEventSignal) {
-            if (evt.value.signals & 0x01) led1 = ON ;
-            if (evt.value.signals & 0x02) led1 = OFF ;
-        } 
+        evt = signals.wait_any(ON1 | OFF1); // wait for either signal
+        if (evt & ON1) led1 = ON ;
+        if (evt & OFF1) led1 = OFF ;
         waitButton() ;  // POSSIBLE FAULT HERE
     }
 }
@@ -50,13 +56,11 @@
 //   Turn LED2 on/off in response to signals 
 // -----------------------------------------------
 void led2_thread() {  // method to run in thread
-    osEvent evt ;
+    int evt ;
     while (true) {
-        evt = Thread::signal_wait(0x0); // wait for any signal
-        if (evt.status == osEventSignal) {
-            if (evt.value.signals & 0x01) led2 = ON ;
-            if (evt.value.signals & 0x02) led2 = OFF ;
-        } 
+        evt = signals.wait_any(ON2 | OFF2); // wait for any signal
+        if (evt & ON2) led2 = ON ;
+        if (evt & OFF2) led2 = OFF ;
         // waitButton() ; // POSSIBLE FAULT HERE
     }
 }
@@ -66,12 +70,10 @@
 // -----------------------------------------------
 void timer_thread() {  // method to run in thread
     while (true) {
-        Thread::wait(250) ;
-        threadLED1.signal_set(0x1) ;
-        threadLED2.signal_set(0x1) ;
-        Thread::wait(250) ;
-        threadLED1.signal_set(0x2) ;
-        threadLED2.signal_set(0x2) ;
+        wait(1.0) ;
+        signals.set(ON1 | ON2) ;
+        wait(1.0) ;
+        signals.set(OFF1 | OFF2) ;
         // waitButton() ; // POSSIBLE FAULT HERE
     }
 }
@@ -86,15 +88,17 @@
 
 int main(void) {
     wdt_32ms() ; // initialise watchdog - 32ms timeout
+    wdt_kick_all() ;
     tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms    
     
     // start threads
-    threadT.start(&timer_thread) ; // start the timer thread 
-    threadLED1.start(&led1_thread) ; // start the LED1 control thread 
-    threadLED2.start(&led2_thread) ; // start the LED2 control thread 
+    threadT.start(timer_thread) ; // start the timer thread 
+    threadLED1.start(led1_thread) ; // start the LED1 control thread 
+    threadLED2.start(led2_thread) ; // start the LED2 control thread 
     
     // show start-up
     led_red = OFF;
-    Thread::wait(1000) ;
+    wait(5.0) ;
     led_red = ON;
+    // main thread terminates
 }
\ No newline at end of file