Peiling Yi
/
TSI_week5_01
Lab5-1
Fork of TSI_sample by
main.cpp@0:4374caa1ef17, 2017-02-15 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Wed Feb 15 11:45:56 2017 +0000
- Revision:
- 0:4374caa1ef17
- Child:
- 1:e6ffa08ad8bf
First 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 "rtos.h" |
WilliamMarshQMUL | 0:4374caa1ef17 | 3 | #include "TSISensor.h" |
WilliamMarshQMUL | 0:4374caa1ef17 | 4 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 5 | // Example program for lab 5 |
WilliamMarshQMUL | 0:4374caa1ef17 | 6 | // ------------------------- |
WilliamMarshQMUL | 0:4374caa1ef17 | 7 | // A value is read from the touch sensor and use |
WilliamMarshQMUL | 0:4374caa1ef17 | 8 | // to control two LEDs |
WilliamMarshQMUL | 0:4374caa1ef17 | 9 | // The value is also output to the serial interface |
WilliamMarshQMUL | 0:4374caa1ef17 | 10 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 11 | Serial pc(USBTX, USBRX); // tx, rx |
WilliamMarshQMUL | 0:4374caa1ef17 | 12 | DigitalOut redLED(LED_RED); |
WilliamMarshQMUL | 0:4374caa1ef17 | 13 | DigitalOut greenLED(LED_GREEN); |
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 |
WilliamMarshQMUL | 0:4374caa1ef17 | 18 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 19 | void red_thread() { // method to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 20 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 21 | Thread::signal_wait(0x1); |
WilliamMarshQMUL | 0:4374caa1ef17 | 22 | redLED = false ; // turn on |
WilliamMarshQMUL | 0:4374caa1ef17 | 23 | Thread::wait(5000); |
WilliamMarshQMUL | 0:4374caa1ef17 | 24 | redLED = true ; // turn off |
WilliamMarshQMUL | 0:4374caa1ef17 | 25 | redThread.signal_clr(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 26 | // Signal are automatically cleared by wait_signal but |
WilliamMarshQMUL | 0:4374caa1ef17 | 27 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 28 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 29 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 30 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 31 | void green_thread() { // method to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 32 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 33 | Thread::signal_wait(0x1); |
WilliamMarshQMUL | 0:4374caa1ef17 | 34 | greenLED = false ; // turn on |
WilliamMarshQMUL | 0:4374caa1ef17 | 35 | Thread::wait(5000); |
WilliamMarshQMUL | 0:4374caa1ef17 | 36 | greenLED = true ; // turn off |
WilliamMarshQMUL | 0:4374caa1ef17 | 37 | greenThread.signal_clr(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 38 | // Signal are automatically cleared by wait_signal but |
WilliamMarshQMUL | 0:4374caa1ef17 | 39 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 40 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 41 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 42 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 43 | int main(void) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 44 | redLED = true ; // turn off |
WilliamMarshQMUL | 0:4374caa1ef17 | 45 | greenLED = true ; // turn off |
WilliamMarshQMUL | 0:4374caa1ef17 | 46 | redThread.start(&red_thread) ; // start the red thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 47 | greenThread.start(&green_thread) ; // start the green thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 48 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 49 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 50 | uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 |
WilliamMarshQMUL | 0:4374caa1ef17 | 51 | // When no touch --> 0 |
WilliamMarshQMUL | 0:4374caa1ef17 | 52 | // Left --> low value Right --> high value |
WilliamMarshQMUL | 0:4374caa1ef17 | 53 | pc.printf("%d", d) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 54 | pc.putc(' ') ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 55 | Thread::wait(200); |
WilliamMarshQMUL | 0:4374caa1ef17 | 56 | if (d == 10) redThread.signal_set(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 57 | if (d == 20) greenThread.signal_set(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 58 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 59 | } |