Haozun Sun / Mbed OS Lab5_180773877_hzsun

Dependencies:   TSI

Files at this revision

API Documentation at this revision

Comitter:
hzsun
Date:
Thu Mar 19 22:05:35 2020 +0000
Parent:
7:da68b308351c
Commit message:
lab5

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r da68b308351c -r 2e9406f997c8 main.cpp
--- a/main.cpp	Wed Mar 18 15:06:50 2020 +0000
+++ b/main.cpp	Thu Mar 19 22:05:35 2020 +0000
@@ -1,17 +1,15 @@
 #include "mbed.h"
 #include "TSISensor.h"
-
-// Example program for lab 5
+//  program for lab 5
 // -------------------------
-//  A value is read from the touch sensor and use
-//    to control two LEDs
-//  The value is also output to the serial interface
+// Implement the functions of four virtual buttons
 
-Serial pc(USBTX, USBRX); // tx, rx
-DigitalOut redLED(D8); // red led port
-DigitalOut greenLED(D7); // green led port
-DigitalOut blueLED(D9); //blue led port
-DigitalOut yellowLED(D6); // yellow led port
+Serial pc(USBTX, USBRX);   // tx, rx
+DigitalOut yellowLED(D6);     // yellow led port
+DigitalOut greenLED(D7);      // green led port
+DigitalOut redLED(D8);         // red led port
+DigitalOut blueLED(D9);        //blue led port
+
 
 TSISensor tsi;
 
@@ -23,87 +21,104 @@
 # define GREENFLAG 0x02
 # define BLUEFLAG 0x04
 # define YELLOWFLAG 0x08
-EventFlags signals;  // event flags for signalling; 2 used
-enum pos {none, leftIn, leftOut, rightIn, rightOut}; // array for all the states
 
