Final, Viva(ed).
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 #define BOTH_BLINK 0 00004 #define ONLY_RED_BLINK 1 00005 #define ONLY_BLUE_BLINK 2 00006 #define NO_BLINK 3 00007 00008 00009 00010 // Labs 2: Example program for using an interrupt (or callback) 00011 // ----------------------------------------------------------- 00012 // A callback function (corresponding to an ISR) is called when a button 00013 // is pressed 00014 // The callback uses a shared variable to signal another thread 00015 00016 InterruptIn button1(PTD0); 00017 InterruptIn button2(PTD2); 00018 DigitalOut led1(LED_RED); 00019 DigitalOut led2(LED_BLUE); 00020 Serial pc(USBTX, USBRX); // tx, rx 00021 00022 volatile int redEvent = 0, blueEvent = 0 ; 00023 00024 // This function is invoked when then interrupt occurs 00025 // Signal that the button has been pressed 00026 // Note: bounce may occur 00027 void button1Callback(){ 00028 redEvent = 1 ; 00029 } 00030 00031 void button2Callback(){ 00032 blueEvent = 1 ; 00033 } 00034 00035 /* ---- Main function (default thread) ---- 00036 Note that if this thread completes, nothing else works 00037 */ 00038 int main() { 00039 button1.mode(PullUp); // Ensure button i/p has pull up 00040 button1.fall(&button1Callback) ; // Attach function to falling edge 00041 button2.mode(PullUp); 00042 button2.fall(&button2Callback); 00043 //blinkStateLed1 = BLINK; 00044 //blinkStateLed2 = BLINK; 00045 led1.write(0); 00046 led2.write(0); 00047 int count = 0, systemState = BOTH_BLINK; 00048 00049 while(true) { 00050 // Toggle the LED every time the button is pressed 00051 00052 wait(0.1); 00053 count++; 00054 00055 switch(systemState) { 00056 case BOTH_BLINK: 00057 00058 if(redEvent) { // Red LED Stops to Blink 00059 systemState = ONLY_BLUE_BLINK; 00060 redEvent = 0; 00061 } 00062 if(blueEvent) { // Blue LED Stops to Blink 00063 systemState = ONLY_RED_BLINK; 00064 blueEvent = 0; 00065 } 00066 break; 00067 00068 case ONLY_BLUE_BLINK: 00069 if(redEvent) { // Red LED Starts blinking 00070 systemState = BOTH_BLINK; 00071 redEvent = 0; 00072 } 00073 if(blueEvent) { //Blue LED Stops to Blink 00074 systemState = NO_BLINK; 00075 blueEvent = 0; 00076 } 00077 00078 case ONLY_RED_BLINK: 00079 if(redEvent) { // Red LED Stops to Blink 00080 systemState = NO_BLINK; 00081 redEvent = 0; 00082 } 00083 if(blueEvent) { // Blue LED Starts blinking 00084 systemState = BOTH_BLINK; 00085 blueEvent = 0; 00086 } 00087 case NO_BLINK: 00088 if(redEvent) { 00089 systemState = ONLY_RED_BLINK; 00090 redEvent = 0; 00091 } 00092 if(blueEvent) { 00093 systemState = ONLY_BLUE_BLINK; 00094 blueEvent = 0; 00095 } 00096 00097 } 00098 00099 /* 00100 if (pressEvent1) { 00101 switch (blinkStateLed1) { 00102 case BLINK: 00103 blinkStateLed1 = NO_BLINK; 00104 break; 00105 case NO_BLINK: 00106 //led1.write(led2.read()); 00107 blinkStateLed1 = BLINK; 00108 break; 00109 } 00110 pressEvent1 = 0 ; // Clear the event variable 00111 } 00112 00113 00114 if (pressEvent2) { 00115 switch (blinkStateLed2) { 00116 case BLINK: 00117 blinkStateLed2 = NO_BLINK; 00118 break; 00119 case NO_BLINK: 00120 //led2.write(led1.read()); 00121 blinkStateLed2 = BLINK; 00122 break; 00123 } 00124 pressEvent2 = 0 ; // Clear the event variable 00125 } 00126 */ 00127 if(count >= 30) 00128 { 00129 count = 0; 00130 if (systemState == BOTH_BLINK) { 00131 led1 = !led1; 00132 led2 = !led2; 00133 } 00134 00135 else if (systemState == ONLY_RED_BLINK) { 00136 led1 = !led1; 00137 } 00138 else if (systemState == ONLY_BLUE_BLINK) { 00139 led2 = !led2; 00140 00141 } 00142 } 00143 pc.printf("%d", systemState); 00144 pc.putc('\r'); 00145 pc.putc('\n'); 00146 00147 00148 } 00149 }
Generated on Thu Sep 8 2022 20:08:58 by
1.7.2