Lab5-1

Dependencies:   TSI

Fork of TSI_sample by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
Peilingyi
Date:
Fri Mar 02 20:53:57 2018 +0000
Parent:
4:d54e74fbf82c
Commit message:
Lab5-1

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 22 16:59:24 2018 +0000
+++ b/main.cpp	Fri Mar 02 20:53:57 2018 +0000
@@ -10,40 +10,73 @@
 Serial pc(USBTX, USBRX); // tx, rx
 DigitalOut redLED(LED_RED);
 DigitalOut greenLED(LED_GREEN);
+DigitalOut blueLED(LED_BLUE);
+DigitalOut LEDout(D0);
+
 TSISensor tsi;
 
-Thread redThread ; // thread for red LED
-Thread greenThread ; // thread for green LED
+Thread redThread(osPriorityNormal,1000) ; // thread for red LED
+Thread greenThread(osPriorityNormal,1000) ; // thread for green LED
+Thread blueThread(osPriorityNormal,1000) ; // thread for blue LED
+Thread mixcolorThread(osPriorityNormal,1000) ; // thread for mix LED
  
-void red_thread() {  // method to run in thread
+void red_thread() {  // methbd to run in thread
     while (true) {
-        Thread::signal_wait(0x1);
-        redLED = false ; // turn on 
-        Thread::wait(5000);
-        redLED = true ; // turn off 
+        Thread::signal_wait(0x1); 
+        
+        redLED = !redLED; // turn on 
+     //   blueLED = 1;
+      //  greenLED = 1;    
         redThread.signal_clr(0x1) ;
           // Signal are automatically cleared by wait_signal but
           // the signal might have been set again while LED on 
     }
 }
 
-void green_thread() {  // method to run in thread
+void blue_thread() {  // methbd to run in thread
     while (true) {
-        Thread::signal_wait(0x1);
-        greenLED = false ; // turn on 
-        Thread::wait(5000);
-        greenLED = true ; // turn off 
+        Thread::signal_wait(0x1); 
+        //redLED = 1; // turn on 
+        blueLED = !blueLED;
+        //greenLED = 1;    
+        blueThread.signal_clr(0x1) ;
+          // Signal are automatically cleared by wait_signal but
+          // the signal might have been set again while LED on 
+    }
+}
+
+void green_thread() {  // methbd to run in thread
+    while (true) {
+        Thread::signal_wait(0x1); 
+        //redLED = 1; // turn on 
+        //blueLED = 1;
+        greenLED = !greenLED;    
         greenThread.signal_clr(0x1) ;
           // Signal are automatically cleared by wait_signal but
           // the signal might have been set again while LED on 
     }
 }
 
+void mixcolor_thread() {  // methbd to run in thread
+    while (true) {
+        Thread::signal_wait(0x1); 
+        LEDout =  !LEDout;    
+        mixcolorThread.signal_clr(0x1) ;
+          // Signal are automatically cleared by wait_signal but
+          // the signal might have been set again while LED on 
+    }
+}
+
 int main(void) {
-    redLED = true ; // turn off 
-    greenLED = true ; // turn off 
+    uint8_t pos = 0;
+    redLED = 1; // turn off 
+    greenLED = 1; // turn off 
+    blueLED = 1;
+    LEDout = 0;
     redThread.start(&red_thread) ; // start the red thread
     greenThread.start(&green_thread) ; // start the green thread
+    blueThread.start(&blue_thread) ; // start the blue thread
+    mixcolorThread.start(&mixcolor_thread) ; // start the mixcolor thread
     
     while (true) {
         uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
@@ -51,9 +84,76 @@
                                           // Left --> low value  Right --> high value
         pc.printf("%d", d) ;  
         pc.putc(' ') ;
-        if (d == 10) redThread.signal_set(0x1) ;
-        if (d == 20) greenThread.signal_set(0x1) ;
-        Thread::wait(200);  // This polling rate is too slow - increase it
+        if (pos == 0)//first touch
+        {
+            if ((d>3) &&(d<9)) 
+            {
+                 redThread.signal_set(0x1);
+                 pos=1 ;
+            }
+          
+            if ((d>13) &&(d<19)) 
+            {
+                pos =2;
+                greenThread.signal_set(0x1);
+            }
+            if ((d>23) &&(d<29)) 
+            {
+                blueThread.signal_set(0x1) ;
+                pos =3;
+            }
+            if (d>33) 
+            {
+                 mixcolorThread.signal_set(0x1);
+                 pos = 4;
+            }
+            
+        }
+        
+        switch (pos)
+        {
+             
+           
+            case 1:
+            {
+                if ((d<3) ||(d>9))
+                {
+               //     redThread.signal_set(0x1) ;
+                    pos = 0;
+                }
+                break;
+            }
+            case 2:
+            {
+                if ((d<13) ||(d>19))
+                {
+           //         greenThread.signal_set(0x1) ;
+                    pos = 0;
+                }
+                break;
+            }
+            case 3:
+            {
+                if ((d<23) ||(d>29))
+                {
+         //           blueThread.signal_set(0x1) ;
+                    pos = 0;
+                }
+                break;
+            }
+            case 4:
+            {
+                if (d<33)
+                {
+        //            mixcolorThread.signal_set(0x1) ;
+                    pos = 0;
+                }
+                break;
+            }
+        }
+                
+           
+        Thread::wait(100);  // This polling rate is too slow - increase it
                             // The slower rate maks it easier to output on the terminal
     }
 }
\ No newline at end of file