Copy 1
Dependencies: mbed
SwitchManager.hpp
00001 #include "mbed.h" 00002 00003 //This class manages an Interrupt in and LED output 00004 //It automatically manages the switch-debounce using edge detection and timers 00005 class SwitchManager { 00006 private: 00007 // enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE}; 00008 InterruptIn& switchInterrupt; 00009 DigitalOut& led; 00010 Timeout t; 00011 00012 void waitForRising() { 00013 //Turn off interrupt 00014 switchInterrupt.rise(NULL); 00015 //Turn on timer 00016 t.attach(callback(this, &SwitchManager::waitForStabilityRising), 0.2); 00017 } 00018 00019 void waitForStabilityRising() { 00020 //Look for falling edge 00021 switchInterrupt.fall(callback(this, &SwitchManager::waitForFalling)); 00022 } 00023 00024 void waitForFalling() { 00025 led = !led; 00026 switchInterrupt.fall(NULL); 00027 t.attach(callback(this, &SwitchManager::waitForStabilityFalling), 0.2); 00028 } 00029 00030 void waitForStabilityFalling() { 00031 //Look for rising edge 00032 switchInterrupt.rise(callback(this, &SwitchManager::waitForRising)); 00033 } 00034 00035 public: 00036 SwitchManager(InterruptIn& intIn, DigitalOut& gpio) : switchInterrupt(intIn), led(gpio) { 00037 //Listen for rising edge 00038 switchInterrupt.rise(callback(this, &SwitchManager::waitForRising)); 00039 } 00040 ~SwitchManager() { 00041 //Turn off LED and shut off any ISRs 00042 led = 0; 00043 switchInterrupt.rise(NULL); 00044 switchInterrupt.fall(NULL); 00045 t.detach(); 00046 } 00047 };
Generated on Sat Aug 6 2022 10:24:05 by 1.7.2