Version 5

Dependencies:   BMP280 TextLCD BME280

Committer:
Alix955
Date:
Fri Dec 07 13:24:50 2018 +0000
Revision:
9:f5eae5211225
Version 5, mine & sams versions merged

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Alix955 9:f5eae5211225 1 #ifndef _SWITCHMANAGER_
Alix955 9:f5eae5211225 2 #define _SWITCHMANAGER_
Alix955 9:f5eae5211225 3
Alix955 9:f5eae5211225 4 #include "mbed.h"
Alix955 9:f5eae5211225 5 #include "sample_hardware.hpp"
Alix955 9:f5eae5211225 6 #include "LCD.hpp"
Alix955 9:f5eae5211225 7
Alix955 9:f5eae5211225 8 InterruptIn sw1(PE_12);
Alix955 9:f5eae5211225 9
Alix955 9:f5eae5211225 10 //This class manages an Interrupt attached to a button
Alix955 9:f5eae5211225 11 //It automatically manages the switch-debounce using edge detection and timers
Alix955 9:f5eae5211225 12
Alix955 9:f5eae5211225 13 class SwitchManager {
Alix955 9:f5eae5211225 14 private:
Alix955 9:f5eae5211225 15 // enum State {LOW, LOW_DEBOUNCE, HIGH, HIGH_DEBOUNCE};
Alix955 9:f5eae5211225 16 InterruptIn& switchInterrupt;
Alix955 9:f5eae5211225 17 Timeout t;
Alix955 9:f5eae5211225 18
Alix955 9:f5eae5211225 19 void waitForRising() {
Alix955 9:f5eae5211225 20 //Turn off interrupt
Alix955 9:f5eae5211225 21 switchInterrupt.rise(NULL);
Alix955 9:f5eae5211225 22 //Turn on timer
Alix955 9:f5eae5211225 23 t.attach(callback(this, &SwitchManager::waitForStabilityRising), 0.2);
Alix955 9:f5eae5211225 24 }
Alix955 9:f5eae5211225 25
Alix955 9:f5eae5211225 26 void waitForStabilityRising() {
Alix955 9:f5eae5211225 27 //Look for falling edge
Alix955 9:f5eae5211225 28 switchInterrupt.fall(callback(this, &SwitchManager::waitForFalling));
Alix955 9:f5eae5211225 29 }
Alix955 9:f5eae5211225 30
Alix955 9:f5eae5211225 31 void waitForFalling() {
Alix955 9:f5eae5211225 32 m_oDisplay.LCD_Queue.call(&m_oDisplay, &LCD_Data::update_time_date);
Alix955 9:f5eae5211225 33 switchInterrupt.fall(NULL);
Alix955 9:f5eae5211225 34 t.attach(callback(this, &SwitchManager::waitForStabilityFalling), 0.2);
Alix955 9:f5eae5211225 35 }
Alix955 9:f5eae5211225 36
Alix955 9:f5eae5211225 37 void waitForStabilityFalling() {
Alix955 9:f5eae5211225 38 //Look for rising edge
Alix955 9:f5eae5211225 39 switchInterrupt.rise(callback(this, &SwitchManager::waitForRising));
Alix955 9:f5eae5211225 40 }
Alix955 9:f5eae5211225 41
Alix955 9:f5eae5211225 42 public:
Alix955 9:f5eae5211225 43 SwitchManager(InterruptIn& intIn) : switchInterrupt(intIn) {
Alix955 9:f5eae5211225 44 //Listen for rising edge
Alix955 9:f5eae5211225 45 switchInterrupt.rise(callback(this, &SwitchManager::waitForRising));
Alix955 9:f5eae5211225 46 }
Alix955 9:f5eae5211225 47 ~SwitchManager() {
Alix955 9:f5eae5211225 48 //Turn off LED and shut off any ISRs
Alix955 9:f5eae5211225 49 switchInterrupt.rise(NULL);
Alix955 9:f5eae5211225 50 switchInterrupt.fall(NULL);
Alix955 9:f5eae5211225 51 t.detach();
Alix955 9:f5eae5211225 52 }
Alix955 9:f5eae5211225 53 };
Alix955 9:f5eae5211225 54
Alix955 9:f5eae5211225 55 SwitchManager sm1(sw1);
Alix955 9:f5eae5211225 56
Alix955 9:f5eae5211225 57 #endif