From 445b696f43a9106f8871a67c077e990ce1ca14ab Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 11 Jan 2025 14:59:22 +0100 Subject: Age out bpm data --- UI.h | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'UI.h') diff --git a/UI.h b/UI.h index fa89c52..bbc5a7d 100644 --- a/UI.h +++ b/UI.h @@ -26,6 +26,46 @@ private: std::chrono::time_point m_checkpoint_timestamp{}; }; +template +class AgeOutValue +{ +public: + AgeOutValue(std::chrono::milliseconds timeout): + m_timeout(timeout), + m_checkpoint_timestamp(clock_type::now()), + m_value{} + { + } + + bool is_current() + { + return clock_type::now() - m_checkpoint_timestamp < m_timeout; + } + + // returns value, or 0 if aged-out + T value() + { + T result{}; + + if (is_current()) { + result = m_value; + } + + return result; + } + + void update(const T& value) + { + m_value = value; + m_checkpoint_timestamp = clock_type::now(); + } + +private: + const std::chrono::milliseconds m_timeout; + std::chrono::time_point m_checkpoint_timestamp{}; + T m_value; +}; + class UI { public: @@ -66,8 +106,8 @@ private: uint64_t m_midi_timestamp; std::deque> m_midi_monitor; - int m_note_bpm; - int m_clock_bpm; + AgeOutValue m_note_bpm; + AgeOutValue m_clock_bpm; Touchpad m_touchpad; Temperature m_temperature; -- cgit v1.2.3