
Copy 1
Dependencies: mbed
Revision 0:bf95f897e13f, committed 2017-10-24
- Comitter:
- motley
- Date:
- Tue Oct 24 13:32:11 2017 +0000
- Commit message:
- First
Changed in this revision
diff -r 000000000000 -r bf95f897e13f SwitchManager.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SwitchManager.hpp Tue Oct 24 13:32:11 2017 +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
diff -r 000000000000 -r bf95f897e13f main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Oct 24 13:32:11 2017 +0000 @@ -0,0 +1,50 @@ +#include "mbed.h" +#include "SwitchManager.hpp" + +#define N 1000000 +#define RELEASED 0 +#define PRESSED 1 + +//Function prototypes +void isr1(); +void isr2(); +void isrTick(); + +//Hardware objects +DigitalOut red_led(PE_15); //CountUp is in its critical section +DigitalOut yellow_led(PB_10); //CountDown is in its critical section +DigitalOut green_led(PB_11); //counter != 0 +DigitalOut onboardLED(LED1); + +InterruptIn sw1(PE_12); +InterruptIn sw2(PE_14); +Ticker yellowTick; + +void toggleYellowISR() { + yellow_led = !yellow_led; +} + +int main() { + + //Light up + red_led = 1; + yellow_led = 1; + green_led = 1; + onboardLED = 0; + + //Interrupt controlled red led using sw1 + SwitchManager sm1(sw1, red_led); + + //Interrupt controlled red led using sw2 + SwitchManager sm2(sw2, green_led); + + //Simple ticker + yellowTick.attach(&toggleYellowISR, 0.5); + + //Now loop forever + while(1) { + sleep(); + }; +} + + \ No newline at end of file
diff -r 000000000000 -r bf95f897e13f mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Oct 24 13:32:11 2017 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/b484a57bc302 \ No newline at end of file