Akhil Nair
/
lab5p1
lab5p1 final version
main.cpp@6:007c89b67d67, 2019-02-28 (annotated)
- Committer:
- anair12345
- Date:
- Thu Feb 28 17:04:08 2019 +0000
- Revision:
- 6:007c89b67d67
- Parent:
- 5:2a9a3d74a1d8
lab5p1 final version
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 |
WilliamMarshQMUL | 0:4374caa1ef17 | 11 | DigitalOut redLED(LED_RED); |
WilliamMarshQMUL | 0:4374caa1ef17 | 12 | DigitalOut greenLED(LED_GREEN); |
anair12345 | 6:007c89b67d67 | 13 | DigitalOut blueLED(LED_BLUE); |
WilliamMarshQMUL | 0:4374caa1ef17 | 14 | TSISensor tsi; |
WilliamMarshQMUL | 0:4374caa1ef17 | 15 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 16 | Thread redThread ; // thread for red LED |
WilliamMarshQMUL | 0:4374caa1ef17 | 17 | Thread greenThread ; // thread for green LED |
anair12345 | 6:007c89b67d67 | 18 | Thread blueThread; |
anair12345 | 6:007c89b67d67 | 19 | Thread whiteThread; |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 20 | |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 21 | # define REDFLAG 0x01 |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 22 | # define GREENFLAG 0x02 |
anair12345 | 6:007c89b67d67 | 23 | # define BLUEFLAG 0x04 |
anair12345 | 6:007c89b67d67 | 24 | # define WHITEFLAG 0x08 |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 25 | EventFlags signals; // event flags for signalling; 2 used |
anair12345 | 6:007c89b67d67 | 26 | |
anair12345 | 6:007c89b67d67 | 27 | enum redLEDState {REDOn,REDOff}; |
WilliamMarshQMUL | 0:4374caa1ef17 | 28 | void red_thread() { // method to run in thread |
anair12345 | 6:007c89b67d67 | 29 | redLEDState redstate = REDOff; |
anair12345 | 6:007c89b67d67 | 30 | while (true) { |
anair12345 | 6:007c89b67d67 | 31 | switch(redstate){ |
anair12345 | 6:007c89b67d67 | 32 | case REDOff: |
anair12345 | 6:007c89b67d67 | 33 | signals.wait_any(REDFLAG); |
anair12345 | 6:007c89b67d67 | 34 | redLED = false; |
anair12345 | 6:007c89b67d67 | 35 | wait(0.5); |
anair12345 | 6:007c89b67d67 | 36 | redstate = REDOn; |
anair12345 | 6:007c89b67d67 | 37 | break; |
anair12345 | 6:007c89b67d67 | 38 | case REDOn: |
anair12345 | 6:007c89b67d67 | 39 | redLED = true ; // turn on |
anair12345 | 6:007c89b67d67 | 40 | signals.clear(REDFLAG); |
anair12345 | 6:007c89b67d67 | 41 | redstate = REDOff; |
anair12345 | 6:007c89b67d67 | 42 | } |
anair12345 | 6:007c89b67d67 | 43 | } |
anair12345 | 6:007c89b67d67 | 44 | } |
anair12345 | 6:007c89b67d67 | 45 | enum greenLEDState {GREENOn,GREENOff}; |
anair12345 | 6:007c89b67d67 | 46 | void green_thread() { // method to run in thread |
anair12345 | 6:007c89b67d67 | 47 | greenLEDState greenstate = GREENOff; |
WilliamMarshQMUL | 0:4374caa1ef17 | 48 | while (true) { |
anair12345 | 6:007c89b67d67 | 49 | switch(greenstate){ |
anair12345 | 6:007c89b67d67 | 50 | case GREENOff: |
anair12345 | 6:007c89b67d67 | 51 | signals.wait_any(GREENFLAG); |
anair12345 | 6:007c89b67d67 | 52 | greenLED = false ; |
anair12345 | 6:007c89b67d67 | 53 | wait(0.5); |
anair12345 | 6:007c89b67d67 | 54 | greenstate = GREENOn; |
anair12345 | 6:007c89b67d67 | 55 | break; |
anair12345 | 6:007c89b67d67 | 56 | case GREENOn: |
anair12345 | 6:007c89b67d67 | 57 | greenLED = true ; // turn on |
anair12345 | 6:007c89b67d67 | 58 | signals.clear(GREENFLAG); |
anair12345 | 6:007c89b67d67 | 59 | greenstate = GREENOff; |
anair12345 | 6:007c89b67d67 | 60 | } |
anair12345 | 6:007c89b67d67 | 61 | } |
anair12345 | 6:007c89b67d67 | 62 | } |
anair12345 | 6:007c89b67d67 | 63 | enum blueLEDState {BLUEOn,BLUEOff}; |
anair12345 | 6:007c89b67d67 | 64 | void blue_thread() { // method to run in thread |
anair12345 | 6:007c89b67d67 | 65 | blueLEDState bluestate = BLUEOff; |
anair12345 | 6:007c89b67d67 | 66 | while (true) { |
anair12345 | 6:007c89b67d67 | 67 | switch(bluestate){ |
anair12345 | 6:007c89b67d67 | 68 | case BLUEOff: |
anair12345 | 6:007c89b67d67 | 69 | signals.wait_any(BLUEFLAG); |
anair12345 | 6:007c89b67d67 | 70 | blueLED = false ; |
anair12345 | 6:007c89b67d67 | 71 | wait(0.5); |
anair12345 | 6:007c89b67d67 | 72 | bluestate = BLUEOn; |
anair12345 | 6:007c89b67d67 | 73 | break; |
anair12345 | 6:007c89b67d67 | 74 | case BLUEOn: |
anair12345 | 6:007c89b67d67 | 75 | blueLED = true ; // turn on |
anair12345 | 6:007c89b67d67 | 76 | signals.clear(BLUEFLAG); |
anair12345 | 6:007c89b67d67 | 77 | bluestate = BLUEOff; |
anair12345 | 6:007c89b67d67 | 78 | } |
anair12345 | 6:007c89b67d67 | 79 | } |
anair12345 | 6:007c89b67d67 | 80 | } |
anair12345 | 6:007c89b67d67 | 81 | enum whiteLEDState {WHITEOn,WHITEOff}; |
anair12345 | 6:007c89b67d67 | 82 | void white_thread() { // method to run in thread |
anair12345 | 6:007c89b67d67 | 83 | whiteLEDState whitestate = WHITEOff; |
anair12345 | 6:007c89b67d67 | 84 | while (true) { |
anair12345 | 6:007c89b67d67 | 85 | switch(whitestate){ |
anair12345 | 6:007c89b67d67 | 86 | case WHITEOff: |
anair12345 | 6:007c89b67d67 | 87 | signals.wait_any(WHITEFLAG); |
anair12345 | 6:007c89b67d67 | 88 | blueLED = false ; // turn on |
anair12345 | 6:007c89b67d67 | 89 | redLED = false ; |
anair12345 | 6:007c89b67d67 | 90 | greenLED = false ; |
anair12345 | 6:007c89b67d67 | 91 | wait(0.5); |
anair12345 | 6:007c89b67d67 | 92 | whitestate = WHITEOn; |
anair12345 | 6:007c89b67d67 | 93 | break; |
anair12345 | 6:007c89b67d67 | 94 | case WHITEOn: |
anair12345 | 6:007c89b67d67 | 95 | blueLED = true ; // turn off |
anair12345 | 6:007c89b67d67 | 96 | redLED = true ; |
anair12345 | 6:007c89b67d67 | 97 | greenLED = true ; |
anair12345 | 6:007c89b67d67 | 98 | signals.clear(WHITEFLAG); |
anair12345 | 6:007c89b67d67 | 99 | whitestate = WHITEOff; |
anair12345 | 6:007c89b67d67 | 100 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 101 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 102 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 103 | |
anair12345 | 6:007c89b67d67 | 104 | enum states {LeftIn,LeftOut,RightIn,RightOut,None}; |
WilliamMarshQMUL | 0:4374caa1ef17 | 105 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 106 | int main(void) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 107 | redLED = true ; // turn off |
WilliamMarshQMUL | 0:4374caa1ef17 | 108 | greenLED = true ; // turn off |
anair12345 | 6:007c89b67d67 | 109 | blueLED = true ; |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 110 | redThread.start(red_thread) ; // start the red thread |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 111 | greenThread.start(green_thread) ; // start the green thread |
anair12345 | 6:007c89b67d67 | 112 | blueThread.start(blue_thread); |
anair12345 | 6:007c89b67d67 | 113 | whiteThread.start(white_thread); |
anair12345 | 6:007c89b67d67 | 114 | |
anair12345 | 6:007c89b67d67 | 115 | states State = None; |
WilliamMarshQMUL | 0:4374caa1ef17 | 116 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 117 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 118 | uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 |
WilliamMarshQMUL | 0:4374caa1ef17 | 119 | // When no touch --> 0 |
WilliamMarshQMUL | 0:4374caa1ef17 | 120 | // Left --> low value Right --> high value |
WilliamMarshQMUL | 0:4374caa1ef17 | 121 | pc.printf("%d", d) ; |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 122 | pc.putc(' ') ; |
anair12345 | 6:007c89b67d67 | 123 | wait(0.1); |
anair12345 | 6:007c89b67d67 | 124 | switch(State){ |
anair12345 | 6:007c89b67d67 | 125 | case None: |
anair12345 | 6:007c89b67d67 | 126 | if (d > 3 && d < 9) {signals.set(REDFLAG);State = LeftOut;} |
anair12345 | 6:007c89b67d67 | 127 | else if (d > 13 && d < 19) {signals.set(GREENFLAG) ;State = LeftIn; } |
anair12345 | 6:007c89b67d67 | 128 | else if (d > 23 && d < 29) {signals.set(BLUEFLAG) ;State = RightIn;} |
anair12345 | 6:007c89b67d67 | 129 | else if (d > 33) {signals.set(WHITEFLAG) ;State = RightOut;} |
anair12345 | 6:007c89b67d67 | 130 | break; |
anair12345 | 6:007c89b67d67 | 131 | case LeftOut: |
anair12345 | 6:007c89b67d67 | 132 | if (d < 3 || d >9) State = None;break; |
anair12345 | 6:007c89b67d67 | 133 | case LeftIn: |
anair12345 | 6:007c89b67d67 | 134 | if (d < 13 || d >19) State = None;break; |
anair12345 | 6:007c89b67d67 | 135 | case RightIn: |
anair12345 | 6:007c89b67d67 | 136 | if (d < 23 || d >29) State = None;break; |
anair12345 | 6:007c89b67d67 | 137 | case RightOut: |
anair12345 | 6:007c89b67d67 | 138 | if (d < 33) State = None;break; |
anair12345 | 6:007c89b67d67 | 139 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 140 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 141 | } |