![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Repo. for the ELEC351 Coursework - Oliver Thompson
Dependencies: BMP280 ELEC350-Practicals-FZ429- TextLCD watchdog_RTOS BME280 ntp-client
Diff: SwitchManager.hpp
- Revision:
- 13:37a7c57f4641
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SwitchManager.hpp Wed Dec 05 19:53:40 2018 +0000 @@ -0,0 +1,47 @@ +#include "mbed.h" + +//This class manages an Interrupt in and LED output +//It automatically manages the switch-debounce using edge detection and timers +class SwitchManager { +private: +// enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE}; + InterruptIn& switchInterrupt; + DigitalOut& led; + Timeout t; + + void waitForRising() { + //Turn off interrupt + switchInterrupt.rise(NULL); + //Turn on timer + t.attach(callback(this, &SwitchManager::waitForStabilityRising), 0.2); + } + + void waitForStabilityRising() { + //Look for falling edge + switchInterrupt.fall(callback(this, &SwitchManager::waitForFalling)); + } + + void waitForFalling() { + led = !led; + switchInterrupt.fall(NULL); + t.attach(callback(this, &SwitchManager::waitForStabilityFalling), 0.2); + } + + void waitForStabilityFalling() { + //Look for rising edge + switchInterrupt.rise(callback(this, &SwitchManager::waitForRising)); + } + +public: + SwitchManager(InterruptIn& intIn, DigitalOut& gpio) : switchInterrupt(intIn), led(gpio) { + //Listen for rising edge + switchInterrupt.rise(callback(this, &SwitchManager::waitForRising)); + } + ~SwitchManager() { + //Turn off LED and shut off any ISRs + led = 0; + switchInterrupt.rise(NULL); + switchInterrupt.fall(NULL); + t.detach(); + } +}; \ No newline at end of file