Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
00001 #include "mbed.h" 00002 00003 // There is a LOT of code repetition here. 00004 // Following bad practise, you can almost copy and paste the code for the red LED to add the greedn 00005 // Further task: Consider ways to reduce this to make it simpler to maintain. 00006 00007 void sw1TimeOutHandler(); 00008 void sw1RisingEdge(); 00009 void sw1FallingEdge(); 00010 void sw2TimeOutHandler(); 00011 void sw2RisingEdge(); 00012 void sw2FallingEdge(); 00013 00014 #define EDGE_RISEN 1 00015 #define EDGE_FALLEN 0 00016 00017 //Global Objects 00018 DigitalOut red_led(D7); 00019 DigitalOut green_led(D5); 00020 InterruptIn sw1(D4); 00021 InterruptIn sw2(D3); 00022 00023 Timeout sw1TimeOut; //Used to prevent switch bounce 00024 Timeout sw2TimeOut; 00025 int sw1State = EDGE_FALLEN; //Initial state for switch 1 00026 int sw2State = EDGE_FALLEN; 00027 00028 //Interrupt service routine for handling the timeout 00029 void sw1TimeOutHandler() { 00030 sw1TimeOut.detach(); //Stop the timeout counter firing 00031 00032 //Which event does this follow? 00033 switch (sw1State) { 00034 case EDGE_RISEN: 00035 sw1.fall(&sw1FallingEdge); //Now wait for a falling edge 00036 break; 00037 case EDGE_FALLEN: 00038 sw1.rise(&sw1RisingEdge); //Now wait for a rising edge 00039 break; 00040 } //end switch 00041 } 00042 00043 //Interrupt service routine for a rising edge (press) 00044 void sw1RisingEdge() { 00045 sw1.rise(NULL); //Disable detecting more rising edges 00046 sw1State = EDGE_RISEN; //Flag state 00047 sw1TimeOut.attach(&sw1TimeOutHandler, 0.2); //Start timeout timer 00048 } 00049 00050 //Interrupt service routive for SW1 falling edge (release) 00051 void sw1FallingEdge() { 00052 sw1.fall(NULL); //Disable this interrupt 00053 red_led = !red_led; //Toggle LED 00054 sw1State = EDGE_FALLEN; //Flag state 00055 sw1TimeOut.attach(&sw1TimeOutHandler, 0.2); //Start timeout counter 00056 } 00057 00058 void sw2TimeOutHandler() { 00059 sw2TimeOut.detach(); //Stop the timeout counter firing 00060 00061 //Which event does this follow? 00062 switch (sw2State) { 00063 case EDGE_RISEN: 00064 sw2.fall(&sw2FallingEdge); //Now wait for a falling edge 00065 break; 00066 case EDGE_FALLEN: 00067 sw2.rise(&sw2RisingEdge); //Now wait for a rising edge 00068 break; 00069 } //end switch 00070 } 00071 00072 //Interrupt service routine for a rising edge (press) 00073 void sw2RisingEdge() { 00074 sw2.rise(NULL); //Disable detecting more rising edges 00075 sw2State = EDGE_RISEN; //Flag state 00076 sw2TimeOut.attach(&sw2TimeOutHandler, 0.2); //Start timeout timer 00077 } 00078 00079 //Interrupt service routive for SW1 falling edge (release) 00080 void sw2FallingEdge() { 00081 sw2.fall(NULL); //Disable this interrupt 00082 green_led = !green_led; //Toggle LED 00083 sw2State = EDGE_FALLEN; //Flag state 00084 sw2TimeOut.attach(&sw2TimeOutHandler, 0.2); //Start timeout counter 00085 } 00086 00087 //Main - only has to initialise and sleep 00088 int main() { 00089 //Initial logging message 00090 puts("START"); 00091 00092 //Initial state 00093 red_led = 0; //Set RED LED to OFF 00094 green_led = 0; 00095 00096 //Configure interrupts, wait for first rising edge 00097 sw1.rise(&sw1RisingEdge); 00098 sw2.rise(&sw2RisingEdge); 00099 00100 //Main Polling Loop 00101 while (true) { 00102 00103 //Put CPU back to sleep 00104 sleep(); 00105 00106 //You can ONLY reach this point if an ISR wakes the CPU 00107 puts("ISR just woke the MPU"); 00108 00109 } //end while 00110 00111 }
Generated on Tue Jul 26 2022 15:00:24 by
1.7.2