Anastasios Barlas
/
digitalInInterrupt_sample
Lab 2 answer to part 2
Fork of digitalInInterrupt_sample by
Diff: main.cpp
- Revision:
- 3:05b6a1431a6b
- Parent:
- 2:071f22412cdc
- Child:
- 4:86fd8725e198
--- a/main.cpp Tue Jan 24 18:31:35 2017 +0000 +++ b/main.cpp Tue Jan 16 18:14:21 2018 +0000 @@ -1,5 +1,4 @@ #include "mbed.h" -#include "rtos.h" // Labs 2: Example program for using an interrupt (or callback) // ----------------------------------------------------------- @@ -8,32 +7,30 @@ // The callback uses a shared variable to signal another thread InterruptIn button(PTD0); -DigitalOut led(LED_RED); +DigitalOut led(LED_GREEN); -Thread flashT ; volatile int pressEvent = 0 ; -// Signal when the button is pressed +// This function is invoked when then interrupt occurs +// Signal that the button has been pressed // Note: bounce may occur void buttonCallback(){ pressEvent = 1 ; } -// Toggle the LED every time the button is pressed -// Note: the LED could be toggled in the callback -void flash() { +/* ---- 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 + while(true) { + // Toggle the LED every time the button is pressed if (pressEvent) { led = !led ; pressEvent = 0 ; // Clear the event variable } Thread::wait(100) ; } -} - -int main() { - button.mode(PullUp); // Ensure button i/p has pull up - button.fall(&buttonCallback) ; // Attach function to falling edge - - flashT.start(&flash) ; // Start the flashing thread running } \ No newline at end of file