633 solution updated for mbed os 54

Fork of Task633Solution-mbedos54 by Stage-1 Students SoCEM

Revision:
8:c5663f5fa848
Parent:
7:cd015e83995a
Child:
9:31031bbb59c7
--- a/main.cpp	Mon Mar 14 16:32:56 2016 +0000
+++ b/main.cpp	Mon Mar 14 19:48:24 2016 +0000
@@ -4,7 +4,11 @@
 #include <stdio.h>
 #include <ctype.h>
 
-#define DELAY 200
+#define SWITCH1_RELEASE 1
+
+void thread1( const void*  );
+void thread2( const void*  );
+void switchISR();
 
 //Digital outputs
 DigitalOut onBoardLED(LED1);
@@ -17,65 +21,79 @@
 
 //Digital inputs
 DigitalIn  onBoardSwitch(USER_BUTTON);
-DigitalIn  SW1(D4);
-DigitalIn  SW2(D3);
+DigitalIn  sw1(D4);                         //CONSIDER CHANGING THIS TO AN INTERRUPT
+DigitalIn  sw2(D3);
+
+//Threads
+Thread *t1;
+Thread *t2;
 
 //Thread ID for the Main function (CMSIS API)
 osThreadId tidMain;
+osThreadId tid1;
+osThreadId tid2;
 
+
+//TBD: Call this on the falling edge of SW1
+void switchISR() {
+     //TBD
+}
+
+//High priority thread
 void thread1( const void* arg ) 
 {
-    pc.printf("Entering thread 1\n");
+    redLED = 1;
     while (true) {
-        yellowLED = 1;
-        Thread::wait(DELAY);   
-        yellowLED = 0;
-        Thread::wait(DELAY);               
+         
+         // THIS IS BAD BAD BAD BAD BAD BAD
+         // BLOCK ON FALLING EDGE OF SW1 BY RAPID POLLING (SPINNING)
+         // Thread is blocked in the RUNNING state 
+         while (sw1 == 0);
+         wait_ms(200);      //Wait for debounce
+         while (sw1 == 1);
+         // TODO: FIX THIS! GET THE INTERRUPT TO SIGNAL THIS THREAD
+         
+         redLED = !redLED;
+         Thread::wait(1000);    //Thread in WAITING state
+         redLED = !redLED;
+         Thread::wait(1000);    //Thread in WAITING state
     }
 }
 
-//This thread has higher priority
+// This thread has normal priority 
+// It is supposed to flash the green LED every second.
+// THIS IS NOT WORKING because it is currently being starved by thread1 (while polling the switch)
 void thread2( const void* arg ) 
 {
-    pc.printf("Entering thread 2\n");  
-    while (true) {
-        redLED = 1;
-        if (SW1 == 1) {
-            
-            // 1) Select the 'type' of wait
-            
-            //wait_ms(osWaitForever);
-            Thread::wait(osWaitForever); 
-        } else {
-            Thread::wait(DELAY);    
-        }
-        redLED = 0;
-        Thread::wait(DELAY);               
+    greenLED = 1; 
+    while (true) {   
+        Thread::wait(500);
+        greenLED = !greenLED;
     }
 }
 
 
-//Main thread
+// Main thread
 int main() {
     redLED    = 0;
     yellowLED = 0;
     greenLED  = 0;
-                
-    //Main thread ID
+           
+    //Threads
+    t1 = new Thread(&thread1, NULL, osPriorityRealtime);    //HIGH PRIORITY
+    t2 = new Thread(&thread2, NULL, osPriorityNormal);              
+              
+    // Thread IDs
     tidMain = Thread::gettid();  
-    
-    Thread t1(thread1, NULL, osPriorityNormal);
+    tid1    = t1->gettid();
+    tid2    = t2->gettid();
     
-    // 2) Select the Thread Priority
-    
-    //Thread t2(thread2, NULL, osPriorityNormal);
-    Thread t2(thread2, NULL, osPriorityAboveNormal);
-    
+    //TBD: Hook up interrupt    
+        
     pc.printf("Main Thread\n");
     while (true) {
         Thread::wait(osWaitForever);
     }
-
 }