Code from N.Outram lecture 23 Oct 2017

Committer:
martinsimpson
Date:
Tue Oct 24 14:01:04 2017 +0000
Revision:
0:24610a6f4e59
edited from N.Outram code lecture 23 Oct 2017

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martinsimpson 0:24610a6f4e59 1 #include "mbed.h"
martinsimpson 0:24610a6f4e59 2
martinsimpson 0:24610a6f4e59 3 class SWPoll {
martinsimpson 0:24610a6f4e59 4 private:
martinsimpson 0:24610a6f4e59 5 enum State {LOW, LOW_DEBOUNCE, HIGH,HIGH_DEBOUNCE};
martinsimpson 0:24610a6f4e59 6 State state;
martinsimpson 0:24610a6f4e59 7 DigitalIn& sw;
martinsimpson 0:24610a6f4e59 8 DigitalOut& led;
martinsimpson 0:24610a6f4e59 9 Timer t;
martinsimpson 0:24610a6f4e59 10
martinsimpson 0:24610a6f4e59 11 public:
martinsimpson 0:24610a6f4e59 12 //Constructor
martinsimpson 0:24610a6f4e59 13 SWPoll(DigitalIn& gpioIn,DigitalOut& gpioOut) : sw(gpioIn),led(gpioOut)
martinsimpson 0:24610a6f4e59 14 {
martinsimpson 0:24610a6f4e59 15 state = LOW;
martinsimpson 0:24610a6f4e59 16 t.reset();
martinsimpson 0:24610a6f4e59 17 led = 0;
martinsimpson 0:24610a6f4e59 18 }
martinsimpson 0:24610a6f4e59 19 //Deconstructor
martinsimpson 0:24610a6f4e59 20 ~SWPoll(){
martinsimpson 0:24610a6f4e59 21 //Shut Down
martinsimpson 0:24610a6f4e59 22 t.stop();
martinsimpson 0:24610a6f4e59 23 t.reset();
martinsimpson 0:24610a6f4e59 24 led = 0;
martinsimpson 0:24610a6f4e59 25 }
martinsimpson 0:24610a6f4e59 26 // API - poll the switches
martinsimpson 0:24610a6f4e59 27 // Use of a Timer to manage switch bounce
martinsimpson 0:24610a6f4e59 28 void poll(){
martinsimpson 0:24610a6f4e59 29 switch (state)
martinsimpson 0:24610a6f4e59 30 {
martinsimpson 0:24610a6f4e59 31 // waiting for switch to rise
martinsimpson 0:24610a6f4e59 32 case LOW:
martinsimpson 0:24610a6f4e59 33 if (sw == 1){
martinsimpson 0:24610a6f4e59 34 state = LOW_DEBOUNCE;
martinsimpson 0:24610a6f4e59 35 t.reset();
martinsimpson 0:24610a6f4e59 36 t.start();
martinsimpson 0:24610a6f4e59 37 }
martinsimpson 0:24610a6f4e59 38 break;
martinsimpson 0:24610a6f4e59 39 case LOW_DEBOUNCE:
martinsimpson 0:24610a6f4e59 40 if (t.read_ms() >= 200){
martinsimpson 0:24610a6f4e59 41 state = HIGH;
martinsimpson 0:24610a6f4e59 42 t.stop();
martinsimpson 0:24610a6f4e59 43 t.reset();
martinsimpson 0:24610a6f4e59 44 }
martinsimpson 0:24610a6f4e59 45 break;
martinsimpson 0:24610a6f4e59 46 case HIGH:
martinsimpson 0:24610a6f4e59 47 if (sw == 0) {
martinsimpson 0:24610a6f4e59 48 led = !led;
martinsimpson 0:24610a6f4e59 49 state = HIGH_DEBOUNCE;
martinsimpson 0:24610a6f4e59 50 t.reset();
martinsimpson 0:24610a6f4e59 51 t.start();
martinsimpson 0:24610a6f4e59 52 }
martinsimpson 0:24610a6f4e59 53 break;
martinsimpson 0:24610a6f4e59 54 case HIGH_DEBOUNCE:
martinsimpson 0:24610a6f4e59 55 if (t.read_ms() >= 200) {
martinsimpson 0:24610a6f4e59 56 state = LOW;
martinsimpson 0:24610a6f4e59 57 t.stop();
martinsimpson 0:24610a6f4e59 58 t.reset();
martinsimpson 0:24610a6f4e59 59 }
martinsimpson 0:24610a6f4e59 60 break;
martinsimpson 0:24610a6f4e59 61 default:
martinsimpson 0:24610a6f4e59 62 t.stop();
martinsimpson 0:24610a6f4e59 63 t.reset();
martinsimpson 0:24610a6f4e59 64 state = LOW;
martinsimpson 0:24610a6f4e59 65 break;
martinsimpson 0:24610a6f4e59 66 } //end switch
martinsimpson 0:24610a6f4e59 67 //This is a Mealy Machine - so no output logic follows
martinsimpson 0:24610a6f4e59 68
martinsimpson 0:24610a6f4e59 69 }
martinsimpson 0:24610a6f4e59 70 };