Repo. for the ELEC351 Coursework - Oliver Thompson
Dependencies: BMP280 ELEC350-Practicals-FZ429- TextLCD watchdog_RTOS BME280 ntp-client
SwitchManager.hpp@13:37a7c57f4641, 2018-12-05 (annotated)
- Committer:
- O_Thom
- Date:
- Wed Dec 05 19:53:40 2018 +0000
- Revision:
- 13:37a7c57f4641
Minor changes to Serial Comms header. Testing still needed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
O_Thom | 13:37a7c57f4641 | 1 | #include "mbed.h" |
O_Thom | 13:37a7c57f4641 | 2 | |
O_Thom | 13:37a7c57f4641 | 3 | //This class manages an Interrupt in and LED output |
O_Thom | 13:37a7c57f4641 | 4 | //It automatically manages the switch-debounce using edge detection and timers |
O_Thom | 13:37a7c57f4641 | 5 | class SwitchManager { |
O_Thom | 13:37a7c57f4641 | 6 | private: |
O_Thom | 13:37a7c57f4641 | 7 | // enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE}; |
O_Thom | 13:37a7c57f4641 | 8 | InterruptIn& switchInterrupt; |
O_Thom | 13:37a7c57f4641 | 9 | DigitalOut& led; |
O_Thom | 13:37a7c57f4641 | 10 | Timeout t; |
O_Thom | 13:37a7c57f4641 | 11 | |
O_Thom | 13:37a7c57f4641 | 12 | void waitForRising() { |
O_Thom | 13:37a7c57f4641 | 13 | //Turn off interrupt |
O_Thom | 13:37a7c57f4641 | 14 | switchInterrupt.rise(NULL); |
O_Thom | 13:37a7c57f4641 | 15 | //Turn on timer |
O_Thom | 13:37a7c57f4641 | 16 | t.attach(callback(this, &SwitchManager::waitForStabilityRising), 0.2); |
O_Thom | 13:37a7c57f4641 | 17 | } |
O_Thom | 13:37a7c57f4641 | 18 | |
O_Thom | 13:37a7c57f4641 | 19 | void waitForStabilityRising() { |
O_Thom | 13:37a7c57f4641 | 20 | //Look for falling edge |
O_Thom | 13:37a7c57f4641 | 21 | switchInterrupt.fall(callback(this, &SwitchManager::waitForFalling)); |
O_Thom | 13:37a7c57f4641 | 22 | } |
O_Thom | 13:37a7c57f4641 | 23 | |
O_Thom | 13:37a7c57f4641 | 24 | void waitForFalling() { |
O_Thom | 13:37a7c57f4641 | 25 | led = !led; |
O_Thom | 13:37a7c57f4641 | 26 | switchInterrupt.fall(NULL); |
O_Thom | 13:37a7c57f4641 | 27 | t.attach(callback(this, &SwitchManager::waitForStabilityFalling), 0.2); |
O_Thom | 13:37a7c57f4641 | 28 | } |
O_Thom | 13:37a7c57f4641 | 29 | |
O_Thom | 13:37a7c57f4641 | 30 | void waitForStabilityFalling() { |
O_Thom | 13:37a7c57f4641 | 31 | //Look for rising edge |
O_Thom | 13:37a7c57f4641 | 32 | switchInterrupt.rise(callback(this, &SwitchManager::waitForRising)); |
O_Thom | 13:37a7c57f4641 | 33 | } |
O_Thom | 13:37a7c57f4641 | 34 | |
O_Thom | 13:37a7c57f4641 | 35 | public: |
O_Thom | 13:37a7c57f4641 | 36 | SwitchManager(InterruptIn& intIn, DigitalOut& gpio) : switchInterrupt(intIn), led(gpio) { |
O_Thom | 13:37a7c57f4641 | 37 | //Listen for rising edge |
O_Thom | 13:37a7c57f4641 | 38 | switchInterrupt.rise(callback(this, &SwitchManager::waitForRising)); |
O_Thom | 13:37a7c57f4641 | 39 | } |
O_Thom | 13:37a7c57f4641 | 40 | ~SwitchManager() { |
O_Thom | 13:37a7c57f4641 | 41 | //Turn off LED and shut off any ISRs |
O_Thom | 13:37a7c57f4641 | 42 | led = 0; |
O_Thom | 13:37a7c57f4641 | 43 | switchInterrupt.rise(NULL); |
O_Thom | 13:37a7c57f4641 | 44 | switchInterrupt.fall(NULL); |
O_Thom | 13:37a7c57f4641 | 45 | t.detach(); |
O_Thom | 13:37a7c57f4641 | 46 | } |
O_Thom | 13:37a7c57f4641 | 47 | }; |