Edwin Kadavy
/
TSI_Edwin_Kadavy
uses touchpad
Fork of TSI_sample by
Revision 3:f8d74ecd8563, committed 2018-03-01
- Comitter:
- edwinkad
- Date:
- Thu Mar 01 17:35:50 2018 +0000
- Parent:
- 2:e7b1f6275b63
- Commit message:
- lab-5 part-1.; Edwin Kadavy
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed_app.json | Show annotated file Show diff for this revision Revisions of this file |
diff -r e7b1f6275b63 -r f8d74ecd8563 main.cpp --- a/main.cpp Wed Feb 14 21:42:51 2018 +0000 +++ b/main.cpp Thu Mar 01 17:35:50 2018 +0000 @@ -1,6 +1,7 @@ #include "mbed.h" -#include "rtos.h" #include "TSISensor.h" +//#pragma import __use_two_region_memory + // Example program for lab 5 // ------------------------- @@ -9,52 +10,155 @@ // The value is also output to the serial interface Serial pc(USBTX, USBRX); // tx, rx -DigitalOut redLED(LED_RED); -DigitalOut greenLED(LED_GREEN); +DigitalOut redLED(LED1); +DigitalOut greenLED(LED2); +DigitalOut blueLED(LED3); +DigitalOut red2LED(PTD0); TSISensor tsi; -Thread redThread ; // thread for red LED -Thread greenThread ; // thread for green LED - -void red_thread() { // method to run in thread +Thread thread1;//(osPriorityNormal, 500); +Thread thread2;//(osPriorityNormal, 500); +Thread thread3;//(osPriorityNormal, 500); +Thread thread4;//(osPriorityNormal, 500); +Thread thread5;//(osPriorityNormal, 2000); + + + +void led1() // method to run in thread +{ while (true) { Thread::signal_wait(0x1); - redLED = false ; // turn on - Thread::wait(5000); - redLED = true ; // turn off - redThread.signal_clr(0x1) ; - // Signal are automatically cleared by wait_signal but - // the signal might have been set again while LED on + redLED = !redLED ; // turn on + //Thread::signal_wait(0x2); + //redLED = true ; // turn off + thread1.signal_clr(0x1); + //thread1.signal_clr(0x2) ; + // Signal are automatically cleared by wait_signal but + // the signal might have been set again while LED on + } +} + +void led2() // method to run in thread +{ + while (true) { + Thread::signal_wait(0x1); + greenLED = !greenLED ; // turn on + //Thread::signal_wait(0x2); + //greenLED = true ; // turn off + thread2.signal_clr(0x1) ; + //thread2.signal_clr(0x2) ; + // Signal are automatically cleared by wait_signal but + // the signal might have been set again while LED on + } +} + +void led3() // method to run in thread +{ + while (true) { + Thread::signal_wait(0x1); + blueLED = !blueLED ; // turn on + //Thread::signal_wait(0x2); + //blueLED = true ; // turn off + thread3.signal_clr(0x1) ; + //thread3.signal_clr(0x2) ; + // 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 led4() // method to run in thread +{ while (true) { Thread::signal_wait(0x1); - greenLED = false ; // turn on - Thread::wait(5000); - greenLED = true ; // turn off - greenThread.signal_clr(0x1) ; - // Signal are automatically cleared by wait_signal but - // the signal might have been set again while LED on + red2LED = !red2LED ; // turn on + //Thread::signal_wait(0x2); + //red2LED = true ; // turn off + thread4.signal_clr(0x1) ; + //thread4.signal_clr(0x2) ; + // Signal are automatically cleared by wait_signal but + // the signal might have been set again while LED on + } +} +/* +void led5() // method to run in thread +{ + while (true) { + Thread::signal_wait(0x1); + greenLED = false ; // turn on + blueLED = false ; // turn on + Thread::signal_wait(0x2); + greenLED = true ; // turn off + blueLED = true ; // turn off + thread5.signal_clr(0x1) ; + thread5.signal_clr(0x2) ; + // 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 - redThread.start(&red_thread) ; // start the red thread - greenThread.start(&green_thread) ; // start the green thread - +int main(void) +{ + redLED = true ; // turn off + greenLED = true ; // turn off + blueLED= true; // turn off + red2LED=false; + thread1.start(&led1) ; // start the red thread + thread2.start(&led2) ; // start the green thread + thread3.start(&led3) ; // start the green thread + thread4.start(&led4) ; // start the green thread + + int touchstate=0; while (true) { uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 - // When no touch --> 0 - // Left --> low value Right --> high value - pc.printf("%d", d) ; + // When no touch --> 0 + // Left --> low value Right --> high value + pc.printf("%d\n\r", 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 - // The slower rate maks it easier to output on the terminal + + + switch (touchstate) { + case 0://none state + if (d>3 && d<9) { + touchstate=1; + thread1.signal_set(0x1); + } else if (d>13 && d<19) { + touchstate=2; + thread2.signal_set(0x1); + } else if (d>23 && d<29) { + touchstate=3; + thread3.signal_set(0x1); + } else if (d>33) { + touchstate=4; + thread4.signal_set(0x1); + } + break; + case 1://leftOut state + if (d<3 || d>9 && touchstate==1) { + touchstate=0; + //thread1.signal_set(0x2); + } + break; + case 2://leftIn + if (d<13 || d>19 && touchstate==2 ) { + touchstate=0; + //thread2.signal_set(0x2); + } + break; + case 3://rightIn + if (d<23 || d>29 && touchstate==3) { + touchstate=0; + //thread3.signal_set(0x2); + } + break; + case 4://rightOut + if (d<33 && touchstate==4 ) { + touchstate=0; + //thread4.signal_set(0x2); + } + break; + } + + Thread::wait(50); // } } \ No newline at end of file
diff -r e7b1f6275b63 -r f8d74ecd8563 mbed_app.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Thu Mar 01 17:35:50 2018 +0000 @@ -0,0 +1,7 @@ +{ + "config": { + "Thread-stack-size": { + "value": 1000 + } + } +} \ No newline at end of file