uses touchpad

Dependencies:   TSI

Fork of TSI_sample by William Marsh

Committer:
edwinkad
Date:
Thu Mar 01 17:35:50 2018 +0000
Revision:
3:f8d74ecd8563
Parent:
1:e6ffa08ad8bf
lab-5 part-1.; Edwin Kadavy

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"
edwinkad 3:f8d74ecd8563 3 //#pragma import __use_two_region_memory
edwinkad 3:f8d74ecd8563 4
WilliamMarshQMUL 0:4374caa1ef17 5
WilliamMarshQMUL 0:4374caa1ef17 6 // Example program for lab 5
WilliamMarshQMUL 0:4374caa1ef17 7 // -------------------------
WilliamMarshQMUL 0:4374caa1ef17 8 // A value is read from the touch sensor and use
WilliamMarshQMUL 0:4374caa1ef17 9 // to control two LEDs
WilliamMarshQMUL 0:4374caa1ef17 10 // The value is also output to the serial interface
WilliamMarshQMUL 0:4374caa1ef17 11
WilliamMarshQMUL 0:4374caa1ef17 12 Serial pc(USBTX, USBRX); // tx, rx
edwinkad 3:f8d74ecd8563 13 DigitalOut redLED(LED1);
edwinkad 3:f8d74ecd8563 14 DigitalOut greenLED(LED2);
edwinkad 3:f8d74ecd8563 15 DigitalOut blueLED(LED3);
edwinkad 3:f8d74ecd8563 16 DigitalOut red2LED(PTD0);
WilliamMarshQMUL 0:4374caa1ef17 17 TSISensor tsi;
WilliamMarshQMUL 0:4374caa1ef17 18
edwinkad 3:f8d74ecd8563 19 Thread thread1;//(osPriorityNormal, 500);
edwinkad 3:f8d74ecd8563 20 Thread thread2;//(osPriorityNormal, 500);
edwinkad 3:f8d74ecd8563 21 Thread thread3;//(osPriorityNormal, 500);
edwinkad 3:f8d74ecd8563 22 Thread thread4;//(osPriorityNormal, 500);
edwinkad 3:f8d74ecd8563 23 Thread thread5;//(osPriorityNormal, 2000);
edwinkad 3:f8d74ecd8563 24
edwinkad 3:f8d74ecd8563 25
edwinkad 3:f8d74ecd8563 26
edwinkad 3:f8d74ecd8563 27 void led1() // method to run in thread
edwinkad 3:f8d74ecd8563 28 {
WilliamMarshQMUL 0:4374caa1ef17 29 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 30 Thread::signal_wait(0x1);
edwinkad 3:f8d74ecd8563 31 redLED = !redLED ; // turn on
edwinkad 3:f8d74ecd8563 32 //Thread::signal_wait(0x2);
edwinkad 3:f8d74ecd8563 33 //redLED = true ; // turn off
edwinkad 3:f8d74ecd8563 34 thread1.signal_clr(0x1);
edwinkad 3:f8d74ecd8563 35 //thread1.signal_clr(0x2) ;
edwinkad 3:f8d74ecd8563 36 // Signal are automatically cleared by wait_signal but
edwinkad 3:f8d74ecd8563 37 // the signal might have been set again while LED on
edwinkad 3:f8d74ecd8563 38 }
edwinkad 3:f8d74ecd8563 39 }
edwinkad 3:f8d74ecd8563 40
edwinkad 3:f8d74ecd8563 41 void led2() // method to run in thread
edwinkad 3:f8d74ecd8563 42 {
edwinkad 3:f8d74ecd8563 43 while (true) {
edwinkad 3:f8d74ecd8563 44 Thread::signal_wait(0x1);
edwinkad 3:f8d74ecd8563 45 greenLED = !greenLED ; // turn on
edwinkad 3:f8d74ecd8563 46 //Thread::signal_wait(0x2);
edwinkad 3:f8d74ecd8563 47 //greenLED = true ; // turn off
edwinkad 3:f8d74ecd8563 48 thread2.signal_clr(0x1) ;
edwinkad 3:f8d74ecd8563 49 //thread2.signal_clr(0x2) ;
edwinkad 3:f8d74ecd8563 50 // Signal are automatically cleared by wait_signal but
edwinkad 3:f8d74ecd8563 51 // the signal might have been set again while LED on
edwinkad 3:f8d74ecd8563 52 }
edwinkad 3:f8d74ecd8563 53 }
edwinkad 3:f8d74ecd8563 54
edwinkad 3:f8d74ecd8563 55 void led3() // method to run in thread
edwinkad 3:f8d74ecd8563 56 {
edwinkad 3:f8d74ecd8563 57 while (true) {
edwinkad 3:f8d74ecd8563 58 Thread::signal_wait(0x1);
edwinkad 3:f8d74ecd8563 59 blueLED = !blueLED ; // turn on
edwinkad 3:f8d74ecd8563 60 //Thread::signal_wait(0x2);
edwinkad 3:f8d74ecd8563 61 //blueLED = true ; // turn off
edwinkad 3:f8d74ecd8563 62 thread3.signal_clr(0x1) ;
edwinkad 3:f8d74ecd8563 63 //thread3.signal_clr(0x2) ;
edwinkad 3:f8d74ecd8563 64 // Signal are automatically cleared by wait_signal but
edwinkad 3:f8d74ecd8563 65 // the signal might have been set again while LED on
WilliamMarshQMUL 0:4374caa1ef17 66 }
WilliamMarshQMUL 0:4374caa1ef17 67 }
WilliamMarshQMUL 0:4374caa1ef17 68
edwinkad 3:f8d74ecd8563 69 void led4() // method to run in thread
edwinkad 3:f8d74ecd8563 70 {
WilliamMarshQMUL 0:4374caa1ef17 71 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 72 Thread::signal_wait(0x1);
edwinkad 3:f8d74ecd8563 73 red2LED = !red2LED ; // turn on
edwinkad 3:f8d74ecd8563 74 //Thread::signal_wait(0x2);
edwinkad 3:f8d74ecd8563 75 //red2LED = true ; // turn off
edwinkad 3:f8d74ecd8563 76 thread4.signal_clr(0x1) ;
edwinkad 3:f8d74ecd8563 77 //thread4.signal_clr(0x2) ;
edwinkad 3:f8d74ecd8563 78 // Signal are automatically cleared by wait_signal but
edwinkad 3:f8d74ecd8563 79 // the signal might have been set again while LED on
edwinkad 3:f8d74ecd8563 80 }
edwinkad 3:f8d74ecd8563 81 }
edwinkad 3:f8d74ecd8563 82 /*
edwinkad 3:f8d74ecd8563 83 void led5() // method to run in thread
edwinkad 3:f8d74ecd8563 84 {
edwinkad 3:f8d74ecd8563 85 while (true) {
edwinkad 3:f8d74ecd8563 86 Thread::signal_wait(0x1);
edwinkad 3:f8d74ecd8563 87 greenLED = false ; // turn on
edwinkad 3:f8d74ecd8563 88 blueLED = false ; // turn on
edwinkad 3:f8d74ecd8563 89 Thread::signal_wait(0x2);
edwinkad 3:f8d74ecd8563 90 greenLED = true ; // turn off
edwinkad 3:f8d74ecd8563 91 blueLED = true ; // turn off
edwinkad 3:f8d74ecd8563 92 thread5.signal_clr(0x1) ;
edwinkad 3:f8d74ecd8563 93 thread5.signal_clr(0x2) ;
edwinkad 3:f8d74ecd8563 94 // Signal are automatically cleared by wait_signal but
edwinkad 3:f8d74ecd8563 95 // the signal might have been set again while LED on
WilliamMarshQMUL 0:4374caa1ef17 96 }
WilliamMarshQMUL 0:4374caa1ef17 97 }
edwinkad 3:f8d74ecd8563 98 */
WilliamMarshQMUL 0:4374caa1ef17 99
edwinkad 3:f8d74ecd8563 100 int main(void)
edwinkad 3:f8d74ecd8563 101 {
edwinkad 3:f8d74ecd8563 102 redLED = true ; // turn off
edwinkad 3:f8d74ecd8563 103 greenLED = true ; // turn off
edwinkad 3:f8d74ecd8563 104 blueLED= true; // turn off
edwinkad 3:f8d74ecd8563 105 red2LED=false;
edwinkad 3:f8d74ecd8563 106 thread1.start(&led1) ; // start the red thread
edwinkad 3:f8d74ecd8563 107 thread2.start(&led2) ; // start the green thread
edwinkad 3:f8d74ecd8563 108 thread3.start(&led3) ; // start the green thread
edwinkad 3:f8d74ecd8563 109 thread4.start(&led4) ; // start the green thread
edwinkad 3:f8d74ecd8563 110
edwinkad 3:f8d74ecd8563 111 int touchstate=0;
WilliamMarshQMUL 0:4374caa1ef17 112 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 113 uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39
edwinkad 3:f8d74ecd8563 114 // When no touch --> 0
edwinkad 3:f8d74ecd8563 115 // Left --> low value Right --> high value
edwinkad 3:f8d74ecd8563 116 pc.printf("%d\n\r", d) ;
WilliamMarshQMUL 1:e6ffa08ad8bf 117 pc.putc(' ') ;
edwinkad 3:f8d74ecd8563 118
edwinkad 3:f8d74ecd8563 119
edwinkad 3:f8d74ecd8563 120 switch (touchstate) {
edwinkad 3:f8d74ecd8563 121 case 0://none state
edwinkad 3:f8d74ecd8563 122 if (d>3 && d<9) {
edwinkad 3:f8d74ecd8563 123 touchstate=1;
edwinkad 3:f8d74ecd8563 124 thread1.signal_set(0x1);
edwinkad 3:f8d74ecd8563 125 } else if (d>13 && d<19) {
edwinkad 3:f8d74ecd8563 126 touchstate=2;
edwinkad 3:f8d74ecd8563 127 thread2.signal_set(0x1);
edwinkad 3:f8d74ecd8563 128 } else if (d>23 && d<29) {
edwinkad 3:f8d74ecd8563 129 touchstate=3;
edwinkad 3:f8d74ecd8563 130 thread3.signal_set(0x1);
edwinkad 3:f8d74ecd8563 131 } else if (d>33) {
edwinkad 3:f8d74ecd8563 132 touchstate=4;
edwinkad 3:f8d74ecd8563 133 thread4.signal_set(0x1);
edwinkad 3:f8d74ecd8563 134 }
edwinkad 3:f8d74ecd8563 135 break;
edwinkad 3:f8d74ecd8563 136 case 1://leftOut state
edwinkad 3:f8d74ecd8563 137 if (d<3 || d>9 && touchstate==1) {
edwinkad 3:f8d74ecd8563 138 touchstate=0;
edwinkad 3:f8d74ecd8563 139 //thread1.signal_set(0x2);
edwinkad 3:f8d74ecd8563 140 }
edwinkad 3:f8d74ecd8563 141 break;
edwinkad 3:f8d74ecd8563 142 case 2://leftIn
edwinkad 3:f8d74ecd8563 143 if (d<13 || d>19 && touchstate==2 ) {
edwinkad 3:f8d74ecd8563 144 touchstate=0;
edwinkad 3:f8d74ecd8563 145 //thread2.signal_set(0x2);
edwinkad 3:f8d74ecd8563 146 }
edwinkad 3:f8d74ecd8563 147 break;
edwinkad 3:f8d74ecd8563 148 case 3://rightIn
edwinkad 3:f8d74ecd8563 149 if (d<23 || d>29 && touchstate==3) {
edwinkad 3:f8d74ecd8563 150 touchstate=0;
edwinkad 3:f8d74ecd8563 151 //thread3.signal_set(0x2);
edwinkad 3:f8d74ecd8563 152 }
edwinkad 3:f8d74ecd8563 153 break;
edwinkad 3:f8d74ecd8563 154 case 4://rightOut
edwinkad 3:f8d74ecd8563 155 if (d<33 && touchstate==4 ) {
edwinkad 3:f8d74ecd8563 156 touchstate=0;
edwinkad 3:f8d74ecd8563 157 //thread4.signal_set(0x2);
edwinkad 3:f8d74ecd8563 158 }
edwinkad 3:f8d74ecd8563 159 break;
edwinkad 3:f8d74ecd8563 160 }
edwinkad 3:f8d74ecd8563 161
edwinkad 3:f8d74ecd8563 162 Thread::wait(50); //
WilliamMarshQMUL 0:4374caa1ef17 163 }
WilliamMarshQMUL 0:4374caa1ef17 164 }