returns the state of the button, true if pressed, false if released. does debouncing, captures and maintains times, previous state, etc.
| 19 | // returns the state of the button, true if pressed, false if released. |
| 20 | // does debouncing, captures and maintains times, previous state, etc. |
| 21 | bool 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected