Soheil Novinfard
/
TSI_STEP2
TSI Step2
Fork of TSI_sample by
main.cpp@5:9ab4c6917f6b, 2018-03-02 (annotated)
- Committer:
- novinfard
- Date:
- Fri Mar 02 12:20:33 2018 +0000
- Revision:
- 5:9ab4c6917f6b
- Parent:
- 4:d54e74fbf82c
Finalise Project
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 |
novinfard | 5:9ab4c6917f6b | 11 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 12 | DigitalOut redLED(LED_RED); |
WilliamMarshQMUL | 0:4374caa1ef17 | 13 | DigitalOut greenLED(LED_GREEN); |
novinfard | 5:9ab4c6917f6b | 14 | DigitalOut blueLED(LED_BLUE); |
novinfard | 5:9ab4c6917f6b | 15 | DigitalOut whiteLED(PTC9); // External LED |
novinfard | 5:9ab4c6917f6b | 16 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 17 | TSISensor tsi; |
WilliamMarshQMUL | 0:4374caa1ef17 | 18 | |
novinfard | 5:9ab4c6917f6b | 19 | Thread leftOutThread (osPriorityNormal, 1000); |
novinfard | 5:9ab4c6917f6b | 20 | Thread leftInThread (osPriorityNormal, 1000); |
novinfard | 5:9ab4c6917f6b | 21 | Thread rightInThread (osPriorityNormal, 1000); |
novinfard | 5:9ab4c6917f6b | 22 | Thread rightOutThread (osPriorityNormal, 1000); |
novinfard | 5:9ab4c6917f6b | 23 | |
novinfard | 5:9ab4c6917f6b | 24 | enum states { LeftOut, LeftIn, RightIn, RightOut, None}; |
novinfard | 5:9ab4c6917f6b | 25 | volatile states state = None; |
novinfard | 5:9ab4c6917f6b | 26 | |
novinfard | 5:9ab4c6917f6b | 27 | |
novinfard | 5:9ab4c6917f6b | 28 | |
novinfard | 5:9ab4c6917f6b | 29 | //void green_thread() { // method to run in thread |
novinfard | 5:9ab4c6917f6b | 30 | // while (true) { |
novinfard | 5:9ab4c6917f6b | 31 | // Thread::signal_wait(0x1); |
novinfard | 5:9ab4c6917f6b | 32 | // greenLED = !greenLED ; // turn on |
novinfard | 5:9ab4c6917f6b | 33 | // |
novinfard | 5:9ab4c6917f6b | 34 | // greenThread.signal_clr(0x1) ; |
novinfard | 5:9ab4c6917f6b | 35 | // // Signal are automatically cleared by wait_signal but |
novinfard | 5:9ab4c6917f6b | 36 | // // the signal might have been set again while LED on |
novinfard | 5:9ab4c6917f6b | 37 | // } |
novinfard | 5:9ab4c6917f6b | 38 | //} |
novinfard | 5:9ab4c6917f6b | 39 | |
novinfard | 5:9ab4c6917f6b | 40 | //void TurnOnLed(int ledNumber) |
novinfard | 5:9ab4c6917f6b | 41 | //{ |
novinfard | 5:9ab4c6917f6b | 42 | // switch (ledNumber) { |
novinfard | 5:9ab4c6917f6b | 43 | // case 1: |
novinfard | 5:9ab4c6917f6b | 44 | // redLED = false; |
novinfard | 5:9ab4c6917f6b | 45 | // greenLED = true; |
novinfard | 5:9ab4c6917f6b | 46 | // blueLED = true; |
novinfard | 5:9ab4c6917f6b | 47 | // |
novinfard | 5:9ab4c6917f6b | 48 | // whiteLED = 0; |
novinfard | 5:9ab4c6917f6b | 49 | // break; |
novinfard | 5:9ab4c6917f6b | 50 | // |
novinfard | 5:9ab4c6917f6b | 51 | // case 2: |
novinfard | 5:9ab4c6917f6b | 52 | // redLED = true; |
novinfard | 5:9ab4c6917f6b | 53 | // greenLED = false; |
novinfard | 5:9ab4c6917f6b | 54 | // blueLED = true; |
novinfard | 5:9ab4c6917f6b | 55 | // |
novinfard | 5:9ab4c6917f6b | 56 | // whiteLED = 0; |
novinfard | 5:9ab4c6917f6b | 57 | // break; |
novinfard | 5:9ab4c6917f6b | 58 | // |
novinfard | 5:9ab4c6917f6b | 59 | // case 3: |
novinfard | 5:9ab4c6917f6b | 60 | // redLED = true; |
novinfard | 5:9ab4c6917f6b | 61 | // greenLED = true; |
novinfard | 5:9ab4c6917f6b | 62 | // blueLED = false; |
novinfard | 5:9ab4c6917f6b | 63 | // |
novinfard | 5:9ab4c6917f6b | 64 | // whiteLED = 0; |
novinfard | 5:9ab4c6917f6b | 65 | // break; |
novinfard | 5:9ab4c6917f6b | 66 | // |
novinfard | 5:9ab4c6917f6b | 67 | // case 4: |
novinfard | 5:9ab4c6917f6b | 68 | // redLED = true; |
novinfard | 5:9ab4c6917f6b | 69 | // greenLED = true; |
novinfard | 5:9ab4c6917f6b | 70 | // blueLED = true; |
novinfard | 5:9ab4c6917f6b | 71 | // |
novinfard | 5:9ab4c6917f6b | 72 | // whiteLED = 1; |
novinfard | 5:9ab4c6917f6b | 73 | // break; |
novinfard | 5:9ab4c6917f6b | 74 | // } |
novinfard | 5:9ab4c6917f6b | 75 | //} |
novinfard | 5:9ab4c6917f6b | 76 | |
novinfard | 5:9ab4c6917f6b | 77 | void TurnOffLed() |
novinfard | 5:9ab4c6917f6b | 78 | { |
novinfard | 5:9ab4c6917f6b | 79 | redLED = true; |
novinfard | 5:9ab4c6917f6b | 80 | greenLED = true; |
novinfard | 5:9ab4c6917f6b | 81 | blueLED = true; |
novinfard | 5:9ab4c6917f6b | 82 | whiteLED = 0; |
novinfard | 5:9ab4c6917f6b | 83 | |
novinfard | 5:9ab4c6917f6b | 84 | } |
novinfard | 5:9ab4c6917f6b | 85 | |
novinfard | 5:9ab4c6917f6b | 86 | |
novinfard | 5:9ab4c6917f6b | 87 | void leftOut_thread() // method to run in thread |
novinfard | 5:9ab4c6917f6b | 88 | { |
WilliamMarshQMUL | 0:4374caa1ef17 | 89 | while (true) { |
novinfard | 5:9ab4c6917f6b | 90 | Thread::signal_wait(0x1); // signal for enter |
novinfard | 5:9ab4c6917f6b | 91 | redLED = false; |
novinfard | 5:9ab4c6917f6b | 92 | |
novinfard | 5:9ab4c6917f6b | 93 | Thread::signal_wait(0x1); // signal for exit |
novinfard | 5:9ab4c6917f6b | 94 | redLED = true; |
novinfard | 5:9ab4c6917f6b | 95 | |
novinfard | 5:9ab4c6917f6b | 96 | leftOutThread.signal_clr(0x1); |
novinfard | 5:9ab4c6917f6b | 97 | leftOutThread.signal_clr(0x2); |
novinfard | 5:9ab4c6917f6b | 98 | // Signal are automatically cleared by wait_signal but |
novinfard | 5:9ab4c6917f6b | 99 | // the signal might have been set again |
novinfard | 5:9ab4c6917f6b | 100 | } |
novinfard | 5:9ab4c6917f6b | 101 | } |
novinfard | 5:9ab4c6917f6b | 102 | |
novinfard | 5:9ab4c6917f6b | 103 | void leftIn_thread() // method to run in thread |
novinfard | 5:9ab4c6917f6b | 104 | { |
novinfard | 5:9ab4c6917f6b | 105 | while (true) { |
novinfard | 5:9ab4c6917f6b | 106 | Thread::signal_wait(0x1); // signal for enter |
novinfard | 5:9ab4c6917f6b | 107 | greenLED = false; |
novinfard | 5:9ab4c6917f6b | 108 | |
novinfard | 5:9ab4c6917f6b | 109 | Thread::signal_wait(0x1); // signal for exit |
novinfard | 5:9ab4c6917f6b | 110 | greenLED = true; |
novinfard | 5:9ab4c6917f6b | 111 | |
novinfard | 5:9ab4c6917f6b | 112 | leftInThread.signal_clr(0x1); |
novinfard | 5:9ab4c6917f6b | 113 | leftInThread.signal_clr(0x2); |
novinfard | 5:9ab4c6917f6b | 114 | // Signal are automatically cleared by wait_signal but |
novinfard | 5:9ab4c6917f6b | 115 | // the signal might have been set again |
WilliamMarshQMUL | 0:4374caa1ef17 | 116 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 117 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 118 | |
novinfard | 5:9ab4c6917f6b | 119 | |
novinfard | 5:9ab4c6917f6b | 120 | void rightIn_thread() // method to run in thread |
novinfard | 5:9ab4c6917f6b | 121 | { |
WilliamMarshQMUL | 0:4374caa1ef17 | 122 | while (true) { |
novinfard | 5:9ab4c6917f6b | 123 | Thread::signal_wait(0x1); // signal for enter |
novinfard | 5:9ab4c6917f6b | 124 | blueLED = !blueLED; |
novinfard | 5:9ab4c6917f6b | 125 | |
novinfard | 5:9ab4c6917f6b | 126 | Thread::signal_wait(0x1); // signal for exit |
novinfard | 5:9ab4c6917f6b | 127 | blueLED = !blueLED; |
novinfard | 5:9ab4c6917f6b | 128 | |
novinfard | 5:9ab4c6917f6b | 129 | rightInThread.signal_clr(0x1); |
novinfard | 5:9ab4c6917f6b | 130 | rightInThread.signal_clr(0x2); |
novinfard | 5:9ab4c6917f6b | 131 | // Signal are automatically cleared by wait_signal but |
novinfard | 5:9ab4c6917f6b | 132 | // the signal might have been set again |
novinfard | 5:9ab4c6917f6b | 133 | } |
novinfard | 5:9ab4c6917f6b | 134 | } |
novinfard | 5:9ab4c6917f6b | 135 | |
novinfard | 5:9ab4c6917f6b | 136 | |
novinfard | 5:9ab4c6917f6b | 137 | void rightOut_thread() // method to run in thread |
novinfard | 5:9ab4c6917f6b | 138 | { |
novinfard | 5:9ab4c6917f6b | 139 | while (true) { |
novinfard | 5:9ab4c6917f6b | 140 | Thread::signal_wait(0x1); // signal for enter |
novinfard | 5:9ab4c6917f6b | 141 | whiteLED = 1; |
novinfard | 5:9ab4c6917f6b | 142 | |
novinfard | 5:9ab4c6917f6b | 143 | Thread::signal_wait(0x1); // signal for exit |
novinfard | 5:9ab4c6917f6b | 144 | whiteLED = 0; |
novinfard | 5:9ab4c6917f6b | 145 | |
novinfard | 5:9ab4c6917f6b | 146 | rightOutThread.signal_clr(0x1); |
novinfard | 5:9ab4c6917f6b | 147 | rightOutThread.signal_clr(0x2); |
novinfard | 5:9ab4c6917f6b | 148 | // Signal are automatically cleared by wait_signal but |
novinfard | 5:9ab4c6917f6b | 149 | // the signal might have been set again |
WilliamMarshQMUL | 0:4374caa1ef17 | 150 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 151 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 152 | |
novinfard | 5:9ab4c6917f6b | 153 | |
novinfard | 5:9ab4c6917f6b | 154 | |
novinfard | 5:9ab4c6917f6b | 155 | |
novinfard | 5:9ab4c6917f6b | 156 | int main(void) |
novinfard | 5:9ab4c6917f6b | 157 | { |
novinfard | 5:9ab4c6917f6b | 158 | TurnOffLed(); // initial turning off LED |
novinfard | 5:9ab4c6917f6b | 159 | |
novinfard | 5:9ab4c6917f6b | 160 | leftOutThread.start(&leftOut_thread) ; // start the leftOut thread |
novinfard | 5:9ab4c6917f6b | 161 | leftInThread.start(&leftIn_thread) ; // start the leftIn thread |
novinfard | 5:9ab4c6917f6b | 162 | rightInThread.start(&rightIn_thread) ; // start the rightIn thread |
novinfard | 5:9ab4c6917f6b | 163 | rightOutThread.start(&rightOut_thread) ; // start the rightOut thread |
novinfard | 5:9ab4c6917f6b | 164 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 165 | while (true) { |
novinfard | 5:9ab4c6917f6b | 166 | uint8_t pos = tsi.readDistance() ; // Distance is between 0 and 39 |
novinfard | 5:9ab4c6917f6b | 167 | |
novinfard | 5:9ab4c6917f6b | 168 | pc.printf("%d", pos) ; |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 169 | pc.putc(' ') ; |
novinfard | 5:9ab4c6917f6b | 170 | |
novinfard | 5:9ab4c6917f6b | 171 | // // start states |
novinfard | 5:9ab4c6917f6b | 172 | // if (pos>3 && pos<9) { |
novinfard | 5:9ab4c6917f6b | 173 | // leftOutThread.signal_set(0x1); |
novinfard | 5:9ab4c6917f6b | 174 | // } else if(pos>13 && pos<19) { |
novinfard | 5:9ab4c6917f6b | 175 | // leftInThread.signal_set(0x1); |
novinfard | 5:9ab4c6917f6b | 176 | // } else if(pos>23 && pos<29) { |
novinfard | 5:9ab4c6917f6b | 177 | // rightInThread.signal_set(0x1); |
novinfard | 5:9ab4c6917f6b | 178 | // } else if(pos>33) { |
novinfard | 5:9ab4c6917f6b | 179 | // rightOutThread.signal_set(0x1); |
novinfard | 5:9ab4c6917f6b | 180 | // } |
novinfard | 5:9ab4c6917f6b | 181 | |
novinfard | 5:9ab4c6917f6b | 182 | switch (state) { |
novinfard | 5:9ab4c6917f6b | 183 | case LeftOut: |
novinfard | 5:9ab4c6917f6b | 184 | if(pos<3 || pos>9) { |
novinfard | 5:9ab4c6917f6b | 185 | state = None ; |
novinfard | 5:9ab4c6917f6b | 186 | } |
novinfard | 5:9ab4c6917f6b | 187 | break; |
novinfard | 5:9ab4c6917f6b | 188 | |
novinfard | 5:9ab4c6917f6b | 189 | case LeftIn: |
novinfard | 5:9ab4c6917f6b | 190 | if(pos<13 || pos>19) { |
novinfard | 5:9ab4c6917f6b | 191 | state = None ; |
novinfard | 5:9ab4c6917f6b | 192 | } |
novinfard | 5:9ab4c6917f6b | 193 | break; |
novinfard | 5:9ab4c6917f6b | 194 | |
novinfard | 5:9ab4c6917f6b | 195 | case RightIn: |
novinfard | 5:9ab4c6917f6b | 196 | if(pos<23 || pos>29) { |
novinfard | 5:9ab4c6917f6b | 197 | state = None ; |
novinfard | 5:9ab4c6917f6b | 198 | } |
novinfard | 5:9ab4c6917f6b | 199 | |
novinfard | 5:9ab4c6917f6b | 200 | break; |
novinfard | 5:9ab4c6917f6b | 201 | |
novinfard | 5:9ab4c6917f6b | 202 | case RightOut: |
novinfard | 5:9ab4c6917f6b | 203 | if(pos<33) { |
novinfard | 5:9ab4c6917f6b | 204 | state = None ; |
novinfard | 5:9ab4c6917f6b | 205 | } |
novinfard | 5:9ab4c6917f6b | 206 | break; |
novinfard | 5:9ab4c6917f6b | 207 | |
novinfard | 5:9ab4c6917f6b | 208 | case None: |
novinfard | 5:9ab4c6917f6b | 209 | if(pos>3 && pos<9) { |
novinfard | 5:9ab4c6917f6b | 210 | state = LeftOut; |
novinfard | 5:9ab4c6917f6b | 211 | leftOutThread.signal_set(0x1); |
novinfard | 5:9ab4c6917f6b | 212 | } |
novinfard | 5:9ab4c6917f6b | 213 | if(pos>13 && pos<19) { |
novinfard | 5:9ab4c6917f6b | 214 | state = LeftIn; |
novinfard | 5:9ab4c6917f6b | 215 | leftInThread.signal_set(0x1); |
novinfard | 5:9ab4c6917f6b | 216 | } |
novinfard | 5:9ab4c6917f6b | 217 | if(pos>23 && pos<29) { |
novinfard | 5:9ab4c6917f6b | 218 | state = RightIn; |
novinfard | 5:9ab4c6917f6b | 219 | rightInThread.signal_set(0x1); |
novinfard | 5:9ab4c6917f6b | 220 | } |
novinfard | 5:9ab4c6917f6b | 221 | if(pos>33) { |
novinfard | 5:9ab4c6917f6b | 222 | state = RightOut; |
novinfard | 5:9ab4c6917f6b | 223 | rightOutThread.signal_set(0x1); |
novinfard | 5:9ab4c6917f6b | 224 | } |
novinfard | 5:9ab4c6917f6b | 225 | |
novinfard | 5:9ab4c6917f6b | 226 | break; |
novinfard | 5:9ab4c6917f6b | 227 | |
novinfard | 5:9ab4c6917f6b | 228 | } |
novinfard | 5:9ab4c6917f6b | 229 | |
novinfard | 5:9ab4c6917f6b | 230 | |
novinfard | 5:9ab4c6917f6b | 231 | Thread::wait(100); // This polling rate is too slow - increase it |
novinfard | 5:9ab4c6917f6b | 232 | // The slower rate maks it easier to output on the terminal |
WilliamMarshQMUL | 0:4374caa1ef17 | 233 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 234 | } |