semf
debounceddigitalinpolling.cpp
Go to the documentation of this file.
1
12
13namespace semf
14{
15DebouncedDigitalInPolling::DebouncedDigitalInPolling(Gpio& gpio, uint32_t debounceLowTime, uint32_t debounceHighTime, bool inverted)
16: DigitalInPolling(gpio, inverted),
17 m_debounceLowTime(debounceLowTime),
18 m_debounceHighTime(debounceHighTime)
19{
20}
21
22DebouncedDigitalInPolling::DebouncedDigitalInPolling(Gpio& gpio, app::TimeBase& timeBase, uint32_t debounceLowTime, uint32_t debounceHighTime, bool inverted)
23: DigitalInPolling(gpio, timeBase, inverted),
24 m_debounceLowTime(debounceLowTime),
25 m_debounceHighTime(debounceHighTime)
26{
27}
28
30{
31 SEMF_INFO("set high time to %u", time);
32 m_debounceHighTime = time;
33}
34
36{
37 SEMF_INFO("set low time to %u", time);
38 m_debounceLowTime = time;
39}
40
42{
43 app::DigitalIn::State oldState = state();
45 if (oldState == app::DigitalIn::State::Low && newState == app::DigitalIn::State::High)
46 {
47 m_lowTime = 0;
48 ++m_highTime;
49 if (m_highTime >= m_debounceHighTime)
50 {
51 setState(app::DigitalIn::State::High);
52 SEMF_INFO("changed to high");
54 }
55 }
56 else if (oldState == app::DigitalIn::State::High && newState == app::DigitalIn::State::Low)
57 {
58 m_highTime = 0;
59 ++m_lowTime;
60 if (m_lowTime >= m_debounceLowTime)
61 {
62 setState(app::DigitalIn::State::Low);
63 SEMF_INFO("changed to low");
65 }
66 }
67 else
68 {
69 m_lowTime = 0;
70 m_highTime = 0;
71 }
72}
73} /* namespace semf */
void tick() override
Checks the present pin state and counts the debounce time.
void setDebounceHighTime(uint32_t time)
Sets the debounce time for switching from low to high.
void setDebounceLowTime(uint32_t time)
Sets the debounce time for switching from high to low.
DebouncedDigitalInPolling(Gpio &gpio, uint32_t debounceLowTime, uint32_t debounceHighTime, bool inverted=false)
Constructor.
State state() const override
Returns the level status of the hardware pin.
Definition: digitalin.cpp:32
Class for reading a digital input in polling mode.
State state() const override
Returns the level status of the hardware pin.
void setState(State state)
Sets the hardware pin status internally.
Interface class for using a GPIO pin of the microcontroller.
Definition: gpio.h:23
A TimeBase is the bridge between e.g. a hardware timer (interrupt service routine) and TickReceiver o...
Definition: timebase.h:36
#define SEMF_INFO(...)
Definition: debug.h:41