Implemented interrupt code to handle presses of two buttons and control 2 leds separately.
Fork of digitalInInterrupt_sample by
main.cpp@4:c8b84240732a, 2018-02-01 (annotated)
- Committer:
- natgovor
- Date:
- Thu Feb 01 12:30:18 2018 +0000
- Revision:
- 4:c8b84240732a
- Parent:
- 3:05b6a1431a6b
Implemented interrupt code to handle presses of two buttons and control 2 leds separately.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:a66a8cb0012c | 1 | #include "mbed.h" |
WilliamMarshQMUL | 0:a66a8cb0012c | 2 | |
WilliamMarshQMUL | 1:13e0c1956b54 | 3 | // Labs 2: Example program for using an interrupt (or callback) |
WilliamMarshQMUL | 1:13e0c1956b54 | 4 | // ----------------------------------------------------------- |
WilliamMarshQMUL | 1:13e0c1956b54 | 5 | // A callback function (corresponding to an ISR) is called when a button |
WilliamMarshQMUL | 1:13e0c1956b54 | 6 | // is pressed |
WilliamMarshQMUL | 1:13e0c1956b54 | 7 | // The callback uses a shared variable to signal another thread |
WilliamMarshQMUL | 0:a66a8cb0012c | 8 | |
natgovor | 4:c8b84240732a | 9 | InterruptIn redButton(PTD0); |
natgovor | 4:c8b84240732a | 10 | InterruptIn blueButton(PTD5); |
natgovor | 4:c8b84240732a | 11 | DigitalOut redLed(LED_RED); |
natgovor | 4:c8b84240732a | 12 | DigitalOut blueLed(LED_BLUE); |
WilliamMarshQMUL | 0:a66a8cb0012c | 13 | |
natgovor | 4:c8b84240732a | 14 | volatile int redPressEvent = 0; |
natgovor | 4:c8b84240732a | 15 | volatile int bluePressEvent = 0; |
WilliamMarshQMUL | 0:a66a8cb0012c | 16 | |
WilliamMarshQMUL | 3:05b6a1431a6b | 17 | // This function is invoked when then interrupt occurs |
WilliamMarshQMUL | 3:05b6a1431a6b | 18 | // Signal that the button has been pressed |
WilliamMarshQMUL | 1:13e0c1956b54 | 19 | // Note: bounce may occur |
natgovor | 4:c8b84240732a | 20 | void redButtonCallback(){ |
natgovor | 4:c8b84240732a | 21 | redPressEvent = 1 ; |
natgovor | 4:c8b84240732a | 22 | } |
natgovor | 4:c8b84240732a | 23 | |
natgovor | 4:c8b84240732a | 24 | void blueButtonCallback(){ |
natgovor | 4:c8b84240732a | 25 | bluePressEvent = 1 ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 26 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 27 | |
WilliamMarshQMUL | 3:05b6a1431a6b | 28 | /* ---- Main function (default thread) ---- |
WilliamMarshQMUL | 3:05b6a1431a6b | 29 | Note that if this thread completes, nothing else works |
WilliamMarshQMUL | 3:05b6a1431a6b | 30 | */ |
WilliamMarshQMUL | 3:05b6a1431a6b | 31 | int main() { |
natgovor | 4:c8b84240732a | 32 | // LEDa are on by default |
natgovor | 4:c8b84240732a | 33 | redLed = 0; |
natgovor | 4:c8b84240732a | 34 | blueLed = 0; |
natgovor | 4:c8b84240732a | 35 | |
natgovor | 4:c8b84240732a | 36 | int flashPeriod = 5; |
natgovor | 4:c8b84240732a | 37 | int redBtnCounter = flashPeriod; |
natgovor | 4:c8b84240732a | 38 | int blueBtnCounter = flashPeriod; |
natgovor | 4:c8b84240732a | 39 | |
natgovor | 4:c8b84240732a | 40 | bool redIsFlashing = true; |
natgovor | 4:c8b84240732a | 41 | bool blueIsFlashing = true; |
natgovor | 4:c8b84240732a | 42 | |
natgovor | 4:c8b84240732a | 43 | redButton.mode(PullUp); // Ensure button i/p has pull up |
natgovor | 4:c8b84240732a | 44 | redButton.fall(&redButtonCallback) ; // Attach function to falling edge |
natgovor | 4:c8b84240732a | 45 | blueButton.mode(PullUp); // Ensure button i/p has pull up |
natgovor | 4:c8b84240732a | 46 | blueButton.fall(&blueButtonCallback) ; // Attach function to falling edge |
WilliamMarshQMUL | 3:05b6a1431a6b | 47 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 48 | while(true) { |
natgovor | 4:c8b84240732a | 49 | Thread::wait(100); |
natgovor | 4:c8b84240732a | 50 | if (redIsFlashing) { |
natgovor | 4:c8b84240732a | 51 | redBtnCounter--; |
natgovor | 4:c8b84240732a | 52 | } |
natgovor | 4:c8b84240732a | 53 | if (blueIsFlashing) { |
natgovor | 4:c8b84240732a | 54 | blueBtnCounter--; |
natgovor | 4:c8b84240732a | 55 | } |
natgovor | 4:c8b84240732a | 56 | |
natgovor | 4:c8b84240732a | 57 | if (redBtnCounter == 0) { |
natgovor | 4:c8b84240732a | 58 | redLed = !redLed; |
natgovor | 4:c8b84240732a | 59 | redBtnCounter = flashPeriod; |
WilliamMarshQMUL | 0:a66a8cb0012c | 60 | } |
natgovor | 4:c8b84240732a | 61 | if (blueBtnCounter == 0) { |
natgovor | 4:c8b84240732a | 62 | blueLed = !blueLed; |
natgovor | 4:c8b84240732a | 63 | blueBtnCounter = flashPeriod; |
natgovor | 4:c8b84240732a | 64 | } |
natgovor | 4:c8b84240732a | 65 | |
natgovor | 4:c8b84240732a | 66 | // Stop flashing (freeze in the current state) every time the button is pressed |
natgovor | 4:c8b84240732a | 67 | if (redPressEvent) { |
natgovor | 4:c8b84240732a | 68 | redIsFlashing = !redIsFlashing; |
natgovor | 4:c8b84240732a | 69 | redPressEvent = 0; // Clear the event variable |
natgovor | 4:c8b84240732a | 70 | } |
natgovor | 4:c8b84240732a | 71 | if (bluePressEvent) { |
natgovor | 4:c8b84240732a | 72 | blueIsFlashing = !blueIsFlashing; |
natgovor | 4:c8b84240732a | 73 | bluePressEvent = 0; // Clear the event variable |
natgovor | 4:c8b84240732a | 74 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 75 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 76 | } |