University of Plymouth - Stages 1, 2 and 3 / Mbed OS Task330
Revision:
0:397b84c74d17
Child:
1:e84a51c98d75
diff -r 000000000000 -r 397b84c74d17 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Oct 23 09:37:46 2017 +0000
@@ -0,0 +1,82 @@
+#include "mbed.h"
+#include "SWPoll.hpp"
+
+#define N 1000000
+#define RELEASED 0
+#define PRESSED  1
+
+//Function prototypes
+void task1();
+void task2();
+
+//Hardware objects
+DigitalOut red_led(PE_15);     //CountUp is in its critical section
+DigitalOut yellow_led(PB_10);  //CountDown is in its critical section
+DigitalOut green_led(PB_11);   //counter != 0
+DigitalOut onboardLED(LED1);
+
+DigitalIn button(USER_BUTTON);
+DigitalIn sw1(PE_12);
+DigitalIn sw2(PE_14);
+
+SWPoll switch1(sw1, red_led);
+SWPoll switch2(sw2, green_led);
+
+
+
+int main() {
+    
+    //Main uses a Timer
+    yellow_led = 1;
+    Timer t;
+        
+    //Now loop forever
+    t.start();
+    while(1) { 
+        //Flash the yellow on the "main thread"
+        if (t.read_ms() >= 500) {
+            yellow_led = !yellow_led;   
+            t.reset(); 
+        }
+        switch1.poll();
+        switch2.poll();
+        
+    };
+}
+
+
+//Thread 1  - polling sw1 and controlling the red LED
+void task1()
+{
+    //Loop forever
+    while(1) {
+        //Spin on sw1
+        while (sw1 == RELEASED) {};
+        //Allow short delay for switch bounce
+        Thread::wait(200);
+        //Spin again on sw1
+        while (sw1 == PRESSED) {};    
+        //Toggle LED
+        red_led = !red_led;
+        //Again, wait for switch bounce
+        Thread::wait(200);
+    }    
+}
+
+//Thread 2  - polling sw2 and controlling the green LED
+void task2()
+{
+    //Loop forever
+    while(1) {
+        //Spin on sw2
+        while (sw2 == RELEASED) {};
+        //Allow short delay for switch bounce
+        Thread::wait(200);
+        //Spin again on sw2
+        while (sw2 == PRESSED) {};    
+        //Toggle LED
+        green_led = !green_led;
+        //Again, wait for switch bounce
+        Thread::wait(200);
+    }    
+}