Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Task330_ISR by
SwitchManager.hpp@0:2a35dbda8863, 2017-10-23 (annotated)
- Committer:
- noutram
- Date:
- Mon Oct 23 13:41:17 2017 +0000
- Revision:
- 0:2a35dbda8863
Solution using ISR and Timeout
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:2a35dbda8863 | 1 | #include "mbed.h" |
| noutram | 0:2a35dbda8863 | 2 | |
| noutram | 0:2a35dbda8863 | 3 | //This class manages an Interrupt in and LED output |
| noutram | 0:2a35dbda8863 | 4 | //It automatically manages the switch-debounce using edge detection and timers |
| noutram | 0:2a35dbda8863 | 5 | class SwitchManager { |
| noutram | 0:2a35dbda8863 | 6 | private: |
| noutram | 0:2a35dbda8863 | 7 | // enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE}; |
| noutram | 0:2a35dbda8863 | 8 | InterruptIn& switchInterrupt; |
| noutram | 0:2a35dbda8863 | 9 | DigitalOut& led; |
| noutram | 0:2a35dbda8863 | 10 | Timeout t; |
| noutram | 0:2a35dbda8863 | 11 | |
| noutram | 0:2a35dbda8863 | 12 | void waitForRising() { |
| noutram | 0:2a35dbda8863 | 13 | //Turn off interrupt |
| noutram | 0:2a35dbda8863 | 14 | switchInterrupt.rise(NULL); |
| noutram | 0:2a35dbda8863 | 15 | //Turn on timer |
| noutram | 0:2a35dbda8863 | 16 | t.attach(callback(this, &SwitchManager::waitForStabilityRising), 0.2); |
| noutram | 0:2a35dbda8863 | 17 | } |
| noutram | 0:2a35dbda8863 | 18 | |
| noutram | 0:2a35dbda8863 | 19 | void waitForStabilityRising() { |
| noutram | 0:2a35dbda8863 | 20 | //Look for falling edge |
| noutram | 0:2a35dbda8863 | 21 | switchInterrupt.fall(callback(this, &SwitchManager::waitForFalling)); |
| noutram | 0:2a35dbda8863 | 22 | } |
| noutram | 0:2a35dbda8863 | 23 | |
| noutram | 0:2a35dbda8863 | 24 | void waitForFalling() { |
| noutram | 0:2a35dbda8863 | 25 | led = !led; |
| noutram | 0:2a35dbda8863 | 26 | switchInterrupt.fall(NULL); |
| noutram | 0:2a35dbda8863 | 27 | t.attach(callback(this, &SwitchManager::waitForStabilityFalling), 0.2); |
| noutram | 0:2a35dbda8863 | 28 | } |
| noutram | 0:2a35dbda8863 | 29 | |
| noutram | 0:2a35dbda8863 | 30 | void waitForStabilityFalling() { |
| noutram | 0:2a35dbda8863 | 31 | //Look for rising edge |
| noutram | 0:2a35dbda8863 | 32 | switchInterrupt.rise(callback(this, &SwitchManager::waitForRising)); |
| noutram | 0:2a35dbda8863 | 33 | } |
| noutram | 0:2a35dbda8863 | 34 | |
| noutram | 0:2a35dbda8863 | 35 | public: |
| noutram | 0:2a35dbda8863 | 36 | SwitchManager(InterruptIn& intIn, DigitalOut& gpio) : switchInterrupt(intIn), led(gpio) { |
| noutram | 0:2a35dbda8863 | 37 | //Listen for rising edge |
| noutram | 0:2a35dbda8863 | 38 | switchInterrupt.rise(callback(this, &SwitchManager::waitForRising)); |
| noutram | 0:2a35dbda8863 | 39 | } |
| noutram | 0:2a35dbda8863 | 40 | ~SwitchManager() { |
| noutram | 0:2a35dbda8863 | 41 | //Turn off LED and shut off any ISRs |
| noutram | 0:2a35dbda8863 | 42 | led = 0; |
| noutram | 0:2a35dbda8863 | 43 | switchInterrupt.rise(NULL); |
| noutram | 0:2a35dbda8863 | 44 | switchInterrupt.fall(NULL); |
| noutram | 0:2a35dbda8863 | 45 | t.detach(); |
| noutram | 0:2a35dbda8863 | 46 | } |
| noutram | 0:2a35dbda8863 | 47 | }; |
