Lab 2 sample 2: use of interrupts

Dependents:   digitalInInterrupt_sample

Fork of digitalInPolling_sample by William Marsh

main.cpp

Committer:
WilliamMarshQMUL
Date:
2019-01-15
Revision:
4:728667196916
Parent:
3:05b6a1431a6b
Child:
5:86742cfaf4e4

File content as of revision 4:728667196916:

#include "mbed.h"

// Labs 2: Example program for using an interrupt (or callback)
// -----------------------------------------------------------
// 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);

volatile int pressEvent = 0 ;

// This function is invoked when then interrupt occurs
//   Signal that the button has been pressed
//   Note: bounce may occur 
void buttonCallback(){
    pressEvent = 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

    while(true) {
        // Toggle the LED every time the button is pressed
        if (pressEvent) {
            led = !led ;
            pressEvent = 0 ; // Clear the event variable
        }
        wait(0.1) ;
    }
}