Sebastian Barrera
/
Lab5_part2
lab 5 part 2
Diff: main.cpp
- Revision:
- 7:a910936bf4f8
- Parent:
- 6:71ef35e456ab
- Child:
- 8:ca714e821245
diff -r 71ef35e456ab -r a910936bf4f8 main.cpp --- a/main.cpp Thu Feb 20 11:28:36 2020 +0000 +++ b/main.cpp Thu Mar 12 11:59:07 2020 +0000 @@ -8,56 +8,91 @@ // The value is also output to the serial interface Serial pc(USBTX, USBRX); // tx, rx -DigitalOut redLED(LED_RED); -DigitalOut greenLED(LED_GREEN); +PwmOut redLED(LED_RED); +PwmOut greenLED(LED_GREEN); TSISensor tsi; +Timer t; -Thread redThread ; // thread for red LED -Thread greenThread ; // thread for green LED +enum pos{neutral, lo, li, ri, ro}; + +Thread green ; // thread for red LED +Thread red ; // thread for green LED -# define REDFLAG 0x01 -# define GREENFLAG 0x02 -EventFlags signals; // event flags for signalling; 2 used - -void red_thread() { // method to run in thread +# define lout 0x01 +# define lin 0x02 +# define rin 0x04 +# define rout 0x08 +EventFlags signals; + +void changebrightnessred() { // method to run in thread + int flags; + float i=0; while (true) { - signals.wait_any(REDFLAG); - redLED = false ; // turn on - ThisThread::sleep_for(5000) ; // wait(5.0); - redLED = true ; // turn off - signals.clear(REDFLAG) ; - // Signal are automatically cleared by wait_any but - // the signal might have been set again while LED on + signals.wait_any(lin,false); + flags=signals.wait_any(lout|rout,true); + if (flags==lout){ + if (i!=0) i--; + } + if (flags==rout){ + if (i!=9) i++; + } + pc.printf("flag: %d, red: %d",flags, i); + redLED= i/10; } } -void green_thread() { // method to run in thread +void changebrightnessgreen() { // method to run in thread + int flags; + float i=0; while (true) { - signals.wait_any(GREENFLAG); - greenLED = false ; // turn on - ThisThread::sleep_for(5000) ; // wait(5.0); - greenLED = true ; // turn off - signals.clear(GREENFLAG) ; - // Signal are automatically cleared by wait_any but - // the signal might have been set again while LED on + signals.wait_any(rin,false); + flags=signals.wait_any(lout|rout,true); + if (flags==lout){ + if (i!=0) i--; + } + if (flags==rout){ + if (i!=9) i++; + } + pc.printf("green: %d", i); + greenLED= i/10; } } 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 - + red.start(changebrightnessgreen) ; // start the red thread + green.start(changebrightnessred) ; // start the green thread + int pos=neutral; while (true) { uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 // When no touch --> 0 // Left --> low value Right --> high value + switch (pos){ + case neutral: + if (d>3 && d<9){pos=lo; signals.set(lout); t.start();} + if (d>13 && d<19){pos=li; signals.set(lin);} + if (d>23 && d<29){pos=ri; signals.set(rin);} + if (d>33){pos=ro; signals.set(rout); t.start();} + break; + case lo: + if (t.read()>1) signals.set(lout); + if (d<3 || d>9) {pos=neutral; t.reset(); t.stop();} + break; + case li: + if (d<13 || d>19) {pos=neutral; signals.clear(lin);} + break; + case ri: + if (d<23 || d>29) {pos=neutral; signals.clear(rin);} + break; + case ro: + if (t.read()>1) signals.set(rout); + if (d<33) {pos=neutral; t.reset(); t.stop();} + break; + } pc.printf("%d", d) ; pc.putc(' ') ; - if (d == 10) signals.set(REDFLAG) ; - if (d == 20) signals.set(GREENFLAG) ; - ThisThread::sleep_for(500) ; // This polling rate is too slow - increase it + ThisThread::sleep_for(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