From 78a52d8107ed285a03d98ab7eeca4e9302fd3b1e Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 26 Sep 2024 21:16:21 +0200 Subject: [PATCH] utils - update from template repo --- src/utils.cc | 10 ++++------ src/utils.hh | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/utils.cc b/src/utils.cc index 3c5ca3f..3ecfe38 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -94,7 +94,6 @@ namespace Heck { } // namespace Heck namespace Heck { - void SWTimer::set_period(u32 time_units) { time_period_ = time_units; @@ -108,14 +107,13 @@ namespace Heck { bool SWTimer::is_it_already_time_again(u32 time_now) { if (time_now - time_last_exec_ >= time_period_) { + time_last_exec_ = time_now; if (callback_) { - callback_(time_now); + //todo: constexpr if metrics + callback_(); } - time_last_exec_ = time_now; return true; } return false; } - - -} // namespace Heck \ No newline at end of file +} // namespace Heck diff --git a/src/utils.hh b/src/utils.hh index 9f2fd99..953b6eb 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -1,8 +1,8 @@ -#ifndef HECK_OSP_UTILS_HH -#define HECK_OSP_UTILS_HH +#ifndef HECK_DAISY_UTILS_HH +#define HECK_DAISY_UTILS_HH #include "daisy_seed.h" -#include "main_osp.hh" +#include "types.hh" namespace Heck { void GetMidiTypeAsString(const ld::MidiEvent& msg, char* str); @@ -21,10 +21,40 @@ namespace Heck { void set_period(u32 time_units); void set_callback(const Callback& cb); bool is_it_already_time_again(u32 time_now); + private: Callback callback_{}; u32 time_last_exec_{}; u32 time_period_{}; }; } // namespace Heck + +namespace Heck { + template class Observer { + + public: + bool on_change(T val_new, std::function fn) + { + if (val_new != val_current_) { + val_current_ = val_new; + fn(val_current_); + return true; + } + return false; + } + + bool on_change_fuzzy(T val_new, T min_deviation, std::function fn) + { + if (std::abs(val_new - val_current_) >= min_deviation) { + val_current_ = val_new; + fn(val_current_); + return true; + } + return false; + } + + private: + static inline T val_current_{}; + }; +} // namespace Heck #endif // HECK_DAISY_UTILS_HH \ No newline at end of file