Basic starter project - no switch bounce - crude blocking
Revision 4:f30ca79f4676, committed 2017-10-24
- Comitter:
- noutram
- Date:
- Tue Oct 24 06:59:20 2017 +0000
- Parent:
- 3:a39db8aa11e8
- Child:
- 5:ac0eea152e50
- Commit message:
- Basic starter code for Task330
Changed in this revision
| SWPoll.hpp | Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/SWPoll.hpp Mon Oct 23 12:28:09 2017 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,71 +0,0 @@
-#include "mbed.h"
-
-class SWPoll {
-private:
- enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE};
- 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)
- {
- //Waiting for switch to rise:
- case LOW:
- if (sw == 1) {
- state = LOW_DEBOUNCE;
- t.reset();
- t.start();
- }
- break;
-
- case LOW_DEBOUNCE:
- if (t.read_ms() >= 200) {
- state = HIGH;
- t.stop();
- t.reset();
- }
- break;
-
- case HIGH:
- if (sw == 0) {
- led = !led; //Toggle output on state transition
- state = HIGH_DEBOUNCE;
- t.reset(); //(purely defensive)
- t.start();
- }
- break;
- case HIGH_DEBOUNCE:
- if (t.read_ms() >= 200) {
- state = LOW;
- t.stop();
- t.reset();
- }
- break;
- default:
- t.stop();
- t.reset();
- state = LOW;
- break;
- } //end switch
-
- //This is a Mealy Machine - so no output logic follows
- }
-};
\ No newline at end of file
--- a/main.cpp Mon Oct 23 12:28:09 2017 +0000
+++ b/main.cpp Tue Oct 24 06:59:20 2017 +0000
@@ -16,6 +16,9 @@
//The code below is hugely flawed and is only to
//illustrate the problem of blocking hardware
+
+// TASK - add some code to address the problem of switch bounce
+
int main() {
//Light up
@@ -28,19 +31,12 @@
while(1) {
while (sw1 == RELEASED) {};
- wait(0.2);
while (sw1 == PRESSED) {};
red_led = !red_led;
- wait(0.2);
while (sw2 == RELEASED) {};
- wait(0.2);
while (sw2 == PRESSED) {};
green_led = !green_led;
- wait(0.2);
-
- yellow_led = !yellow_led;
- wait(0.5);
//Flash the yellow
yellow_led = !yellow_led;