Soheil Novinfard
/
digitalInInterrupt_modified
Interrupt - modified version
Fork of digitalInInterrupt_sample by
Diff: main.cpp
- Revision:
- 4:f2c84efa572d
- Parent:
- 3:05b6a1431a6b
--- a/main.cpp Tue Jan 16 18:14:21 2018 +0000 +++ b/main.cpp Thu Feb 01 16:42:49 2018 +0000 @@ -2,35 +2,85 @@ // Labs 2: Example program for using an interrupt (or callback) // ----------------------------------------------------------- -// A callback function (corresponding to an ISR) is called when a button +// A callback function (corresponding to an ISR) is called when a button // is pressed // The callback uses a shared variable to signal another thread -InterruptIn button(PTD0); -DigitalOut led(LED_GREEN); +InterruptIn button_blue(PTD0); +DigitalOut led_blue(LED_BLUE); -volatile int pressEvent = 0 ; +InterruptIn button_red(PTD4); +DigitalOut led_red(LED_RED); + +volatile int pressEvent_blue = 0; +volatile int pressEvent_red = 0; + // This function is invoked when then interrupt occurs // Signal that the button has been pressed -// Note: bounce may occur -void buttonCallback(){ - pressEvent = 1 ; +// Note: bounce may occur +void buttonCallback_blue() +{ + pressEvent_blue = 1 ; +} +void buttonCallback_red() +{ + pressEvent_red = 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 +int main() +{ + led_blue = 0; + led_red = 0; + + button_blue.mode(PullUp); // Ensure button i/p has pull up + button_blue.fall(&buttonCallback_blue) ; // Attach function to falling edge + + button_red.mode(PullUp); + button_red.fall(&buttonCallback_red); + + // active mode of each LED + int active_blue = 1; + int active_red = 1; + + // cycle counter of each LED + int cycle_blue = 1; + int cycle_red = 1; while(true) { - // Toggle the LED every time the button is pressed - if (pressEvent) { - led = !led ; - pressEvent = 0 ; // Clear the event variable + // blue led + if (pressEvent_blue) { + active_blue = !active_blue; + pressEvent_blue = 0 ; // Clear the event variable + cycle_blue = 1; + } + if(active_blue) { + if(cycle_blue == 5) { + led_blue = !led_blue ; + cycle_blue = 1; + } else { + cycle_blue = cycle_blue+1; + } } + + // red led + if (pressEvent_red) { + active_red = !active_red; + pressEvent_red = 0 ; // Clear the event variable + cycle_red = 1; + } + if(active_red) { + if(cycle_red == 5) { + led_red = !led_red ; + cycle_red = 1; + } else { + cycle_red = cycle_red+1; + } + } + Thread::wait(100) ; } } \ No newline at end of file