Charles Tritt / Mbed 2 deprecated TrafficEx

Dependencies:   mbed

Fork of TimeoutEx by Charles Tritt

Revision:
1:2438293c128c
Parent:
0:3151531e9a31
Child:
2:2c03f9b6d286
--- a/main.cpp	Fri Oct 06 21:08:45 2017 +0000
+++ b/main.cpp	Mon Oct 09 02:34:12 2017 +0000
@@ -1,33 +1,51 @@
 /*
-    Project: TW_Ex_9_1
+    Project: TimeOutEX
     File: main.cpp
     
-    An example similar to T&W example 9.1. Green junction will flash 
-    continuously. Blue junction will toggle in response to depressing the user 
-    button.
+    Button interrupt sets global config flag. Timeout clears it.Main program 
+    flashes red or green LED based on config status.
     
     Created by Dr. C. S. Tritt
-    Last revised: 10/6/17 (v. 1.0)
+    Last revised: 10/8/17 (v. 1.0)
 */
 #include "mbed.h"
 
+void buttonISR(); // Button ISR declaration.
+void configOff(); // Configuration mode off declaration.
+ 
 InterruptIn myButton(USER_BUTTON); // Button is normally high. Goes low w/press.
+Timeout configTime; // TimeOut constructor takes no arguments.
 
-DigitalOut bluLED(D4); // Bluee and green LED junctions.
+DigitalOut redLED(D2); // Red and green LED junctions.
 DigitalOut grnLED(D3);
 
-void myISR() { // Simple ISR toggles the red LED junction when called.
-    bluLED = !bluLED; // Toggle blue junction.
-}
+bool config = false; // Configuration mode flag.
 
 int main() {
-    bluLED = 0; // Turn blue & green off at start.
+    redLED = 0; // Turn red & green off at start.
     grnLED = 0; 
     
-    myButton.fall(&myISR); // "Register" the ISR routine. Sets vector.
-    
-    while(true) {
-        grnLED = !grnLED; // Toggle green junction.
+    myButton.fall(&buttonISR); // Register ISR routine.
+ 
+    while(true) { // Main loop.
+        if (config) {
+            redLED = !redLED; // Toggle red junction.
+        } else {
+            grnLED = !grnLED; // Toggle green junction.
+        }
         wait(0.5); // Pause half a second.
     }
+}
+
+void buttonISR() { // Sets config status when button falls.
+    config = true; // Set config status.
+    grnLED = 0; // Force green junction off.
+    redLED = 1; // Turn red junction on.
+    configTime.attach(&configOff, 5.0f); // Register TimeOut callback.
+}
+
+void configOff() { // Clears config flag after timeout.
+    config = false; // Clear config flag.
+    redLED = 0; // Force red junction off.
+    grnLED = 1; // Turn green junction on.
 }
\ No newline at end of file