Akhil Nair
/
Lab21
lab 2 interrupts
Revision 5:63fd9de15f27, committed 2019-02-07
- Comitter:
- anair12345
- Date:
- Thu Feb 07 09:56:08 2019 +0000
- Parent:
- 4:728667196916
- Commit message:
- lab 2 part 2 full version
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 728667196916 -r 63fd9de15f27 main.cpp --- a/main.cpp Tue Jan 15 10:09:53 2019 +0000 +++ b/main.cpp Thu Feb 07 09:56:08 2019 +0000 @@ -7,9 +7,18 @@ // The callback uses a shared variable to signal another thread InterruptIn button(PTD0); -DigitalOut led(LED_GREEN); +InterruptIn button1(PTD2); +DigitalOut led(LED_BLUE); +DigitalOut led1(LED_RED); volatile int pressEvent = 0 ; +volatile int pressEvent1 = 0 ; + +bool ledflashing = true; +bool led1flashing = true; + +enum blueledState { Flashing, NotFlashing }; +enum redledState { Flashing1, NotFlashing1 }; // This function is invoked when then interrupt occurs // Signal that the button has been pressed @@ -18,19 +27,75 @@ pressEvent = 1 ; } +void buttonCallback1(){ + pressEvent1 = 1 ; +} + /* ---- Main function (default thread) ---- Note that if this thread completes, nothing else works */ int main() { button.mode(PullUp); // Ensure button i/p has pull up button.fall(&buttonCallback) ; // Attach function to falling edge + + button1.mode(PullUp); // Ensure button i/p has pull up + button1.fall(&buttonCallback1) ; // Attach function to falling edge + + led = 1; + led1 = 1; + blueledState State1 = Flashing; + redledState State2 = Flashing1; + int count = 5; + int count1 = 5; while(true) { - // Toggle the LED every time the button is pressed - if (pressEvent) { - led = !led ; - pressEvent = 0 ; // Clear the event variable + wait(0.1); + switch(State1){ + case Flashing: + if(pressEvent){ + State1 = NotFlashing; + pressEvent = 0; + } + else{ + if(count!=0){ + count-- ; + } + else if(count == 0){ + led = !led ; + count = 5; + } + } + case NotFlashing: + if(pressEvent){ + State1 = Flashing; + count = 5; + pressEvent = 0; + } + } + switch(State2){ + case Flashing1: + if(pressEvent1){ + State2 = NotFlashing1; + pressEvent1 = 0; + } + else{ + if(count1!=0){ + count1-- ; + } + else if(count1 == 0){ + led1 = !led1 ; + count1 = 5; + } + } + break; + case NotFlashing1: + if(pressEvent1){ + State2 = Flashing1; + count1 = 5; + pressEvent1 = 0; + } + break; } - wait(0.1) ; + } } \ No newline at end of file