noop din / Mbed OS TSI_sample

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 led1(D9);
00015 
00016 
00017 TSISensor tsi;
00018 
00019 Thread redThread  ; // thread for red LED
00020 Thread greenThread  ; // thread for green LED
00021 Thread blueThread; // thread for blue LED
00022 Thread yellowThread ; // thread for yellow LED
00023 Thread position; // thread position 
00024  
00025 void red_thread() {  // method to run in thread
00026     while (true) {
00027         Thread::signal_wait(0x1);
00028         redLED = false ; // turn on 
00029         Thread::wait(5000);
00030         redLED = true ; // turn off 
00031         redThread.signal_clr(0x1) ;
00032           // Signal are automatically cleared by wait_signal but
00033           // the signal might have been set again while LED on 
00034     }
00035 }
00036 
00037 void green_thread() {  // method to run in thread
00038     while (true) {
00039         Thread::signal_wait(0x1);
00040         greenLED = false ; // turn on 
00041         Thread::wait(5000);
00042         greenLED = true ; // turn off 
00043         greenThread.signal_clr(0x1) ;
00044           // Signal are automatically cleared by wait_signal but
00045           // the signal might have been set again while LED on 
00046     }
00047 }
00048 
00049 // implement more thread 
00050 
00051 // this is more thread 
00052 
00053 void blue_thread() {  // method to run in thread
00054     while (true) {
00055         Thread::signal_wait(0x1);
00056         blueLED = false ; // turn on 
00057         Thread::wait(5000);
00058         blueLED = true ; // turn off 
00059         blueThread.signal_clr(0x1) ;
00060           // Signal are automatically cleared by wait_signal but
00061           // the signal might have been set again while LED on 
00062     }
00063 }
00064 
00065 // this is for addititonal yellow 
00066 
00067 
00068 void yellow_thread() {  // method to run in thread
00069     while (true) {
00070         Thread::signal_wait(0x1);
00071         led1 = false ; // turn on 
00072         Thread::wait(5000);
00073         led1 = true; // turn off 
00074         yellowThread.signal_clr(0x1) ;
00075           // Signal are automatically cleared by wait_signal but
00076           // the signal might have been set again while LED on 
00077     }
00078 }
00079 
00080 
00081 int main(void) {
00082     redLED = true ; // turn off 
00083     greenLED = true ; // turn off 
00084     blueLED = true; // turn off
00085     led1 = true;  
00086     //yellowLED = true; // turn off 
00087     
00088     redThread.start(&red_thread) ; // start the red thread
00089     greenThread.start(&green_thread) ; // start the green thread
00090     blueThread.start(&blue_thread) ; // start the blue thread
00091     yellowThread.start(&yellow_thread) ; // start the green thread
00092     
00093     while (true) {
00094         uint8_t d = tsi.readDistance() ;  // Distance is between 0 and 39
00095                                           // When no touch --> 0
00096                                           // Left --> low value  Right --> high value
00097         pc.printf("%d \n \r", d) ;  
00098         pc.putc(' ') ;
00099         if (d > 3 && d < 9) {
00100             redThread.signal_set(0x1) ;
00101         }
00102         else if (d > 13 && d < 19) {
00103             greenThread.signal_set(0x1) ;
00104         }
00105         else if (d > 23 && d < 29) {
00106             blueThread.signal_set(0x1) ;
00107         }
00108         else if (d > 33 ) {
00109             yellowThread.signal_set(0x1) ;
00110         }
00111         else{
00112             
00113             
00114             
00115         }
00116         Thread::wait(50);  // This polling rate is too slow - increase it
00117                             // The slower rate maks it easier to output on the terminal
00118     }
00119 }