first version

Dependencies:   TSI

Fork of TSI_sample by William Marsh

Committer:
cvmanuel
Date:
Thu Mar 01 03:18:40 2018 +0000
Revision:
4:1654ec08fde4
Parent:
3:adf1bff83db6
first version;

Who changed what in which revision?

UserRevisionLine numberNew 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);
cvmanuel 3:adf1bff83db6 14 DigitalOut blueLED(LED_BLUE);
cvmanuel 3:adf1bff83db6 15
WilliamMarshQMUL 0:4374caa1ef17 16 TSISensor tsi;
WilliamMarshQMUL 0:4374caa1ef17 17
cvmanuel 3:adf1bff83db6 18 Thread redThread(osPriorityNormal, 1000); // thread for red LED
cvmanuel 3:adf1bff83db6 19 Thread greenThread(osPriorityNormal, 1000); // thread for green LED
cvmanuel 3:adf1bff83db6 20 Thread blueThread(osPriorityNormal, 1000); // thread for green LED
cvmanuel 3:adf1bff83db6 21 Thread allThread(osPriorityNormal, 1000); // thread for green LED
WilliamMarshQMUL 0:4374caa1ef17 22
WilliamMarshQMUL 0:4374caa1ef17 23 void red_thread() { // method to run in thread
WilliamMarshQMUL 0:4374caa1ef17 24 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 25 Thread::signal_wait(0x1);
WilliamMarshQMUL 0:4374caa1ef17 26 redLED = false ; // turn on
WilliamMarshQMUL 0:4374caa1ef17 27 Thread::wait(5000);
WilliamMarshQMUL 0:4374caa1ef17 28 redLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 29 redThread.signal_clr(0x1) ;
WilliamMarshQMUL 0:4374caa1ef17 30 // Signal are automatically cleared by wait_signal but
WilliamMarshQMUL 0:4374caa1ef17 31 // the signal might have been set again while LED on
WilliamMarshQMUL 0:4374caa1ef17 32 }
WilliamMarshQMUL 0:4374caa1ef17 33 }
WilliamMarshQMUL 0:4374caa1ef17 34
WilliamMarshQMUL 0:4374caa1ef17 35 void green_thread() { // method to run in thread
WilliamMarshQMUL 0:4374caa1ef17 36 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 37 Thread::signal_wait(0x1);
WilliamMarshQMUL 0:4374caa1ef17 38 greenLED = false ; // turn on
WilliamMarshQMUL 0:4374caa1ef17 39 Thread::wait(5000);
WilliamMarshQMUL 0:4374caa1ef17 40 greenLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 41 greenThread.signal_clr(0x1) ;
WilliamMarshQMUL 0:4374caa1ef17 42 // Signal are automatically cleared by wait_signal but
WilliamMarshQMUL 0:4374caa1ef17 43 // the signal might have been set again while LED on
WilliamMarshQMUL 0:4374caa1ef17 44 }
WilliamMarshQMUL 0:4374caa1ef17 45 }
WilliamMarshQMUL 0:4374caa1ef17 46
cvmanuel 3:adf1bff83db6 47 void blue_thread() { // method to run in thread
cvmanuel 3:adf1bff83db6 48 while (true) {
cvmanuel 3:adf1bff83db6 49 Thread::signal_wait(0x1);
cvmanuel 3:adf1bff83db6 50 blueLED = false ; // turn on
cvmanuel 3:adf1bff83db6 51 Thread::wait(5000);
cvmanuel 3:adf1bff83db6 52 blueLED = true ; // turn off
cvmanuel 3:adf1bff83db6 53 redThread.signal_clr(0x1) ;
cvmanuel 3:adf1bff83db6 54 // Signal are automatically cleared by wait_signal but
cvmanuel 3:adf1bff83db6 55 // the signal might have been set again while LED on
cvmanuel 3:adf1bff83db6 56 }
cvmanuel 3:adf1bff83db6 57 }
cvmanuel 3:adf1bff83db6 58
cvmanuel 3:adf1bff83db6 59 void all_thread() { // method to run in thread
cvmanuel 3:adf1bff83db6 60 while (true) {
cvmanuel 3:adf1bff83db6 61 Thread::signal_wait(0x1);
cvmanuel 3:adf1bff83db6 62 redLED = false;
cvmanuel 3:adf1bff83db6 63 greenLED = false ; // turn on
cvmanuel 3:adf1bff83db6 64 blueLED = false;
cvmanuel 3:adf1bff83db6 65 Thread::wait(5000);
cvmanuel 3:adf1bff83db6 66 greenLED = true ; // turn off
cvmanuel 4:1654ec08fde4 67 redLED = true;
cvmanuel 4:1654ec08fde4 68 blueLED = true;
cvmanuel 3:adf1bff83db6 69 greenThread.signal_clr(0x1) ;
cvmanuel 3:adf1bff83db6 70 // Signal are automatically cleared by wait_signal but
cvmanuel 3:adf1bff83db6 71 // the signal might have been set again while LED on
cvmanuel 3:adf1bff83db6 72 }
cvmanuel 3:adf1bff83db6 73 }
cvmanuel 3:adf1bff83db6 74
cvmanuel 3:adf1bff83db6 75 enum softkeys {outer_left,inner_left,outer_right,inner_right, grey };
cvmanuel 3:adf1bff83db6 76
WilliamMarshQMUL 0:4374caa1ef17 77 int main(void) {
WilliamMarshQMUL 0:4374caa1ef17 78 redLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 79 greenLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 80 redThread.start(&red_thread) ; // start the red thread
WilliamMarshQMUL 0:4374caa1ef17 81 greenThread.start(&green_thread) ; // start the green thread
cvmanuel 4:1654ec08fde4 82 blueThread.start(&blue_thread) ; // start the blue thread
cvmanuel 4:1654ec08fde4 83 allThread.start(&all_thread) ; // start the all thread
WilliamMarshQMUL 0:4374caa1ef17 84
cvmanuel 3:adf1bff83db6 85 softkeys key = grey;
cvmanuel 3:adf1bff83db6 86
WilliamMarshQMUL 0:4374caa1ef17 87 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 88 uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39
cvmanuel 3:adf1bff83db6 89
cvmanuel 3:adf1bff83db6 90 if(d>3 && d <9)
cvmanuel 3:adf1bff83db6 91 {
cvmanuel 3:adf1bff83db6 92 key = outer_left;
cvmanuel 4:1654ec08fde4 93 redThread.signal_set(0x1) ;
cvmanuel 3:adf1bff83db6 94
cvmanuel 3:adf1bff83db6 95 }
cvmanuel 3:adf1bff83db6 96 else
cvmanuel 3:adf1bff83db6 97 if( d>13 && d < 19)
cvmanuel 3:adf1bff83db6 98 {
cvmanuel 3:adf1bff83db6 99 key=inner_left;
cvmanuel 4:1654ec08fde4 100 greenThread.signal_set(0x1) ;
cvmanuel 3:adf1bff83db6 101
cvmanuel 3:adf1bff83db6 102 }else
cvmanuel 3:adf1bff83db6 103 if(d>23 && d < 29)
cvmanuel 3:adf1bff83db6 104 {
cvmanuel 3:adf1bff83db6 105 key = inner_right;
cvmanuel 4:1654ec08fde4 106 blueThread.signal_set(0x1) ;
cvmanuel 3:adf1bff83db6 107 }else
cvmanuel 3:adf1bff83db6 108 if(d>33)
cvmanuel 3:adf1bff83db6 109 {
cvmanuel 3:adf1bff83db6 110 key = outer_right;
cvmanuel 4:1654ec08fde4 111 allThread.signal_set(0x1) ;
cvmanuel 3:adf1bff83db6 112 }
cvmanuel 3:adf1bff83db6 113
cvmanuel 3:adf1bff83db6 114 switch(key){
cvmanuel 3:adf1bff83db6 115 case outer_left :
cvmanuel 3:adf1bff83db6 116 if (d<3 || d >9 ) { //
cvmanuel 3:adf1bff83db6 117 key= grey;
cvmanuel 3:adf1bff83db6 118 }
cvmanuel 3:adf1bff83db6 119 break ;
cvmanuel 3:adf1bff83db6 120 case inner_left :
cvmanuel 3:adf1bff83db6 121 if (d<13 || d >19 ) { //
cvmanuel 3:adf1bff83db6 122 key= grey;
cvmanuel 3:adf1bff83db6 123 }
cvmanuel 3:adf1bff83db6 124 break ;
cvmanuel 3:adf1bff83db6 125 case inner_right :
cvmanuel 3:adf1bff83db6 126 if (d<23 || d >29 ) { //
cvmanuel 3:adf1bff83db6 127 key = grey;
cvmanuel 3:adf1bff83db6 128 }
cvmanuel 3:adf1bff83db6 129 break ;
cvmanuel 3:adf1bff83db6 130 case outer_right :
cvmanuel 3:adf1bff83db6 131 if (d<33) { //
cvmanuel 3:adf1bff83db6 132 key = grey;
cvmanuel 3:adf1bff83db6 133 }
cvmanuel 3:adf1bff83db6 134 break ;
cvmanuel 4:1654ec08fde4 135 case grey:
cvmanuel 4:1654ec08fde4 136 greenLED = true ; // turn off
cvmanuel 4:1654ec08fde4 137 redLED = true;
cvmanuel 4:1654ec08fde4 138 blueLED = true;
cvmanuel 4:1654ec08fde4 139 break;
cvmanuel 3:adf1bff83db6 140 }
cvmanuel 4:1654ec08fde4 141
WilliamMarshQMUL 0:4374caa1ef17 142 pc.printf("%d", d) ;
WilliamMarshQMUL 1:e6ffa08ad8bf 143 pc.putc(' ') ;
cvmanuel 4:1654ec08fde4 144
WilliamMarshQMUL 1:e6ffa08ad8bf 145 Thread::wait(200); // This polling rate is too slow - increase it
WilliamMarshQMUL 1:e6ffa08ad8bf 146 // The slower rate maks it easier to output on the terminal
WilliamMarshQMUL 0:4374caa1ef17 147 }
WilliamMarshQMUL 0:4374caa1ef17 148 }