Sebastian Barrera
/
Lab5_part2
lab 5 part 2
main.cpp@7:a910936bf4f8, 2020-03-12 (annotated)
- Committer:
- sebbarpar
- Date:
- Thu Mar 12 11:59:07 2020 +0000
- Revision:
- 7:a910936bf4f8
- Parent:
- 6:71ef35e456ab
- Child:
- 8:ca714e821245
Lab 5 part 2;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:4374caa1ef17 | 1 | #include "mbed.h" |
WilliamMarshQMUL | 0:4374caa1ef17 | 2 | #include "TSISensor.h" |
WilliamMarshQMUL | 0:4374caa1ef17 | 3 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 4 | // Example program for lab 5 |
WilliamMarshQMUL | 0:4374caa1ef17 | 5 | // ------------------------- |
WilliamMarshQMUL | 0:4374caa1ef17 | 6 | // A value is read from the touch sensor and use |
WilliamMarshQMUL | 0:4374caa1ef17 | 7 | // to control two LEDs |
WilliamMarshQMUL | 0:4374caa1ef17 | 8 | // The value is also output to the serial interface |
WilliamMarshQMUL | 0:4374caa1ef17 | 9 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 10 | Serial pc(USBTX, USBRX); // tx, rx |
sebbarpar | 7:a910936bf4f8 | 11 | PwmOut redLED(LED_RED); |
sebbarpar | 7:a910936bf4f8 | 12 | PwmOut greenLED(LED_GREEN); |
WilliamMarshQMUL | 0:4374caa1ef17 | 13 | TSISensor tsi; |
sebbarpar | 7:a910936bf4f8 | 14 | Timer t; |
WilliamMarshQMUL | 0:4374caa1ef17 | 15 | |
sebbarpar | 7:a910936bf4f8 | 16 | enum pos{neutral, lo, li, ri, ro}; |
sebbarpar | 7:a910936bf4f8 | 17 | |
sebbarpar | 7:a910936bf4f8 | 18 | Thread green ; // thread for red LED |
sebbarpar | 7:a910936bf4f8 | 19 | Thread red ; // thread for green LED |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 20 | |
sebbarpar | 7:a910936bf4f8 | 21 | # define lout 0x01 |
sebbarpar | 7:a910936bf4f8 | 22 | # define lin 0x02 |
sebbarpar | 7:a910936bf4f8 | 23 | # define rin 0x04 |
sebbarpar | 7:a910936bf4f8 | 24 | # define rout 0x08 |
sebbarpar | 7:a910936bf4f8 | 25 | EventFlags signals; |
sebbarpar | 7:a910936bf4f8 | 26 | |
sebbarpar | 7:a910936bf4f8 | 27 | void changebrightnessred() { // method to run in thread |
sebbarpar | 7:a910936bf4f8 | 28 | int flags; |
sebbarpar | 7:a910936bf4f8 | 29 | float i=0; |
WilliamMarshQMUL | 0:4374caa1ef17 | 30 | while (true) { |
sebbarpar | 7:a910936bf4f8 | 31 | signals.wait_any(lin,false); |
sebbarpar | 7:a910936bf4f8 | 32 | flags=signals.wait_any(lout|rout,true); |
sebbarpar | 7:a910936bf4f8 | 33 | if (flags==lout){ |
sebbarpar | 7:a910936bf4f8 | 34 | if (i!=0) i--; |
sebbarpar | 7:a910936bf4f8 | 35 | } |
sebbarpar | 7:a910936bf4f8 | 36 | if (flags==rout){ |
sebbarpar | 7:a910936bf4f8 | 37 | if (i!=9) i++; |
sebbarpar | 7:a910936bf4f8 | 38 | } |
sebbarpar | 7:a910936bf4f8 | 39 | pc.printf("flag: %d, red: %d",flags, i); |
sebbarpar | 7:a910936bf4f8 | 40 | redLED= i/10; |
WilliamMarshQMUL | 0:4374caa1ef17 | 41 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 42 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 43 | |
sebbarpar | 7:a910936bf4f8 | 44 | void changebrightnessgreen() { // method to run in thread |
sebbarpar | 7:a910936bf4f8 | 45 | int flags; |
sebbarpar | 7:a910936bf4f8 | 46 | float i=0; |
WilliamMarshQMUL | 0:4374caa1ef17 | 47 | while (true) { |
sebbarpar | 7:a910936bf4f8 | 48 | signals.wait_any(rin,false); |
sebbarpar | 7:a910936bf4f8 | 49 | flags=signals.wait_any(lout|rout,true); |
sebbarpar | 7:a910936bf4f8 | 50 | if (flags==lout){ |
sebbarpar | 7:a910936bf4f8 | 51 | if (i!=0) i--; |
sebbarpar | 7:a910936bf4f8 | 52 | } |
sebbarpar | 7:a910936bf4f8 | 53 | if (flags==rout){ |
sebbarpar | 7:a910936bf4f8 | 54 | if (i!=9) i++; |
sebbarpar | 7:a910936bf4f8 | 55 | } |
sebbarpar | 7:a910936bf4f8 | 56 | pc.printf("green: %d", i); |
sebbarpar | 7:a910936bf4f8 | 57 | greenLED= i/10; |
WilliamMarshQMUL | 0:4374caa1ef17 | 58 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 59 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 60 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 61 | int main(void) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 62 | redLED = true ; // turn off |
WilliamMarshQMUL | 0:4374caa1ef17 | 63 | greenLED = true ; // turn off |
sebbarpar | 7:a910936bf4f8 | 64 | red.start(changebrightnessgreen) ; // start the red thread |
sebbarpar | 7:a910936bf4f8 | 65 | green.start(changebrightnessred) ; // start the green thread |
sebbarpar | 7:a910936bf4f8 | 66 | int pos=neutral; |
WilliamMarshQMUL | 0:4374caa1ef17 | 67 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 68 | uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 |
WilliamMarshQMUL | 0:4374caa1ef17 | 69 | // When no touch --> 0 |
WilliamMarshQMUL | 0:4374caa1ef17 | 70 | // Left --> low value Right --> high value |
sebbarpar | 7:a910936bf4f8 | 71 | switch (pos){ |
sebbarpar | 7:a910936bf4f8 | 72 | case neutral: |
sebbarpar | 7:a910936bf4f8 | 73 | if (d>3 && d<9){pos=lo; signals.set(lout); t.start();} |
sebbarpar | 7:a910936bf4f8 | 74 | if (d>13 && d<19){pos=li; signals.set(lin);} |
sebbarpar | 7:a910936bf4f8 | 75 | if (d>23 && d<29){pos=ri; signals.set(rin);} |
sebbarpar | 7:a910936bf4f8 | 76 | if (d>33){pos=ro; signals.set(rout); t.start();} |
sebbarpar | 7:a910936bf4f8 | 77 | break; |
sebbarpar | 7:a910936bf4f8 | 78 | case lo: |
sebbarpar | 7:a910936bf4f8 | 79 | if (t.read()>1) signals.set(lout); |
sebbarpar | 7:a910936bf4f8 | 80 | if (d<3 || d>9) {pos=neutral; t.reset(); t.stop();} |
sebbarpar | 7:a910936bf4f8 | 81 | break; |
sebbarpar | 7:a910936bf4f8 | 82 | case li: |
sebbarpar | 7:a910936bf4f8 | 83 | if (d<13 || d>19) {pos=neutral; signals.clear(lin);} |
sebbarpar | 7:a910936bf4f8 | 84 | break; |
sebbarpar | 7:a910936bf4f8 | 85 | case ri: |
sebbarpar | 7:a910936bf4f8 | 86 | if (d<23 || d>29) {pos=neutral; signals.clear(rin);} |
sebbarpar | 7:a910936bf4f8 | 87 | break; |
sebbarpar | 7:a910936bf4f8 | 88 | case ro: |
sebbarpar | 7:a910936bf4f8 | 89 | if (t.read()>1) signals.set(rout); |
sebbarpar | 7:a910936bf4f8 | 90 | if (d<33) {pos=neutral; t.reset(); t.stop();} |
sebbarpar | 7:a910936bf4f8 | 91 | break; |
sebbarpar | 7:a910936bf4f8 | 92 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 93 | pc.printf("%d", d) ; |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 94 | pc.putc(' ') ; |
sebbarpar | 7:a910936bf4f8 | 95 | ThisThread::sleep_for(100) ; // This polling rate is too slow - increase it |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 96 | // The slower rate maks it easier to output on the terminal |
WilliamMarshQMUL | 0:4374caa1ef17 | 97 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 98 | } |