Lab5

Dependencies:   TSI

Fork of TSI_sample by William Marsh

Committer:
Bossman
Date:
Fri Mar 02 18:45:24 2018 +0000
Revision:
5:6601376ae1b8
Parent:
4:d54e74fbf82c
Version 1

Who changed what in which revision?

UserRevisionLine numberNew 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);
Bossman 5:6601376ae1b8 11 DigitalOut blueLED(LED_BLUE);
Bossman 5:6601376ae1b8 12 DigitalOut led(PTD5);
WilliamMarshQMUL 0:4374caa1ef17 13 TSISensor tsi;
Bossman 5:6601376ae1b8 14 Thread redThread(osPriorityNormal,1000) ; // thread for red LED
Bossman 5:6601376ae1b8 15 Thread greenThread(osPriorityNormal,1000) ; // thread for green LED
Bossman 5:6601376ae1b8 16 Thread blueThread(osPriorityNormal,1000) ; // thread for blue LED
Bossman 5:6601376ae1b8 17 Thread whiteThread(osPriorityNormal,1000) ; // thread for mix LED
WilliamMarshQMUL 0:4374caa1ef17 18
Bossman 5:6601376ae1b8 19 void red_thread() // methbd to run in thread
Bossman 5:6601376ae1b8 20 {
Bossman 5:6601376ae1b8 21 while (true) {
Bossman 5:6601376ae1b8 22 Thread::signal_wait(0x1);
Bossman 5:6601376ae1b8 23 redLED = !redLED; // turn on
Bossman 5:6601376ae1b8 24 redThread.signal_clr(0x1) ;
Bossman 5:6601376ae1b8 25 // Signal are automatically cleared by wait_signal but
Bossman 5:6601376ae1b8 26 // the signal might have been set again while LED on
Bossman 5:6601376ae1b8 27 }
Bossman 5:6601376ae1b8 28 }
Bossman 5:6601376ae1b8 29 void blue_thread() // methbd to run in thread
Bossman 5:6601376ae1b8 30 {
WilliamMarshQMUL 0:4374caa1ef17 31 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 32 Thread::signal_wait(0x1);
Bossman 5:6601376ae1b8 33 blueLED = !blueLED;
Bossman 5:6601376ae1b8 34 blueThread.signal_clr(0x1) ;
Bossman 5:6601376ae1b8 35 // Signal are automatically cleared by wait_signal but
Bossman 5:6601376ae1b8 36 // the signal might have been set again while LED on
Bossman 5:6601376ae1b8 37 }
Bossman 5:6601376ae1b8 38 }
Bossman 5:6601376ae1b8 39 void green_thread() // methbd to run in thread
Bossman 5:6601376ae1b8 40 {
Bossman 5:6601376ae1b8 41 while (true) {
Bossman 5:6601376ae1b8 42 Thread::signal_wait(0x1);
Bossman 5:6601376ae1b8 43 greenLED = !greenLED;
Bossman 5:6601376ae1b8 44 greenThread.signal_clr(0x1) ;
Bossman 5:6601376ae1b8 45 // Signal are automatically cleared by wait_signal but
Bossman 5:6601376ae1b8 46 // the signal might have been set again while LED on
Bossman 5:6601376ae1b8 47 }
Bossman 5:6601376ae1b8 48 }
Bossman 5:6601376ae1b8 49 void white_thread() // methbd to run in thread
Bossman 5:6601376ae1b8 50 {
Bossman 5:6601376ae1b8 51 while (true) {
Bossman 5:6601376ae1b8 52 Thread::signal_wait(0x1);
Bossman 5:6601376ae1b8 53 led=!led;
Bossman 5:6601376ae1b8 54 whiteThread.signal_clr(0x1) ;
Bossman 5:6601376ae1b8 55 // Signal are automatically cleared by wait_signal but
Bossman 5:6601376ae1b8 56 // the signal might have been set again while LED on
WilliamMarshQMUL 0:4374caa1ef17 57 }
WilliamMarshQMUL 0:4374caa1ef17 58 }
WilliamMarshQMUL 0:4374caa1ef17 59
Bossman 5:6601376ae1b8 60 int main(void)
Bossman 5:6601376ae1b8 61 {
Bossman 5:6601376ae1b8 62 int pos=1;
Bossman 5:6601376ae1b8 63 redLED = 1; // turn off
Bossman 5:6601376ae1b8 64 greenLED = 1; // turn off
Bossman 5:6601376ae1b8 65 blueLED = 1;
Bossman 5:6601376ae1b8 66 led=1;
Bossman 5:6601376ae1b8 67 redThread.start(&red_thread) ; // start the red thread
Bossman 5:6601376ae1b8 68 greenThread.start(&green_thread) ; // start the green thread
Bossman 5:6601376ae1b8 69 blueThread.start(&blue_thread) ; // start the blue thread
Bossman 5:6601376ae1b8 70 whiteThread.start(&white_thread) ; // start the mixcolor thread
Bossman 5:6601376ae1b8 71
WilliamMarshQMUL 0:4374caa1ef17 72 while (true) {
Bossman 5:6601376ae1b8 73 uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39
Bossman 5:6601376ae1b8 74 // When no touch --> 0
Bossman 5:6601376ae1b8 75 // Left --> low value Right --> high value
Bossman 5:6601376ae1b8 76 pc.printf("%d", d) ;
Bossman 5:6601376ae1b8 77 pc.putc(' ') ;
Bossman 5:6601376ae1b8 78 if ((d>3) &&(d<9)) {
Bossman 5:6601376ae1b8 79 if (pos==1) {
Bossman 5:6601376ae1b8 80 redThread.signal_set(0x1) ;
Bossman 5:6601376ae1b8 81 pos=0;
Bossman 5:6601376ae1b8 82 }
Bossman 5:6601376ae1b8 83 } else if ((d>13) &&(d<19)) {
Bossman 5:6601376ae1b8 84 if (pos==1) {
Bossman 5:6601376ae1b8 85 greenThread.signal_set(0x1) ;
Bossman 5:6601376ae1b8 86 pos=0;
Bossman 5:6601376ae1b8 87 }
Bossman 5:6601376ae1b8 88 } else if ((d>23) &&(d<29)) {
Bossman 5:6601376ae1b8 89 if (pos==1) {
Bossman 5:6601376ae1b8 90 blueThread.signal_set(0x1) ;
Bossman 5:6601376ae1b8 91 pos=0;
Bossman 5:6601376ae1b8 92 }
Bossman 5:6601376ae1b8 93 } else if (d>33) {
Bossman 5:6601376ae1b8 94 if (pos==1) {
Bossman 5:6601376ae1b8 95 whiteThread.signal_set(0x1) ;
Bossman 5:6601376ae1b8 96 pos=0;
Bossman 5:6601376ae1b8 97 }
Bossman 5:6601376ae1b8 98 } else if(d == 0) { // touch off
Bossman 5:6601376ae1b8 99 pos=1;
Bossman 5:6601376ae1b8 100 }
Bossman 5:6601376ae1b8 101 Thread::wait(200); // This polling rate is too slow - increase it
Bossman 5:6601376ae1b8 102 // The slower rate maks it easier to output on the terminal
WilliamMarshQMUL 0:4374caa1ef17 103 }
WilliamMarshQMUL 0:4374caa1ef17 104 }
WilliamMarshQMUL 0:4374caa1ef17 105