add interface

This commit is contained in:
2022-01-02 18:46:08 +01:00
parent 91ddc75ed7
commit fb3743eb8d
2 changed files with 13 additions and 3 deletions

View File

@@ -12,7 +12,8 @@ void Button::begin()
{
pinMode(m_pin, m_puEnable ? INPUT_PULLUP : INPUT);
m_state = digitalRead(m_pin);
if (m_invert) m_state = !m_state;
if (m_invert)
m_state = !m_state;
m_time = millis();
m_lastState = m_state;
m_changed = false;
@@ -27,7 +28,8 @@ bool Button::read()
{
uint32_t ms = millis();
bool pinVal = digitalRead(m_pin);
if (m_invert) pinVal = !pinVal;
if (m_invert)
pinVal = !pinVal;
if (ms - m_lastChange < m_dbTime)
{
m_changed = false;
@@ -37,7 +39,8 @@ bool Button::read()
m_lastState = m_state;
m_state = pinVal;
m_changed = (m_state != m_lastState);
if (m_changed) m_lastChange = ms;
if (m_changed)
m_lastChange = ms;
}
m_time = ms;
return m_state;
@@ -90,6 +93,11 @@ bool Button::releasedFor(uint32_t ms)
return !m_state && m_time - m_lastChange >= ms;
}
uint32_t Button::getPressedFor(void)
{
return (m_state ? m_time - m_lastChange : 0);
}
/*----------------------------------------------------------------------*
* lastChange() returns the time the button last changed state, *
* in milliseconds. *

View File

@@ -51,6 +51,8 @@ class Button
// and has been in that state for at least the given number of milliseconds.
bool pressedFor(uint32_t ms);
uint32_t getPressedFor( void);
// Returns true if the button state at the last call to read() was released,
// and has been in that state for at least the given number of milliseconds.
bool releasedFor(uint32_t ms);