Peiling Yi
/
TSI_week5_01
Lab5-1
Fork of TSI_sample by
main.cpp@5:c9802e8dcbcf, 2018-03-02 (annotated)
- Committer:
- Peilingyi
- Date:
- Fri Mar 02 20:53:57 2018 +0000
- Revision:
- 5:c9802e8dcbcf
- Parent:
- 4:d54e74fbf82c
Lab5-1
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); |
Peilingyi | 5:c9802e8dcbcf | 13 | DigitalOut blueLED(LED_BLUE); |
Peilingyi | 5:c9802e8dcbcf | 14 | DigitalOut LEDout(D0); |
Peilingyi | 5:c9802e8dcbcf | 15 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 16 | TSISensor tsi; |
WilliamMarshQMUL | 0:4374caa1ef17 | 17 | |
Peilingyi | 5:c9802e8dcbcf | 18 | Thread redThread(osPriorityNormal,1000) ; // thread for red LED |
Peilingyi | 5:c9802e8dcbcf | 19 | Thread greenThread(osPriorityNormal,1000) ; // thread for green LED |
Peilingyi | 5:c9802e8dcbcf | 20 | Thread blueThread(osPriorityNormal,1000) ; // thread for blue LED |
Peilingyi | 5:c9802e8dcbcf | 21 | Thread mixcolorThread(osPriorityNormal,1000) ; // thread for mix LED |
WilliamMarshQMUL | 0:4374caa1ef17 | 22 | |
Peilingyi | 5:c9802e8dcbcf | 23 | void red_thread() { // methbd to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 24 | while (true) { |
Peilingyi | 5:c9802e8dcbcf | 25 | Thread::signal_wait(0x1); |
Peilingyi | 5:c9802e8dcbcf | 26 | |
Peilingyi | 5:c9802e8dcbcf | 27 | redLED = !redLED; // turn on |
Peilingyi | 5:c9802e8dcbcf | 28 | // blueLED = 1; |
Peilingyi | 5:c9802e8dcbcf | 29 | // greenLED = 1; |
WilliamMarshQMUL | 0:4374caa1ef17 | 30 | redThread.signal_clr(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 31 | // Signal are automatically cleared by wait_signal but |
WilliamMarshQMUL | 0:4374caa1ef17 | 32 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 33 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 34 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 35 | |
Peilingyi | 5:c9802e8dcbcf | 36 | void blue_thread() { // methbd to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 37 | while (true) { |
Peilingyi | 5:c9802e8dcbcf | 38 | Thread::signal_wait(0x1); |
Peilingyi | 5:c9802e8dcbcf | 39 | //redLED = 1; // turn on |
Peilingyi | 5:c9802e8dcbcf | 40 | blueLED = !blueLED; |
Peilingyi | 5:c9802e8dcbcf | 41 | //greenLED = 1; |
Peilingyi | 5:c9802e8dcbcf | 42 | blueThread.signal_clr(0x1) ; |
Peilingyi | 5:c9802e8dcbcf | 43 | // Signal are automatically cleared by wait_signal but |
Peilingyi | 5:c9802e8dcbcf | 44 | // the signal might have been set again while LED on |
Peilingyi | 5:c9802e8dcbcf | 45 | } |
Peilingyi | 5:c9802e8dcbcf | 46 | } |
Peilingyi | 5:c9802e8dcbcf | 47 | |
Peilingyi | 5:c9802e8dcbcf | 48 | void green_thread() { // methbd to run in thread |
Peilingyi | 5:c9802e8dcbcf | 49 | while (true) { |
Peilingyi | 5:c9802e8dcbcf | 50 | Thread::signal_wait(0x1); |
Peilingyi | 5:c9802e8dcbcf | 51 | //redLED = 1; // turn on |
Peilingyi | 5:c9802e8dcbcf | 52 | //blueLED = 1; |
Peilingyi | 5:c9802e8dcbcf | 53 | greenLED = !greenLED; |
WilliamMarshQMUL | 0:4374caa1ef17 | 54 | greenThread.signal_clr(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 55 | // Signal are automatically cleared by wait_signal but |
WilliamMarshQMUL | 0:4374caa1ef17 | 56 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 57 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 58 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 59 | |
Peilingyi | 5:c9802e8dcbcf | 60 | void mixcolor_thread() { // methbd to run in thread |
Peilingyi | 5:c9802e8dcbcf | 61 | while (true) { |
Peilingyi | 5:c9802e8dcbcf | 62 | Thread::signal_wait(0x1); |
Peilingyi | 5:c9802e8dcbcf | 63 | LEDout = !LEDout; |
Peilingyi | 5:c9802e8dcbcf | 64 | mixcolorThread.signal_clr(0x1) ; |
Peilingyi | 5:c9802e8dcbcf | 65 | // Signal are automatically cleared by wait_signal but |
Peilingyi | 5:c9802e8dcbcf | 66 | // the signal might have been set again while LED on |
Peilingyi | 5:c9802e8dcbcf | 67 | } |
Peilingyi | 5:c9802e8dcbcf | 68 | } |
Peilingyi | 5:c9802e8dcbcf | 69 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 70 | int main(void) { |
Peilingyi | 5:c9802e8dcbcf | 71 | uint8_t pos = 0; |
Peilingyi | 5:c9802e8dcbcf | 72 | redLED = 1; // turn off |
Peilingyi | 5:c9802e8dcbcf | 73 | greenLED = 1; // turn off |
Peilingyi | 5:c9802e8dcbcf | 74 | blueLED = 1; |
Peilingyi | 5:c9802e8dcbcf | 75 | LEDout = 0; |
WilliamMarshQMUL | 0:4374caa1ef17 | 76 | redThread.start(&red_thread) ; // start the red thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 77 | greenThread.start(&green_thread) ; // start the green thread |
Peilingyi | 5:c9802e8dcbcf | 78 | blueThread.start(&blue_thread) ; // start the blue thread |
Peilingyi | 5:c9802e8dcbcf | 79 | mixcolorThread.start(&mixcolor_thread) ; // start the mixcolor thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 80 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 81 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 82 | uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 |
WilliamMarshQMUL | 0:4374caa1ef17 | 83 | // When no touch --> 0 |
WilliamMarshQMUL | 0:4374caa1ef17 | 84 | // Left --> low value Right --> high value |
WilliamMarshQMUL | 0:4374caa1ef17 | 85 | pc.printf("%d", d) ; |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 86 | pc.putc(' ') ; |
Peilingyi | 5:c9802e8dcbcf | 87 | if (pos == 0)//first touch |
Peilingyi | 5:c9802e8dcbcf | 88 | { |
Peilingyi | 5:c9802e8dcbcf | 89 | if ((d>3) &&(d<9)) |
Peilingyi | 5:c9802e8dcbcf | 90 | { |
Peilingyi | 5:c9802e8dcbcf | 91 | redThread.signal_set(0x1); |
Peilingyi | 5:c9802e8dcbcf | 92 | pos=1 ; |
Peilingyi | 5:c9802e8dcbcf | 93 | } |
Peilingyi | 5:c9802e8dcbcf | 94 | |
Peilingyi | 5:c9802e8dcbcf | 95 | if ((d>13) &&(d<19)) |
Peilingyi | 5:c9802e8dcbcf | 96 | { |
Peilingyi | 5:c9802e8dcbcf | 97 | pos =2; |
Peilingyi | 5:c9802e8dcbcf | 98 | greenThread.signal_set(0x1); |
Peilingyi | 5:c9802e8dcbcf | 99 | } |
Peilingyi | 5:c9802e8dcbcf | 100 | if ((d>23) &&(d<29)) |
Peilingyi | 5:c9802e8dcbcf | 101 | { |
Peilingyi | 5:c9802e8dcbcf | 102 | blueThread.signal_set(0x1) ; |
Peilingyi | 5:c9802e8dcbcf | 103 | pos =3; |
Peilingyi | 5:c9802e8dcbcf | 104 | } |
Peilingyi | 5:c9802e8dcbcf | 105 | if (d>33) |
Peilingyi | 5:c9802e8dcbcf | 106 | { |
Peilingyi | 5:c9802e8dcbcf | 107 | mixcolorThread.signal_set(0x1); |
Peilingyi | 5:c9802e8dcbcf | 108 | pos = 4; |
Peilingyi | 5:c9802e8dcbcf | 109 | } |
Peilingyi | 5:c9802e8dcbcf | 110 | |
Peilingyi | 5:c9802e8dcbcf | 111 | } |
Peilingyi | 5:c9802e8dcbcf | 112 | |
Peilingyi | 5:c9802e8dcbcf | 113 | switch (pos) |
Peilingyi | 5:c9802e8dcbcf | 114 | { |
Peilingyi | 5:c9802e8dcbcf | 115 | |
Peilingyi | 5:c9802e8dcbcf | 116 | |
Peilingyi | 5:c9802e8dcbcf | 117 | case 1: |
Peilingyi | 5:c9802e8dcbcf | 118 | { |
Peilingyi | 5:c9802e8dcbcf | 119 | if ((d<3) ||(d>9)) |
Peilingyi | 5:c9802e8dcbcf | 120 | { |
Peilingyi | 5:c9802e8dcbcf | 121 | // redThread.signal_set(0x1) ; |
Peilingyi | 5:c9802e8dcbcf | 122 | pos = 0; |
Peilingyi | 5:c9802e8dcbcf | 123 | } |
Peilingyi | 5:c9802e8dcbcf | 124 | break; |
Peilingyi | 5:c9802e8dcbcf | 125 | } |
Peilingyi | 5:c9802e8dcbcf | 126 | case 2: |
Peilingyi | 5:c9802e8dcbcf | 127 | { |
Peilingyi | 5:c9802e8dcbcf | 128 | if ((d<13) ||(d>19)) |
Peilingyi | 5:c9802e8dcbcf | 129 | { |
Peilingyi | 5:c9802e8dcbcf | 130 | // greenThread.signal_set(0x1) ; |
Peilingyi | 5:c9802e8dcbcf | 131 | pos = 0; |
Peilingyi | 5:c9802e8dcbcf | 132 | } |
Peilingyi | 5:c9802e8dcbcf | 133 | break; |
Peilingyi | 5:c9802e8dcbcf | 134 | } |
Peilingyi | 5:c9802e8dcbcf | 135 | case 3: |
Peilingyi | 5:c9802e8dcbcf | 136 | { |
Peilingyi | 5:c9802e8dcbcf | 137 | if ((d<23) ||(d>29)) |
Peilingyi | 5:c9802e8dcbcf | 138 | { |
Peilingyi | 5:c9802e8dcbcf | 139 | // blueThread.signal_set(0x1) ; |
Peilingyi | 5:c9802e8dcbcf | 140 | pos = 0; |
Peilingyi | 5:c9802e8dcbcf | 141 | } |
Peilingyi | 5:c9802e8dcbcf | 142 | break; |
Peilingyi | 5:c9802e8dcbcf | 143 | } |
Peilingyi | 5:c9802e8dcbcf | 144 | case 4: |
Peilingyi | 5:c9802e8dcbcf | 145 | { |
Peilingyi | 5:c9802e8dcbcf | 146 | if (d<33) |
Peilingyi | 5:c9802e8dcbcf | 147 | { |
Peilingyi | 5:c9802e8dcbcf | 148 | // mixcolorThread.signal_set(0x1) ; |
Peilingyi | 5:c9802e8dcbcf | 149 | pos = 0; |
Peilingyi | 5:c9802e8dcbcf | 150 | } |
Peilingyi | 5:c9802e8dcbcf | 151 | break; |
Peilingyi | 5:c9802e8dcbcf | 152 | } |
Peilingyi | 5:c9802e8dcbcf | 153 | } |
Peilingyi | 5:c9802e8dcbcf | 154 | |
Peilingyi | 5:c9802e8dcbcf | 155 | |
Peilingyi | 5:c9802e8dcbcf | 156 | Thread::wait(100); // This polling rate is too slow - increase it |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 157 | // The slower rate maks it easier to output on the terminal |
WilliamMarshQMUL | 0:4374caa1ef17 | 158 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 159 | } |