University of Plymouth - Stages 1, 2 and 3 / Mbed OS Task330_polling

Fork of Task330_polling by University of Plymouth - Stages 1, 2 and 3

Revision:
1:e84a51c98d75
Parent:
0:397b84c74d17
--- a/SWPoll.hpp	Mon Oct 23 09:37:46 2017 +0000
+++ b/SWPoll.hpp	Mon Oct 23 09:47:16 2017 +0000
@@ -3,23 +3,27 @@
 class SWPoll {
 private:
     enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE};
-    State state;
-    DigitalIn& sw;
-    DigitalOut& led;
-    Timer t;    
+    State state;        // Internal state
+    DigitalIn& sw;      // These are references (aliases) and MUST be initialised
+    DigitalOut& led;    // ""
+    Timer t;            // Each instance has it's own timer, so this is is finite
     
 public:
+    //Constructor - MUST be given two parameters (for the switch and led) BY REFERENCE
     SWPoll(DigitalIn& gpioIn, DigitalOut& gpioOut) : sw(gpioIn), led(gpioOut) {
         state = LOW;
         t.reset();
         led = 0;
     }    
+    //Destructor - should the instance go out of scope, this is called
     ~SWPoll() {
         //Shut down
         t.stop();
         t.reset();
         led = 0;   
     }
+    //The public API - poll the switches
+    //Bascially, a mealy machine - uses a timer to manage switch bounce
     void poll() {
         switch (state) 
         {
@@ -42,8 +46,9 @@
         
         case HIGH:
             if (sw == 0) {
+                led = !led; //Toggle output on state transition  
                 state = HIGH_DEBOUNCE;
-                t.reset();
+                t.reset(); //(purely defensive)
                 t.start();
             }
             break;
@@ -52,7 +57,6 @@
                 state = LOW;
                 t.stop();
                 t.reset();  
-                led = !led; //Toggle output on state transition  
             }
             break;            
          default: