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.
Fork of Task330_polling by
Diff: SWPoll.hpp
- 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: