-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
47 lines (39 loc) · 974 Bytes
/
Button.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <Arduino.h>
#include "Button.h"
Button::Button(uint8_t pin,
bool activeLow,
bool pullUp,
uint32_t debounceDelay)
: m_active(false)
, m_lastStateChange(0)
, m_lastActiveDuration(0)
, m_debounceDelay(debounceDelay)
, m_pin(pin)
, m_activeLow(activeLow)
, m_pullUp(pullUp)
, m_next(0)
{
if (pullUp) pinMode(pin, INPUT_PULLUP);
else pinMode(pin, INPUT);
}
bool Button::poll() {
if ((millis() - m_lastStateChange > m_debounceDelay) ||
(m_lastStateChange == 0))
{
bool state = getPhysicalState();
if (state != m_active)
{
if (!state) m_lastActiveDuration = millis() - m_lastStateChange;
m_lastStateChange = millis();
m_active = state;
return true;
}
}
return false;
}
uint8_t Button::getPhysicalState() const {
if (m_activeLow)
return !digitalRead(m_pin);
else
return digitalRead(m_pin);
}