Andrew Mkpanam
/
TSI_Code
final
Revision 7:36b690644500, committed 2020-03-20
- Comitter:
- ex19397
- Date:
- Fri Mar 20 09:27:56 2020 +0000
- Parent:
- 6:71ef35e456ab
- Commit message:
- final
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed_app.json | Show annotated file Show diff for this revision Revisions of this file |
diff -r 71ef35e456ab -r 36b690644500 main.cpp --- a/main.cpp Thu Feb 20 11:28:36 2020 +0000 +++ b/main.cpp Fri Mar 20 09:27:56 2020 +0000 @@ -10,44 +10,137 @@ Serial pc(USBTX, USBRX); // tx, rx DigitalOut redLED(LED_RED); DigitalOut greenLED(LED_GREEN); +DigitalOut blueLED(LED_BLUE); TSISensor tsi; + Thread redThread ; // thread for red LED Thread greenThread ; // thread for green LED +Thread blueThread; +Thread whiteThread; -# define REDFLAG 0x01 -# define GREENFLAG 0x02 -EventFlags signals; // event flags for signalling; 2 used + + +# define FLAG1 0x01 +# define FLAG2 0x02 +# define FLAG3 0x04 +# define FLAG4 0x08 +EventFlags signals; // event flags for signalling; 4 used -void red_thread() { // method to run in thread +void red_thread() // method to run in thread +{ +redLED = true ; // turn off +greenLED = true ; // turn off +blueLED = true; + enum state {ON, OFF} lev ; + lev = OFF; while (true) { - signals.wait_any(REDFLAG); - redLED = false ; // turn on - ThisThread::sleep_for(5000) ; // wait(5.0); - redLED = true ; // turn off - signals.clear(REDFLAG) ; - // Signal are automatically cleared by wait_any but - // the signal might have been set again while LED on + switch (lev) { + case ON: + signals.wait_any(FLAG1); + redLED = true ; // turn off + //signals.clear(FLAG1) ; + lev= OFF; + break ; + case OFF: + signals.wait_any(FLAG1); + redLED = false ; // turn off + lev = ON; + break ; + //signals.clear(FLAG1) ; + // Signal are automatically cleared by wait_any but + } // the signal might have been set again while LED on } } -void green_thread() { // method to run in thread +void green_thread() // method to run in thread +{ + redLED = true ; // turn off + greenLED = true ; // turn off + blueLED = true; + enum state {ON, OFF} lev ; + lev = OFF; + while (true) { + switch (lev) { + case ON: + signals.wait_any(FLAG2); + greenLED = true ; // turn off + lev = OFF; + break ; + //signals.clear(FLAG2) ; + case OFF: + signals.wait_any(FLAG2); + greenLED = false ; // turn off + lev = ON; + break ; + + //signals.clear(FLAG2) ; + // Signal are automatically cleared by wait_any but + // the signal might have been set again while LED on + } + } +} +void blue_thread() // method to run in thread +{ +redLED = true ; // turn off +greenLED = true ; // turn off +blueLED = true; + enum state {ON, OFF} lev ; + lev = OFF; while (true) { - signals.wait_any(GREENFLAG); - greenLED = false ; // turn on - ThisThread::sleep_for(5000) ; // wait(5.0); - greenLED = true ; // turn off - signals.clear(GREENFLAG) ; + switch (lev) { + case ON: + signals.wait_any(FLAG3); + blueLED = true ; // turn off + lev = OFF ; + break ; + //signals.clear(FLAG3) ; + case OFF: + signals.wait_any(FLAG3); + blueLED = false ; // turn off + lev = ON ; + break ; + //signals.clear(FLAG3) ; + // Signal are automatically cleared by wait_any but + // the signal might have been set again while LED on + } + } +} +void white_thread() { // method to run in thread +redLED = true ; // turn off +greenLED = true ; // turn off +blueLED = true; +enum state {ON, OFF} lev ; + lev = OFF; + while (true) { + switch (lev){ + case ON: + signals.wait_any(FLAG4); + break ; + lev = OFF; + case OFF: + signals.wait_any(FLAG4); + break ; + lev = ON; + //signals.clear(FLAG4) ; // Signal are automatically cleared by wait_any but // the signal might have been set again while LED on + } } } int main(void) { - redLED = true ; // turn off - greenLED = true ; // turn off + //redLED = true ; // turn off + //greenLED = true ; // turn off + //blueLED = true; + redThread.start(red_thread) ; // start the red thread greenThread.start(green_thread) ; // start the green thread + blueThread.start(blue_thread) ; // start the red thread + whiteThread.start(white_thread) ; // start the green thread + + enum position { none, leftout, leftin, rightin, rightout,} pos; + pos = none; while (true) { uint8_t d = tsi.readDistance() ; // Distance is between 0 and 39 @@ -55,9 +148,62 @@ // Left --> low value Right --> high value pc.printf("%d", d) ; pc.putc(' ') ; - if (d == 10) signals.set(REDFLAG) ; - if (d == 20) signals.set(GREENFLAG) ; + switch (pos) { + + case none: + if ((d > 3)&& (d<9)) { + pos = leftout; + signals.set(FLAG1); + pc.printf("Case Leftout \n\r", d); + } + if ((d > 13)&&(d<19)) { + pos = leftin; + signals.set(FLAG2); + pc.printf("Case Leftin \n\r", d); + } + if ((d > 23)&&(d <29)) { + pos = rightin; + signals.set(FLAG3); + pc.printf("Case Rightin \n\r", d); + } + if (d > 33) { + pos = rightout; + signals.set(FLAG4); + pc.printf("Case Rightiout \n\r", d); + } + break; + + case leftout: + if ((d < 3)||(d>9)) { + pos = none; + pc.printf("Case None \n\r", d); + } + break; + + case leftin: + if ((d < 13)||(d>19)) { + pos = none; + pc.printf("Case None \n\r", d); + } + break; + + case rightin: + if ((d < 23)||(d>29)) { + pos = none; + pc.printf("Case None \n\r", d); + } + break; + case rightout: + if (d < 33) { + pos = none; + pc.printf("Case None \n\r", d); + } + break; + } + + //if (d == 10) signals.set(REDFLAG) ; + //if (d == 20) signals.set(GREENFLAG) ; ThisThread::sleep_for(500) ; // This polling rate is too slow - increase it - // The slower rate maks it easier to output on the terminal + // The slower rate maks it easier to output on the terminal } } \ No newline at end of file
diff -r 71ef35e456ab -r 36b690644500 mbed_app.json --- a/mbed_app.json Thu Feb 20 11:28:36 2020 +0000 +++ b/mbed_app.json Fri Mar 20 09:27:56 2020 +0000 @@ -2,6 +2,9 @@ "config": { "main-stack-size": { "value": 2000 + }, + "thread-stack-size": { + "value": 1000 } } } \ No newline at end of file