Soheil Novinfard
/
TSI_STEP2
TSI Step2
Fork of TSI_sample by
Diff: main.cpp
- Revision:
- 5:9ab4c6917f6b
- Parent:
- 4:d54e74fbf82c
--- a/main.cpp Thu Feb 22 16:59:24 2018 +0000 +++ b/main.cpp Fri Mar 02 12:20:33 2018 +0000 @@ -8,52 +8,227 @@ // The value is also output to the serial interface Serial pc(USBTX, USBRX); // tx, rx + DigitalOut redLED(LED_RED); DigitalOut greenLED(LED_GREEN); +DigitalOut blueLED(LED_BLUE); +DigitalOut whiteLED(PTC9); // External LED + TSISensor tsi; -Thread redThread ; // thread for red LED -Thread greenThread ; // thread for green LED - -void red_thread() { // method to run in thread +Thread leftOutThread (osPriorityNormal, 1000); +Thread leftInThread (osPriorityNormal, 1000); +Thread rightInThread (osPriorityNormal, 1000); +Thread rightOutThread (osPriorityNormal, 1000); + +enum states { LeftOut, LeftIn, RightIn, RightOut, None}; +volatile states state = None; + + + +//void green_thread() { // method to run in thread +// while (true) { +// Thread::signal_wait(0x1); +// greenLED = !greenLED ; // turn on +// +// greenThread.signal_clr(0x1) ; +// // Signal are automatically cleared by wait_signal but +// // the signal might have been set again while LED on +// } +//} + +//void TurnOnLed(int ledNumber) +//{ +// switch (ledNumber) { +// case 1: +// redLED = false; +// greenLED = true; +// blueLED = true; +// +// whiteLED = 0; +// break; +// +// case 2: +// redLED = true; +// greenLED = false; +// blueLED = true; +// +// whiteLED = 0; +// break; +// +// case 3: +// redLED = true; +// greenLED = true; +// blueLED = false; +// +// whiteLED = 0; +// break; +// +// case 4: +// redLED = true; +// greenLED = true; +// blueLED = true; +// +// whiteLED = 1; +// break; +// } +//} + +void TurnOffLed() +{ + redLED = true; + greenLED = true; + blueLED = true; + whiteLED = 0; + +} + + +void leftOut_thread() // 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 + Thread::signal_wait(0x1); // signal for enter + redLED = false; + + Thread::signal_wait(0x1); // signal for exit + redLED = true; + + leftOutThread.signal_clr(0x1); + leftOutThread.signal_clr(0x2); + // Signal are automatically cleared by wait_signal but + // the signal might have been set again + } +} + +void leftIn_thread() // method to run in thread +{ + while (true) { + Thread::signal_wait(0x1); // signal for enter + greenLED = false; + + Thread::signal_wait(0x1); // signal for exit + greenLED = true; + + leftInThread.signal_clr(0x1); + leftInThread.signal_clr(0x2); + // Signal are automatically cleared by wait_signal but + // the signal might have been set again } } -void green_thread() { // method to run in thread + +void rightIn_thread() // 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 + Thread::signal_wait(0x1); // signal for enter + blueLED = !blueLED; + + Thread::signal_wait(0x1); // signal for exit + blueLED = !blueLED; + + rightInThread.signal_clr(0x1); + rightInThread.signal_clr(0x2); + // Signal are automatically cleared by wait_signal but + // the signal might have been set again + } +} + + +void rightOut_thread() // method to run in thread +{ + while (true) { + Thread::signal_wait(0x1); // signal for enter + whiteLED = 1; + + Thread::signal_wait(0x1); // signal for exit + whiteLED = 0; + + rightOutThread.signal_clr(0x1); + rightOutThread.signal_clr(0x2); + // Signal are automatically cleared by wait_signal but + // the signal might have been set again } } -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) +{ + TurnOffLed(); // initial turning off LED + + leftOutThread.start(&leftOut_thread) ; // start the leftOut thread + leftInThread.start(&leftIn_thread) ; // start the leftIn thread + rightInThread.start(&rightIn_thread) ; // start the rightIn thread + rightOutThread.start(&rightOut_thread) ; // start the rightOut thread + 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) ; + uint8_t pos = tsi.readDistance() ; // Distance is between 0 and 39 + + pc.printf("%d", pos) ; 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 + +// // start states +// if (pos>3 && pos<9) { +// leftOutThread.signal_set(0x1); +// } else if(pos>13 && pos<19) { +// leftInThread.signal_set(0x1); +// } else if(pos>23 && pos<29) { +// rightInThread.signal_set(0x1); +// } else if(pos>33) { +// rightOutThread.signal_set(0x1); +// } + + switch (state) { + case LeftOut: + if(pos<3 || pos>9) { + state = None ; + } + break; + + case LeftIn: + if(pos<13 || pos>19) { + state = None ; + } + break; + + case RightIn: + if(pos<23 || pos>29) { + state = None ; + } + + break; + + case RightOut: + if(pos<33) { + state = None ; + } + break; + + case None: + if(pos>3 && pos<9) { + state = LeftOut; + leftOutThread.signal_set(0x1); + } + if(pos>13 && pos<19) { + state = LeftIn; + leftInThread.signal_set(0x1); + } + if(pos>23 && pos<29) { + state = RightIn; + rightInThread.signal_set(0x1); + } + if(pos>33) { + state = RightOut; + rightOutThread.signal_set(0x1); + } + + 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