lab 5 part 2

Dependencies:   TSI

Committer:
sebbarpar
Date:
Mon Mar 16 12:01:39 2020 +0000
Revision:
9:595d1dbf0086
Parent:
8:ca714e821245
Finished lab 5 part 2

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
sebbarpar 9:595d1dbf0086 4 //Lab 5 part 2
WilliamMarshQMUL 0:4374caa1ef17 5
WilliamMarshQMUL 0:4374caa1ef17 6 Serial pc(USBTX, USBRX); // tx, rx
sebbarpar 7:a910936bf4f8 7 PwmOut redLED(LED_RED);
sebbarpar 7:a910936bf4f8 8 PwmOut greenLED(LED_GREEN);
WilliamMarshQMUL 0:4374caa1ef17 9 TSISensor tsi;
sebbarpar 7:a910936bf4f8 10 Timer t;
WilliamMarshQMUL 0:4374caa1ef17 11
sebbarpar 7:a910936bf4f8 12 enum pos{neutral, lo, li, ri, ro};
sebbarpar 7:a910936bf4f8 13
sebbarpar 9:595d1dbf0086 14 Thread green ; // thread for green LED
sebbarpar 9:595d1dbf0086 15 Thread red ; // thread for red LED
WilliamMarshQMUL 5:2a9a3d74a1d8 16
sebbarpar 7:a910936bf4f8 17 # define lout 0x01
sebbarpar 7:a910936bf4f8 18 # define lin 0x02
sebbarpar 7:a910936bf4f8 19 # define rin 0x04
sebbarpar 7:a910936bf4f8 20 # define rout 0x08
sebbarpar 7:a910936bf4f8 21 EventFlags signals;
sebbarpar 8:ca714e821245 22 volatile uint32_t flags_red;
sebbarpar 8:ca714e821245 23 volatile uint32_t flags_green;
sebbarpar 7:a910936bf4f8 24
sebbarpar 7:a910936bf4f8 25 void changebrightnessred() { // method to run in thread
sebbarpar 7:a910936bf4f8 26 float i=0;
WilliamMarshQMUL 0:4374caa1ef17 27 while (true) {
sebbarpar 9:595d1dbf0086 28 signals.wait_any(lin,osWaitForever,false);//Run thread indefinitely after left is is pressed
sebbarpar 8:ca714e821245 29 flags_red=signals.wait_any(lout|rout,osWaitForever,true);
sebbarpar 9:595d1dbf0086 30 if (flags_red==3){//If left out is pressed, less brightness
sebbarpar 7:a910936bf4f8 31 if (i!=0) i--;
sebbarpar 7:a910936bf4f8 32 }
sebbarpar 9:595d1dbf0086 33 if (flags_red==10){//If right out is pressed, more brightness
sebbarpar 7:a910936bf4f8 34 if (i!=9) i++;
sebbarpar 7:a910936bf4f8 35 }
sebbarpar 8:ca714e821245 36 signals.clear(lout);
sebbarpar 8:ca714e821245 37 signals.clear(rout);
sebbarpar 9:595d1dbf0086 38 //pc.printf("flags_red: %d, red: %f \n ",flags_red, i);
sebbarpar 9:595d1dbf0086 39 redLED= 1-i/10;
WilliamMarshQMUL 0:4374caa1ef17 40 }
WilliamMarshQMUL 0:4374caa1ef17 41 }
WilliamMarshQMUL 0:4374caa1ef17 42
sebbarpar 7:a910936bf4f8 43 void changebrightnessgreen() { // method to run in thread
sebbarpar 7:a910936bf4f8 44 float i=0;
WilliamMarshQMUL 0:4374caa1ef17 45 while (true) {
sebbarpar 9:595d1dbf0086 46 signals.wait_any(rin,osWaitForever,false);//Run indefinitely after right in pressed
sebbarpar 8:ca714e821245 47 flags_green=signals.wait_any(lout|rout,osWaitForever,true);
sebbarpar 9:595d1dbf0086 48 if (flags_green==5){//Left out less brightness
sebbarpar 7:a910936bf4f8 49 if (i!=0) i--;
sebbarpar 7:a910936bf4f8 50 }
sebbarpar 9:595d1dbf0086 51 if (flags_green==12){//Right out more brightness
sebbarpar 7:a910936bf4f8 52 if (i!=9) i++;
sebbarpar 7:a910936bf4f8 53 }
sebbarpar 8:ca714e821245 54 signals.clear(lout);
sebbarpar 8:ca714e821245 55 signals.clear(rout);
sebbarpar 9:595d1dbf0086 56 //pc.printf("flags_green: %d, green: %f \n",flags_green, i);
sebbarpar 9:595d1dbf0086 57 greenLED= 1-i/10;
WilliamMarshQMUL 0:4374caa1ef17 58 }
WilliamMarshQMUL 0:4374caa1ef17 59 }
WilliamMarshQMUL 0:4374caa1ef17 60
WilliamMarshQMUL 0:4374caa1ef17 61 int main(void) {
WilliamMarshQMUL 0:4374caa1ef17 62 redLED = true ; // turn off
WilliamMarshQMUL 0:4374caa1ef17 63 greenLED = true ; // turn off
sebbarpar 7:a910936bf4f8 64 red.start(changebrightnessgreen) ; // start the red thread
sebbarpar 7:a910936bf4f8 65 green.start(changebrightnessred) ; // start the green thread
sebbarpar 7:a910936bf4f8 66 int pos=neutral;
WilliamMarshQMUL 0:4374caa1ef17 67 while (true) {
WilliamMarshQMUL 0:4374caa1ef17 68 uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39
WilliamMarshQMUL 0:4374caa1ef17 69 // When no touch --> 0
WilliamMarshQMUL 0:4374caa1ef17 70 // Left --> low value Right --> high value
sebbarpar 7:a910936bf4f8 71 switch (pos){
sebbarpar 7:a910936bf4f8 72 case neutral:
sebbarpar 7:a910936bf4f8 73 if (d>3 && d<9){pos=lo; signals.set(lout); t.start();}
sebbarpar 9:595d1dbf0086 74 if (d>13 && d<19){pos=li; signals.set(lin); signals.clear(rin);} //Select LED
sebbarpar 9:595d1dbf0086 75 if (d>23 && d<29){pos=ri; signals.set(rin);signals.clear(lin);} //Select LED
sebbarpar 7:a910936bf4f8 76 if (d>33){pos=ro; signals.set(rout); t.start();}
sebbarpar 7:a910936bf4f8 77 break;
sebbarpar 7:a910936bf4f8 78 case lo:
sebbarpar 9:595d1dbf0086 79 if (t.read()>1) {signals.set(lout); t.reset();}//Repeat event after 1 second
sebbarpar 7:a910936bf4f8 80 if (d<3 || d>9) {pos=neutral; t.reset(); t.stop();}
sebbarpar 7:a910936bf4f8 81 break;
sebbarpar 7:a910936bf4f8 82 case li:
sebbarpar 8:ca714e821245 83 if (d<13 || d>19) {pos=neutral; }
sebbarpar 7:a910936bf4f8 84 break;
sebbarpar 7:a910936bf4f8 85 case ri:
sebbarpar 8:ca714e821245 86 if (d<23 || d>29) {pos=neutral; }
sebbarpar 7:a910936bf4f8 87 break;
sebbarpar 7:a910936bf4f8 88 case ro:
sebbarpar 9:595d1dbf0086 89 if (t.read()>1) {signals.set(rout);t.reset();}//repeat event after 1 second
sebbarpar 7:a910936bf4f8 90 if (d<33) {pos=neutral; t.reset(); t.stop();}
sebbarpar 7:a910936bf4f8 91 break;
sebbarpar 7:a910936bf4f8 92 }
sebbarpar 9:595d1dbf0086 93 //pc.printf("%d, position: %d", d, pos) ;
sebbarpar 9:595d1dbf0086 94 //pc.putc(' ') ;
sebbarpar 9:595d1dbf0086 95 ThisThread::sleep_for(50) ;
sebbarpar 9:595d1dbf0086 96 }
WilliamMarshQMUL 0:4374caa1ef17 97 }