Copy 1

Dependencies:   mbed

Committer:
motley
Date:
Tue Oct 24 13:32:11 2017 +0000
Revision:
0:bf95f897e13f
First

Who changed what in which revision?

UserRevisionLine numberNew contents of line
motley 0:bf95f897e13f 1 #include "mbed.h"
motley 0:bf95f897e13f 2
motley 0:bf95f897e13f 3 //This class manages an Interrupt in and LED output
motley 0:bf95f897e13f 4 //It automatically manages the switch-debounce using edge detection and timers
motley 0:bf95f897e13f 5 class SwitchManager {
motley 0:bf95f897e13f 6 private:
motley 0:bf95f897e13f 7 // enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE};
motley 0:bf95f897e13f 8 InterruptIn& switchInterrupt;
motley 0:bf95f897e13f 9 DigitalOut& led;
motley 0:bf95f897e13f 10 Timeout t;
motley 0:bf95f897e13f 11
motley 0:bf95f897e13f 12 void waitForRising() {
motley 0:bf95f897e13f 13 //Turn off interrupt
motley 0:bf95f897e13f 14 switchInterrupt.rise(NULL);
motley 0:bf95f897e13f 15 //Turn on timer
motley 0:bf95f897e13f 16 t.attach(callback(this, &SwitchManager::waitForStabilityRising), 0.2);
motley 0:bf95f897e13f 17 }
motley 0:bf95f897e13f 18
motley 0:bf95f897e13f 19 void waitForStabilityRising() {
motley 0:bf95f897e13f 20 //Look for falling edge
motley 0:bf95f897e13f 21 switchInterrupt.fall(callback(this, &SwitchManager::waitForFalling));
motley 0:bf95f897e13f 22 }
motley 0:bf95f897e13f 23
motley 0:bf95f897e13f 24 void waitForFalling() {
motley 0:bf95f897e13f 25 led = !led;
motley 0:bf95f897e13f 26 switchInterrupt.fall(NULL);
motley 0:bf95f897e13f 27 t.attach(callback(this, &SwitchManager::waitForStabilityFalling), 0.2);
motley 0:bf95f897e13f 28 }
motley 0:bf95f897e13f 29
motley 0:bf95f897e13f 30 void waitForStabilityFalling() {
motley 0:bf95f897e13f 31 //Look for rising edge
motley 0:bf95f897e13f 32 switchInterrupt.rise(callback(this, &SwitchManager::waitForRising));
motley 0:bf95f897e13f 33 }
motley 0:bf95f897e13f 34
motley 0:bf95f897e13f 35 public:
motley 0:bf95f897e13f 36 SwitchManager(InterruptIn& intIn, DigitalOut& gpio) : switchInterrupt(intIn), led(gpio) {
motley 0:bf95f897e13f 37 //Listen for rising edge
motley 0:bf95f897e13f 38 switchInterrupt.rise(callback(this, &SwitchManager::waitForRising));
motley 0:bf95f897e13f 39 }
motley 0:bf95f897e13f 40 ~SwitchManager() {
motley 0:bf95f897e13f 41 //Turn off LED and shut off any ISRs
motley 0:bf95f897e13f 42 led = 0;
motley 0:bf95f897e13f 43 switchInterrupt.rise(NULL);
motley 0:bf95f897e13f 44 switchInterrupt.fall(NULL);
motley 0:bf95f897e13f 45 t.detach();
motley 0:bf95f897e13f 46 }
motley 0:bf95f897e13f 47 };