![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Lab 5
Fork of TSI_sample by
main.cpp@5:dc9f3a2ebce9, 2018-03-02 (annotated)
- Committer:
- AnastasiosBarlas
- Date:
- Fri Mar 02 22:11:44 2018 +0000
- Revision:
- 5:dc9f3a2ebce9
- Parent:
- 4:d54e74fbf82c
Version 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 | // Example program for lab 5 |
WilliamMarshQMUL | 0:4374caa1ef17 | 4 | // ------------------------- |
WilliamMarshQMUL | 0:4374caa1ef17 | 5 | // A value is read from the touch sensor and use |
WilliamMarshQMUL | 0:4374caa1ef17 | 6 | // to control two LEDs |
WilliamMarshQMUL | 0:4374caa1ef17 | 7 | // The value is also output to the serial interface |
WilliamMarshQMUL | 0:4374caa1ef17 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
WilliamMarshQMUL | 0:4374caa1ef17 | 9 | DigitalOut redLED(LED_RED); |
WilliamMarshQMUL | 0:4374caa1ef17 | 10 | DigitalOut greenLED(LED_GREEN); |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 11 | DigitalOut blueLED(LED_BLUE); |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 12 | DigitalOut MIXLED(PTD5); |
WilliamMarshQMUL | 0:4374caa1ef17 | 13 | TSISensor tsi; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 14 | Thread redThread(osPriorityNormal,1000) ; // thread for red LED |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 15 | Thread greenThread(osPriorityNormal,1000) ; // thread for green LED |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 16 | Thread blueThread(osPriorityNormal,1000) ; // thread for blue LED |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 17 | Thread mixcolorThread(osPriorityNormal,1000) ; // thread for mix LED |
WilliamMarshQMUL | 0:4374caa1ef17 | 18 | |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 19 | void red_thread() { // methbd to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 20 | while (true) { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 21 | Thread::signal_wait(0x1); |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 22 | redLED = !redLED; // turn on |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 23 | // blueLED = 1; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 24 | // greenLED = 1; |
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 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 30 | void blue_thread() { // methbd to run in thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 31 | while (true) { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 32 | Thread::signal_wait(0x1); |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 33 | // redLED = 1; // turn on |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 34 | blueLED = !blueLED; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 35 | // greenLED = 1; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 36 | blueThread.signal_clr(0x1) ; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 37 | // Signal are automatically cleared by wait_signal but |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 38 | // the signal might have been set again while LED on |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 39 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 40 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 41 | void green_thread() { // methbd to run in thread |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 42 | while (true) { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 43 | Thread::signal_wait(0x1); |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 44 | // redLED = 1; // turn on |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 45 | // blueLED = 1; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 46 | greenLED = !greenLED; |
WilliamMarshQMUL | 0:4374caa1ef17 | 47 | greenThread.signal_clr(0x1) ; |
WilliamMarshQMUL | 0:4374caa1ef17 | 48 | // Signal are automatically cleared by wait_signal but |
WilliamMarshQMUL | 0:4374caa1ef17 | 49 | // the signal might have been set again while LED on |
WilliamMarshQMUL | 0:4374caa1ef17 | 50 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 51 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 52 | void mixcolor_thread() { // methbd to run in thread |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 53 | while (true) { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 54 | Thread::signal_wait(0x1); |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 55 | // |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 56 | MIXLED = !MIXLED; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 57 | // |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 58 | mixcolorThread.signal_clr(0x1) ; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 59 | // Signal are automatically cleared by wait_signal but |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 60 | // the signal might have been set again while LED on |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 61 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 62 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 63 | int main(void) { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 64 | uint8_t pos = 0; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 65 | redLED = 1; // turn off |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 66 | greenLED = 1; // turn off |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 67 | blueLED = 1; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 68 | MIXLED = 0; |
WilliamMarshQMUL | 0:4374caa1ef17 | 69 | redThread.start(&red_thread) ; // start the red thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 70 | greenThread.start(&green_thread) ; // start the green thread |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 71 | blueThread.start(&blue_thread) ; // start the blue thread |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 72 | mixcolorThread.start(&mixcolor_thread) ; // start the mixcolor thread |
WilliamMarshQMUL | 0:4374caa1ef17 | 73 | |
WilliamMarshQMUL | 0:4374caa1ef17 | 74 | while (true) { |
WilliamMarshQMUL | 0:4374caa1ef17 | 75 | uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 |
WilliamMarshQMUL | 0:4374caa1ef17 | 76 | // When no touch --> 0 |
WilliamMarshQMUL | 0:4374caa1ef17 | 77 | // Left --> low value Right --> high value |
WilliamMarshQMUL | 0:4374caa1ef17 | 78 | pc.printf("%d", d) ; |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 79 | pc.putc(' ') ; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 80 | if (pos == 0)//first touch |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 81 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 82 | if ((d>3) &&(d<9))pos = 1;//outerleft |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 83 | if ((d>13) &&(d<19))pos = 2;//interleft |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 84 | if ((d>23) &&(d<29))pos = 3;//interright; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 85 | if (d>33) pos = 4;//outerright; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 86 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 87 | |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 88 | switch (pos) |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 89 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 90 | case 1: |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 91 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 92 | if ((d<3) ||(d>9)) |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 93 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 94 | redThread.signal_set(0x1) ; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 95 | pos = 0; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 96 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 97 | break; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 98 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 99 | case 2: |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 100 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 101 | if ((d<13) ||(d>19)) |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 102 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 103 | greenThread.signal_set(0x1) ; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 104 | pos = 0; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 105 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 106 | break; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 107 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 108 | case 3: |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 109 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 110 | if ((d<23) ||(d>29)) |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 111 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 112 | blueThread.signal_set(0x1) ; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 113 | // pc.printf("blue"); |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 114 | pos = 0; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 115 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 116 | break; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 117 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 118 | case 4: |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 119 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 120 | if (d<33) |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 121 | { |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 122 | mixcolorThread.signal_set(0x1) ; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 123 | pos = 0; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 124 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 125 | break; |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 126 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 127 | } |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 128 | |
AnastasiosBarlas | 5:dc9f3a2ebce9 | 129 | |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 130 | Thread::wait(200); // This polling rate is too slow - increase it |
WilliamMarshQMUL | 1:e6ffa08ad8bf | 131 | // The slower rate maks it easier to output on the terminal |
WilliamMarshQMUL | 0:4374caa1ef17 | 132 | } |
WilliamMarshQMUL | 0:4374caa1ef17 | 133 | } |