From fb3743eb8d138d2a069092ca2cb4ced1d7892cc2 Mon Sep 17 00:00:00 2001 From: willem Date: Sun, 2 Jan 2022 18:46:08 +0100 Subject: [PATCH] add interface --- src/JC_Button.cpp | 14 +++++++++++--- src/JC_Button.h | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/JC_Button.cpp b/src/JC_Button.cpp index aaa50de..746aaf9 100644 --- a/src/JC_Button.cpp +++ b/src/JC_Button.cpp @@ -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. * diff --git a/src/JC_Button.h b/src/JC_Button.h index 7170294..57ebbbc 100644 --- a/src/JC_Button.h +++ b/src/JC_Button.h @@ -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);