Sebastian Barrera
/
Lab5_part2
lab 5 part 2
main.cpp@5:2a9a3d74a1d8, 2019-02-19 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Tue Feb 19 09:38:31 2019 +0000
- Revision:
- 5:2a9a3d74a1d8
- Parent:
- 4:d54e74fbf82c
- Child:
- 6:71ef35e456ab
Update to event-flags interface
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); |
WilliamMarshQMUL | 0:4374caa1ef17 | 13 | TSISensor tsi; |
WilliamMarshQMUL | 0:4374caa1ef17 | 14 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 15 | Thread redThread ; // thread for red LED |
WilliamMarshQMUL | 0:4374caa1ef17 | 16 | Thread greenThread ; // thread for green LED |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 17 | |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 18 | # define REDFLAG 0x01 |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 19 | # define GREENFLAG 0x02 |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 20 | EventFlags signals; // event flags for signalling; 2 used |
WilliamMarshQMUL | 0:4374caa1ef17 | 21 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 22 | void red_thread() { // method to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 23 | while (true) { |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 24 | signals.wait_any(REDFLAG); |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 25 | redLED = false ; // turn on |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 26 | wait(5.0); |
WilliamMarshQMUL | 0:4374caa1ef17 | 27 | redLED = true ; // turn off |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 28 | signals.clear(REDFLAG) ; |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 29 | // Signal are automatically cleared by wait_any but |
WilliamMarshQMUL | 0:4374caa1ef17 | 30 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 31 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 32 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 33 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 34 | void green_thread() { // method to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 35 | while (true) { |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 36 | signals.wait_any(GREENFLAG); |
WilliamMarshQMUL | 0:4374caa1ef17 | 37 | greenLED = false ; // turn on |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 38 | wait(5.0); |
WilliamMarshQMUL | 0:4374caa1ef17 | 39 | greenLED = true ; // turn off |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 40 | signals.clear(GREENFLAG) ; |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 41 | // Signal are automatically cleared by wait_any but |
WilliamMarshQMUL | 0:4374caa1ef17 | 42 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 43 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 44 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 45 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 46 | int main(void) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 47 | redLED = true ; // turn off |
WilliamMarshQMUL | 0:4374caa1ef17 | 48 | greenLED = true ; // turn off |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 49 | redThread.start(red_thread) ; // start the red thread |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 50 | greenThread.start(green_thread) ; // start the green thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 51 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 52 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 53 | uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 |
WilliamMarshQMUL | 0:4374caa1ef17 | 54 | // When no touch --> 0 |
WilliamMarshQMUL | 0:4374caa1ef17 | 55 | // Left --> low value Right --> high value |
WilliamMarshQMUL | 0:4374caa1ef17 | 56 | pc.printf("%d", d) ; |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 57 | pc.putc(' ') ; |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 58 | if (d == 10) signals.set(REDFLAG) ; |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 59 | if (d == 20) signals.set(GREENFLAG) ; |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 60 | wait(0.5); // This polling rate is too slow - increase it |
WilliamMarshQMUL | 5:2a9a3d74a1d8 | 61 | // The slower rate maks it easier to output on the terminal |
WilliamMarshQMUL | 0:4374caa1ef17 | 62 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 63 | } |