Dependencies:   ELEC350-Practicals-FZ429

Fork of Task622Solution-mbedos54 by Nicholas Outram

Revision:
10:730250526509
Parent:
9:c46e831f8e4a
Child:
11:9dcbcda8ea12
--- a/main.cpp	Mon Apr 03 14:02:26 2017 +0000
+++ b/main.cpp	Tue Nov 14 14:12:15 2017 +0000
@@ -1,5 +1,7 @@
 #include "mbed.h"
-#include "rtos.h"
+#include <iostream>
+#include "sample_hardware.hpp"
+
 #include "string.h"
 #include <stdio.h>
 #include <ctype.h>
@@ -10,33 +12,17 @@
 void thread2();
 void switchISR();
 
-//Digital outputs
-DigitalOut onBoardLED(LED1);
-DigitalOut redLED(D7);
-DigitalOut yellowLED(D6);
-DigitalOut greenLED(D5);
-
-//Serial Interface
-Serial pc(USBTX, USBRX);
-
-//Digital inputs
-DigitalIn  onBoardSwitch(USER_BUTTON);
-InterruptIn  sw1(D4);
-DigitalIn    sw2(D3);
+InterruptIn  sw(PE_12);
 
 //Threads
-Thread *t1;
-Thread *t2;
-
-//Thread ID for the Main function (CMSIS API)
-osThreadId tidMain;
-osThreadId tid1;
-osThreadId tid2;
+Thread t1(osPriorityRealtime);  //Higher priority
+Thread t2(osPriorityNormal);
 
 
 //Called on the falling edge of a switch
 void switchISR() {
-     t1->signal_set(SWITCH1_RELEASE);    //Very short
+     sw.fall(NULL);
+     t1.signal_set(SWITCH1_RELEASE);    //Very short
 }
 
 //High priority thread
@@ -46,10 +32,17 @@
     while (true) {
          Thread::signal_wait(SWITCH1_RELEASE);
          redLED = !redLED;
-         Thread::wait(1000);
-         redLED = !redLED;
-         Thread::wait(1000);  
-         t1->signal_clr(SWITCH1_RELEASE);   //Debounce
+         sw.fall(NULL);  //Turn off any further interrupts
+         Thread::wait(200); 
+         
+         //Block using BUSY-WAIT
+//         Timer t;
+//         t.start();
+//         while (t < 2);
+         
+         //Clear any additional signals
+         t1.signal_clr(SWITCH1_RELEASE);   //Debounce - clear pending signals
+         sw.fall(switchISR);
     }
 }
 
@@ -57,9 +50,8 @@
 void thread2() 
 {
     greenLED = 1; 
-    while (true) {
-        //Thread::wait(2000);    
-        Thread::wait(500);          // WAIT FOR 0.5 SECONDS
+    while (true) {   
+        Thread::wait(500);          // WAIT FOR 0.5 SECONDS - spinning
         greenLED = !greenLED;
     }
 }
@@ -67,26 +59,18 @@
 
 // Main thread
 int main() {
-    redLED    = 0;
-    yellowLED = 0;
-    greenLED  = 0;
-           
-    //Threads
-    t1 = new Thread(osPriorityRealtime);
-    t2 = new Thread(osPriorityNormal);              
-       
-    t1->start(thread1);
-    t2->start(thread2);
-           
-    // Thread IDs
-    tidMain = Thread::gettid();  
-    tid1    = t1->gettid();
-    tid2    = t2->gettid();
+        
+    //Power on self-test       
+    post();
+    
+    //Start Threads
+    t1.start(thread1);
+    t2.start(thread2);
     
     //Hook up interrupt
-    sw1.fall(switchISR);      
+    sw.fall(switchISR);      
         
-    pc.printf("Main Thread\n");
+    printf("Main Thread\n");
     while (true) {
         Thread::wait(osWaitForever);
     }