Stage-1 Students SoCEM / Mbed 2 deprecated Task327

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 void sw1TimeOutHandler();
00004 void sw1RisingEdge();
00005 void sw1FallingEdge();
00006 
00007 
00008 #define EDGE_RISEN 1
00009 #define EDGE_FALLEN 0
00010 
00011 //Global Objects
00012 DigitalOut  red_led(D7);
00013 DigitalOut  green_led(D5);
00014 InterruptIn   sw1(D4);
00015 InterruptIn   sw2(D3);
00016 
00017 Timeout sw1TimeOut;             //Used to prevent switch bounce
00018 int sw1State = EDGE_FALLEN;     //Initial state for switch 1
00019 
00020 //Interrupt service routine for handling the timeout
00021 void sw1TimeOutHandler() {
00022     sw1TimeOut.detach();            //Stop the timeout counter firing
00023 
00024     //Which event does this follow?
00025     switch (sw1State) {
00026     case EDGE_RISEN:    
00027         sw1.fall(&sw1FallingEdge);  //Now wait for a falling edge
00028         break;
00029     case EDGE_FALLEN:
00030         sw1.rise(&sw1RisingEdge);   //Now wait for a rising edge
00031         break;
00032     } //end switch 
00033 }
00034 
00035 //Interrupt service routine for a rising edge (press)
00036 void sw1RisingEdge() {
00037     sw1.rise(NULL);             //Disable detecting more rising edges
00038     sw1State = EDGE_RISEN;      //Flag state
00039     sw1TimeOut.attach(&sw1TimeOutHandler, 0.2);    //Start timeout timer
00040 }
00041 
00042 //Interrupt service routive for SW1 falling edge (release)
00043 void sw1FallingEdge() {
00044     sw1.fall(NULL);                         //Disable this interrupt
00045     red_led = !red_led;                     //Toggle LED    
00046     sw1State = EDGE_FALLEN;                 //Flag state
00047     sw1TimeOut.attach(&sw1TimeOutHandler, 0.2);    //Start timeout counter    
00048 }
00049 
00050 //Main - only has to initialise and sleep
00051 int main() {
00052     //Initial logging message
00053     puts("START");
00054     
00055     //Initial state
00056     red_led    = 0; //Set RED LED to OFF
00057     green_led   = 1;
00058     
00059     //Configure interrupts, wait for first rising edge
00060     sw1.rise(&sw1RisingEdge);
00061     
00062     //Main Polling Loop
00063     while (true) {
00064         
00065         //Put CPU back to sleep
00066         sleep();
00067         
00068         //You can ONLY reach this point if an ISR wakes the CPU
00069         puts("ISR just woke the MPU");
00070         
00071     } //end while
00072 
00073 }