Lab version

Fork of Task327 by Nicholas Outram

Revision:
3:384a8f9726ac
Parent:
2:23bd19542152
Child:
4:c85556b07606
--- a/main.cpp	Wed Oct 11 16:52:31 2017 +0000
+++ b/main.cpp	Thu Oct 12 14:35:26 2017 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 
-void sw1TimeOutHandler();
+void updateState();
 void sw1RisingEdge();
 void sw1FallingEdge();
 
@@ -8,13 +8,18 @@
 #define EDGE_FALLEN 0
 
 //Global Objects
-DigitalOut  led(LED1);
+DigitalOut    led(LED1);
 InterruptIn   sw1(USER_BUTTON);
-Timeout sw1TimeOut;             //Used to prevent switch bounce
-int sw1State = EDGE_FALLEN;     //Initial state for switch 1
+
+//State
+static int sw1State = EDGE_FALLEN;     //Initial state for switch 1
 
 //Interrupt service routine for handling the timeout
-void sw1TimeOutHandler() {
+void updateState() {
+    
+    //Allow for switch bounce
+    wait(0.1); 
+    
     //Which event does this follow?
     switch (sw1State) {
     case EDGE_RISEN:    
@@ -34,8 +39,7 @@
 
 //Interrupt service routive for SW1 falling edge (release)
 void sw1FallingEdge() {
-    sw1.fall(NULL);                         //Disable this interrupt
-    led = !led;                             //Toggle LED    
+    sw1.fall(NULL);                         //Disable this interrupt   
     sw1State = EDGE_FALLEN;                 //Flag state
 }
 
@@ -53,16 +57,15 @@
         led    = 0; 
         
         //Put CPU back to sleep
-        sleep(); //woken by rising edge
-        puts("Rising");
-        wait(0.2);
-        sw1TimeOutHandler();
+        sleep();                //woken by rising edge
+        puts("Rising");                
+        updateState();
+        
         sleep(); //Falling edge
         puts("Falling");
-        wait(0.2);
-        sw1TimeOutHandler();
+        updateState();
         
-        //You can ONLY reach this point if an ISR wakes the CPU
+        //You can ONLY reach this point if an ISR wakes the CPU twice
         for (int n=0; n<10; n++) {
             led = !led;
             wait(0.1);