lab 2 interrupts

main.cpp

Committer:
WilliamMarshQMUL
Date:
2017-01-24
Revision:
2:071f22412cdc
Parent:
1:13e0c1956b54
Child:
3:05b6a1431a6b

File content as of revision 2:071f22412cdc:

#include "mbed.h"
#include "rtos.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_RED);

Thread flashT ;
volatile int pressEvent = 0 ;

// Signal when the button is 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() {
    while(true) {
        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 
}