Lab version
Fork of Task327 by
main.cpp
- Committer:
- noutram
- Date:
- 2017-10-11
- Revision:
- 2:23bd19542152
- Parent:
- 0:bf0e4c6e1f4d
- Child:
- 3:384a8f9726ac
File content as of revision 2:23bd19542152:
#include "mbed.h" void sw1TimeOutHandler(); void sw1RisingEdge(); void sw1FallingEdge(); #define EDGE_RISEN 1 #define EDGE_FALLEN 0 //Global Objects DigitalOut led(LED1); InterruptIn sw1(USER_BUTTON); Timeout sw1TimeOut; //Used to prevent switch bounce int sw1State = EDGE_FALLEN; //Initial state for switch 1 //Interrupt service routine for handling the timeout void sw1TimeOutHandler() { //Which event does this follow? switch (sw1State) { case EDGE_RISEN: sw1.fall(&sw1FallingEdge); //Now wait for a falling edge break; case EDGE_FALLEN: sw1.rise(&sw1RisingEdge); //Now wait for a rising edge break; } //end switch } //Interrupt service routine for a rising edge (press) void sw1RisingEdge() { sw1.rise(NULL); //Disable detecting more rising edges sw1State = EDGE_RISEN; //Flag state } //Interrupt service routive for SW1 falling edge (release) void sw1FallingEdge() { sw1.fall(NULL); //Disable this interrupt led = !led; //Toggle LED sw1State = EDGE_FALLEN; //Flag state } //Main - only has to initialise and sleep int main() { //Initial logging message puts("START"); //Configure interrupts, wait for first rising edge sw1.rise(&sw1RisingEdge); //Main Polling Loop while (true) { //Set LED to OFF led = 0; //Put CPU back to sleep sleep(); //woken by rising edge puts("Rising"); wait(0.2); sw1TimeOutHandler(); sleep(); //Falling edge puts("Falling"); wait(0.2); sw1TimeOutHandler(); //You can ONLY reach this point if an ISR wakes the CPU for (int n=0; n<10; n++) { led = !led; wait(0.1); } } //end while }