Peiling Yi / Mbed OS TSI_week5_01

Dependencies:   TSI

Fork of TSI_sample by William Marsh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TSISensor.h"
00003 
00004 // Example program for lab 5
00005 // -------------------------
00006 //  A value is read from the touch sensor and use
00007 //    to control two LEDs
00008 //  The value is also output to the serial interface
00009 
00010 Serial pc(USBTX, USBRX); // tx, rx
00011 DigitalOut redLED(LED_RED);
00012 DigitalOut greenLED(LED_GREEN);
00013 DigitalOut blueLED(LED_BLUE);
00014 DigitalOut LEDout(D0);
00015 
00016 TSISensor tsi;
00017 
00018 Thread redThread(osPriorityNormal,1000) ; // thread for red LED
00019 Thread greenThread(osPriorityNormal,1000) ; // thread for green LED
00020 Thread blueThread(osPriorityNormal,1000) ; // thread for blue LED
00021 Thread mixcolorThread(osPriorityNormal,1000) ; // thread for mix LED
00022  
00023 void red_thread() {  // methbd to run in thread
00024     while (true) {
00025         Thread::signal_wait(0x1); 
00026         
00027         redLED = !redLED; // turn on 
00028      //   blueLED = 1;
00029       //  greenLED = 1;    
00030         redThread.signal_clr(0x1) ;
00031           // Signal are automatically cleared by wait_signal but
00032           // the signal might have been set again while LED on 
00033     }
00034 }
00035 
00036 void blue_thread() {  // methbd to run in thread
00037     while (true) {
00038         Thread::signal_wait(0x1); 
00039         //redLED = 1; // turn on 
00040         blueLED = !blueLED;
00041         //greenLED = 1;    
00042         blueThread.signal_clr(0x1) ;
00043           // Signal are automatically cleared by wait_signal but
00044           // the signal might have been set again while LED on 
00045     }
00046 }
00047 
00048 void green_thread() {  // methbd to run in thread
00049     while (true) {
00050         Thread::signal_wait(0x1); 
00051         //redLED = 1; // turn on 
00052         //blueLED = 1;
00053         greenLED = !greenLED;    
00054         greenThread.signal_clr(0x1) ;
00055           // Signal are automatically cleared by wait_signal but
00056           // the signal might have been set again while LED on 
00057     }
00058 }
00059 
00060 void mixcolor_thread() {  // methbd to run in thread
00061     while (true) {
00062         Thread::signal_wait(0x1); 
00063         LEDout =  !LEDout;    
00064         mixcolorThread.signal_clr(0x1) ;
00065           // Signal are automatically cleared by wait_signal but
00066           // the signal might have been set again while LED on 
00067     }
00068 }
00069 
00070 int main(void) {
00071     uint8_t pos = 0;
00072     redLED = 1; // turn off 
00073     greenLED = 1; // turn off 
00074     blueLED = 1;
00075     LEDout = 0;
00076     redThread.start(&red_thread) ; // start the red thread
00077     greenThread.start(&green_thread) ; // start the green thread
00078     blueThread.start(&blue_thread) ; // start the blue thread
00079     mixcolorThread.start(&mixcolor_thread) ; // start the mixcolor thread
00080     
00081     while (true) {
00082         uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
00083                                           // When no touch --> 0
00084                                           // Left --> low value  Right --> high value
00085         pc.printf("%d", d) ;  
00086         pc.putc(' ') ;
00087         if (pos == 0)//first touch
00088         {
00089             if ((d>3) &&(d<9)) 
00090             {
00091                  redThread.signal_set(0x1);
00092                  pos=1 ;
00093             }
00094           
00095             if ((d>13) &&(d<19)) 
00096             {
00097                 pos =2;
00098                 greenThread.signal_set(0x1);
00099             }
00100             if ((d>23) &&(d<29)) 
00101             {
00102                 blueThread.signal_set(0x1) ;
00103                 pos =3;
00104             }
00105             if (d>33) 
00106             {
00107                  mixcolorThread.signal_set(0x1);
00108                  pos = 4;
00109             }
00110             
00111         }
00112         
00113         switch (pos)
00114         {
00115              
00116            
00117             case 1:
00118             {
00119                 if ((d<3) ||(d>9))
00120                 {
00121                //     redThread.signal_set(0x1) ;
00122                     pos = 0;
00123                 }
00124                 break;
00125             }
00126             case 2:
00127             {
00128                 if ((d<13) ||(d>19))
00129                 {
00130            //         greenThread.signal_set(0x1) ;
00131                     pos = 0;
00132                 }
00133                 break;
00134             }
00135             case 3:
00136             {
00137                 if ((d<23) ||(d>29))
00138                 {
00139          //           blueThread.signal_set(0x1) ;
00140                     pos = 0;
00141                 }
00142                 break;
00143             }
00144             case 4:
00145             {
00146                 if (d<33)
00147                 {
00148         //            mixcolorThread.signal_set(0x1) ;
00149                     pos = 0;
00150                 }
00151                 break;
00152             }
00153         }
00154                 
00155            
00156         Thread::wait(100);  // This polling rate is too slow - increase it
00157                             // The slower rate maks it easier to output on the terminal
00158     }
00159 }