Sarang Kharche
/
TSI_sample
TSI
Fork of TSI_sample by
main.cpp@5:dad93c5af6f6, 2018-03-01 (annotated)
- Committer:
- Sarang
- Date:
- Thu Mar 01 18:59:53 2018 +0000
- Revision:
- 5:dad93c5af6f6
- Parent:
- 4:d54e74fbf82c
TSI
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); |
Sarang | 5:dad93c5af6f6 | 13 | DigitalOut blueLED (LED_BLUE); |
Sarang | 5:dad93c5af6f6 | 14 | DigitalOut led1(D0); |
WilliamMarshQMUL | 0:4374caa1ef17 | 15 | TSISensor tsi; |
WilliamMarshQMUL | 0:4374caa1ef17 | 16 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 17 | Thread redThread ; // thread for red LED |
WilliamMarshQMUL | 0:4374caa1ef17 | 18 | Thread greenThread ; // thread for green LED |
Sarang | 5:dad93c5af6f6 | 19 | Thread blueThread ; |
Sarang | 5:dad93c5af6f6 | 20 | Thread orangeThread ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 21 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 22 | void red_thread() { // method to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 23 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 24 | Thread::signal_wait(0x1); |
Sarang | 5:dad93c5af6f6 | 25 | redLED = !redLED ; // turn on |
WilliamMarshQMUL | 0:4374caa1ef17 | 26 | redThread.signal_clr(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 27 | // Signal are automatically cleared by wait_signal but |
WilliamMarshQMUL | 0:4374caa1ef17 | 28 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 29 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 30 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 31 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 32 | void green_thread() { // method to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 33 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 34 | Thread::signal_wait(0x1); |
Sarang | 5:dad93c5af6f6 | 35 | greenLED = !greenLED ; // turn on |
WilliamMarshQMUL | 0:4374caa1ef17 | 36 | greenThread.signal_clr(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 37 | // Signal are automatically cleared by wait_signal but |
WilliamMarshQMUL | 0:4374caa1ef17 | 38 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 39 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 40 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 41 | |
Sarang | 5:dad93c5af6f6 | 42 | void blue_thread() { // method to run in thread |
Sarang | 5:dad93c5af6f6 | 43 | while (true) { |
Sarang | 5:dad93c5af6f6 | 44 | Thread::signal_wait(0x1); |
Sarang | 5:dad93c5af6f6 | 45 | blueLED = !blueLED ; // turn on |
Sarang | 5:dad93c5af6f6 | 46 | blueThread.signal_clr(0x1) ; |
Sarang | 5:dad93c5af6f6 | 47 | // Signal are automatically cleared by wait_signal but |
Sarang | 5:dad93c5af6f6 | 48 | // the signal might have been set again while LED on |
Sarang | 5:dad93c5af6f6 | 49 | } |
Sarang | 5:dad93c5af6f6 | 50 | } |
Sarang | 5:dad93c5af6f6 | 51 | |
Sarang | 5:dad93c5af6f6 | 52 | void orange_thread() { // method to run in thread |
Sarang | 5:dad93c5af6f6 | 53 | while (true) { |
Sarang | 5:dad93c5af6f6 | 54 | |
Sarang | 5:dad93c5af6f6 | 55 | led1 = true; |
Sarang | 5:dad93c5af6f6 | 56 | Thread::signal_wait(0x1); |
Sarang | 5:dad93c5af6f6 | 57 | led1 = !led1 ; // turn on |
Sarang | 5:dad93c5af6f6 | 58 | orangeThread.signal_clr(0x1) ; |
Sarang | 5:dad93c5af6f6 | 59 | // Signal are automatically cleared by wait_signal but |
Sarang | 5:dad93c5af6f6 | 60 | // the signal might have been set again while LED on |
Sarang | 5:dad93c5af6f6 | 61 | } |
Sarang | 5:dad93c5af6f6 | 62 | } |
Sarang | 5:dad93c5af6f6 | 63 | |
Sarang | 5:dad93c5af6f6 | 64 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 65 | int main(void) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 66 | redLED = true ; // turn off |
WilliamMarshQMUL | 0:4374caa1ef17 | 67 | greenLED = true ; // turn off |
Sarang | 5:dad93c5af6f6 | 68 | blueLED = true ; |
Sarang | 5:dad93c5af6f6 | 69 | led1 = true ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 70 | redThread.start(&red_thread) ; // start the red thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 71 | greenThread.start(&green_thread) ; // start the green thread |
Sarang | 5:dad93c5af6f6 | 72 | blueThread.start(&blue_thread) ; |
Sarang | 5:dad93c5af6f6 | 73 | orangeThread.start(&orange_thread); |
WilliamMarshQMUL | 0:4374caa1ef17 | 74 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 75 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 76 | uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 |
WilliamMarshQMUL | 0:4374caa1ef17 | 77 | // When no touch --> 0 |
WilliamMarshQMUL | 0:4374caa1ef17 | 78 | // Left --> low value Right --> high value |
WilliamMarshQMUL | 0:4374caa1ef17 | 79 | pc.printf("%d", d) ; |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 80 | pc.putc(' ') ; |
Sarang | 5:dad93c5af6f6 | 81 | if (d > 3 && d < 9) redThread.signal_set(0x1) ; |
Sarang | 5:dad93c5af6f6 | 82 | |
Sarang | 5:dad93c5af6f6 | 83 | if (d > 13 && d < 19) greenThread.signal_set(0x1) ; |
Sarang | 5:dad93c5af6f6 | 84 | |
Sarang | 5:dad93c5af6f6 | 85 | if (d > 23 && d < 29) blueThread.signal_set(0x1) ; |
Sarang | 5:dad93c5af6f6 | 86 | |
Sarang | 5:dad93c5af6f6 | 87 | if (d > 30 && d < 39) orangeThread.signal_set(0x1) ; |
Sarang | 5:dad93c5af6f6 | 88 | |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 89 | Thread::wait(200); // This polling rate is too slow - increase it |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 90 | // The slower rate maks it easier to output on the terminal |
WilliamMarshQMUL | 0:4374caa1ef17 | 91 | } |
Sarang | 5:dad93c5af6f6 | 92 | } |