-void yellow_thread() {  // method to run in thread
-    while (true) {
-        signals.wait_any(YELLOWFLAG);
-        yellowLED = !yellowLED ; // turn on or off
-        ThisThread::sleep_for(1000) ; // wait(1.0);
-        signals.clear(YELLOWFLAG) ;
-          // Signal are automatically cleared by wait_any 
-    }
-}
-void blue_thread() {  // method to run in thread
-    while (true) {
-        signals.wait_any(BLUEFLAG);
-        blueLED = !blueLED ; // turn on or off
-        ThisThread::sleep_for(1000) ; // wait(1.0);
-        signals.clear(BLUEFLAG) ;
-          // Signal are automatically cleared by wait_any 
-    }
-}
-void red_thread() {  // method to run in thread
+EventFlags signals;  // eventflag signal, uesd for comunicate 
+enum pos {none, leftIn, leftOut, rightIn, rightOut}; // 5 states for the buttons
+
+void red_thread() {  // thread for red LED
     while (true) {
         signals.wait_any(REDFLAG);
-        redLED = !redLED ; // turn on or off
-        ThisThread::sleep_for(1000) ; // wait(1.0);
-        signals.clear(REDFLAG) ;
-          // Signal are automatically cleared by wait_any 
+        redLED = !redLED ; //  on&off
+        ThisThread::sleep_for(1000) ; // delay
+        signals.clear(REDFLAG) ;        // clear the signal flag for futher use
     }
 }
 
-void green_thread() {  // method to run in thread
+void green_thread() {  // thread for green LED
     while (true) {
         signals.wait_any(GREENFLAG);
-        greenLED = !greenLED ; // turn on 
-        ThisThread::sleep_for(1000) ; // wait(1.0);
-        signals.clear(GREENFLAG) ;
-          // Signal are automatically cleared by wait_any
+        greenLED = !greenLED ; //  on&off
+        ThisThread::sleep_for(1000) ; // delay
+        signals.clear(GREENFLAG) ;        // clear the signal flag for futher use
+    }
+}
+
+void blue_thread() {  // thread for blue LED
+    while (true) {
+        signals.wait_any(BLUEFLAG);
+        blueLED = !blueLED ; //  on&off
+        ThisThread::sleep_for(1000) ; // delay
+        signals.clear(BLUEFLAG) ;        // clear the signal flag for futher use
+    }
+}
+
+void yellow_thread()    // thread for yellow LED
+{
+    while (true) {
+        signals.wait_any(YELLOWFLAG);
+        yellowLED = !yellowLED ; //  on&off
+        ThisThread::sleep_for(1000) ; // delay
+        signals.clear(YELLOWFLAG) ;        // clear the signal flag for futher use
     }
 }
 
 int main(void) {
-    redLED = 0 ; // turn off red led
-    greenLED = 0 ; // turn off green led
-    blueLED = 0; // turn off blue led
-    yellowLED = 0; // turn off yellow led
+    redLED = 0 ; 
+    greenLED = 0 ; 
+    blueLED = 0; 
+    yellowLED = 0;  // beginning state for the four LEDs
     int pos = none; // initialise state
-    redThread.start(red_thread) ; // start the red thread
-    greenThread.start(green_thread) ; // start the green thread
-    blueThread.start(blue_thread);// start the blue thread
-    yellowThread.start(yellow_thread);// start the yellow thread
+    redThread.start(red_thread) ; //  red LED thread
+    greenThread.start(green_thread) ; //  green LED thread
+    blueThread.start(blue_thread);//  blue LED thread
+    yellowThread.start(yellow_thread);// yellow LED thread
     
     while (true) {
-        uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
-        switch (pos){ // depending on state
+        uint8_t d = tsi.readDistance() ;  // read the variables form the touchpad
+        switch (pos){ 
+        
             case none: // none state
-                if (d > 3 && d < 9) {signals.set(YELLOWFLAG);pos = leftOut;} // if in range inside 3 - 9, calls yellow led thread and changes state to left out
-                if (d > 13 && d < 19) {signals.set(BLUEFLAG);pos = leftIn;}// if in range inside 13 - 19, calls blue led thread and changes state to left in
-                if (d > 23 && d < 29) {signals.set(GREENFLAG);pos = rightIn;}// if in range inside 23 - 29, calls green led thread and changes state to right in
-                if (d > 33) {signals.set(REDFLAG);pos = rightOut;}// if in range d bigger 33 , calls red led thread and changes state to right out
+                if (d > 3 && d < 9) 
+                {signals.set(YELLOWFLAG);pos = leftOut;}
+                 // if in range inside 3 - 9, left out button
+ 
+                if (d > 13 && d < 19) 
+                {signals.set(BLUEFLAG);pos = leftIn;}
+                // if in range inside 13 - 19, leftin button
+
+                if (d > 23 && d < 29) 
+                {signals.set(GREENFLAG);pos = rightIn;}
+                // if in range inside 23 - 29, right in button
+
+                if (d > 33) 
+                {signals.set(REDFLAG);pos = rightOut;}
+                // if in range d bigger 33 ,right out button
                 break;
-            case leftIn: // case left in
-                if(d < 13 || d > 19) pos = none; // if d is out of the range, state changed to none
+                
+            case leftIn: 
+                if(d < 13 || d > 19) 
+                pos = none; // if out of the range, state goes back to none
                 break;
-            case leftOut:// case left out
-                if(d < 3 || d > 9) pos = none;// if d is out of the range, state changed to none
+                
+            case leftOut:
+                if(d < 3 || d > 9) 
+                pos = none;// if out of the range, state goes back to none
                 break;
-            case rightIn:// case right in
-                if(d < 23 || d > 29) pos = none;// if d is out of the range, state changed to none
+            case rightIn:
+            // case right in
+                if(d < 23 || d > 29) 
+                pos = none;// if  out of the range, state goes back to none
                 break;
             case rightOut:// case right out
-                if(d < 33) pos = none;// if d is out of the range, state changed to none
+                if(d < 33) 
+                pos = none;// if  out of the range, state goes back to none
                 break;
         }
 
-                                          // When no touch --> 0
-                                          // Left --> low value  Right --> high value
         pc.printf("%d", d) ;  
         pc.printf("%d", pos) ;  
         pc.putc(' ') ;
-        ThisThread::sleep_for(1000) ; // This polling rate is too slow - increase it
-                    // The slower rate maks it easier to output on the terminal
+        ThisThread::sleep_for(1000) ;
+ 
     }
 }
\ No newline at end of file