MCPcopy Create free account
hub / github.com/JChristensen/JC_Button / read

Method read

src/JC_Button.cpp:21–48  ·  view source on GitHub ↗

returns the state of the button, true if pressed, false if released. does debouncing, captures and maintains times, previous state, etc.

Source from the content-addressed store, hash-verified

19// returns the state of the button, true if pressed, false if released.
20// does debouncing, captures and maintains times, previous state, etc.
21bool Button::read()
22{
23 m_time = millis();
24 bool pinVal = static_cast<bool>(digitalRead(m_pin)) ^ m_invert;
25
26 switch (m_fsm) {
27 case STABLE:
28 if (pinVal != m_state) { // maybe a change, but debounce first
29 m_dbStart = m_time;
30 m_fsm = DEBOUNCE;
31 }
32 m_changed = false;
33 break;
34
35 case DEBOUNCE:
36 if (m_time - m_dbStart >= m_dbTime) {
37 m_fsm = STABLE;
38 if (pinVal != m_state) { // a real change (else just noise)
39 m_lastState = m_state;
40 m_state = pinVal;
41 m_lastChange = m_time;
42 m_changed = true;
43 }
44 }
45 break;
46 }
47 return m_state;
48}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected