diff --git a/src/utils.cc b/src/utils.cc new file mode 100644 index 0000000..3ecfe38 --- /dev/null +++ b/src/utils.cc @@ -0,0 +1,119 @@ +#include "utils.hh" + +namespace Heck { + + void GetMidiTypeAsString(const ld::MidiEvent& msg, char* str) + { + switch (msg.type) { + case ld::NoteOff: + strcpy(str, "NoteOff"); + break; + case ld::NoteOn: + strcpy(str, "NoteOn"); + break; + case ld::PolyphonicKeyPressure: + strcpy(str, "PolyKeyPres."); + break; + case ld::ControlChange: + strcpy(str, "CC"); + break; + case ld::ProgramChange: + strcpy(str, "Prog. Change"); + break; + case ld::ChannelPressure: + strcpy(str, "Chn. Pressure"); + break; + case ld::PitchBend: + strcpy(str, "PitchBend"); + break; + case ld::SystemCommon: + strcpy(str, "Sys. Common"); + break; + case ld::SystemRealTime: + strcpy(str, "Sys. Realtime"); + break; + case ld::ChannelMode: + strcpy(str, "Chn. Mode"); + break; + default: + strcpy(str, "Unknown"); + break; + } + } + + void GetMidiRTTypeAsString(const ld::MidiEvent& msg, char* str) + { + switch (msg.srt_type) { + case ld::TimingClock: + strcpy(str, "TimingClock"); + break; + case ld::SRTUndefined0: + strcpy(str, "SRTUndefined0"); + break; + case ld::Start: + strcpy(str, "Start"); + break; + case ld::Continue: + strcpy(str, "Continue"); + break; + case ld::Stop: + strcpy(str, "Stop"); + break; + case ld::SRTUndefined1: + strcpy(str, "SRTUndefined1"); + break; + case ld::ActiveSensing: + strcpy(str, "ActiveSensing"); + break; + case ld::Reset: + strcpy(str, "Reset"); + break; + case ld::SystemRealTimeLast: + strcpy(str, "SystemRealTimeLast"); + break; + default: + strcpy(str, "Unknown"); + break; + } + } + + + float scalen_min_max(float val, float min, float max) + { + float range = max - min; + float ret = min + (val * range); + return ret; + } + + float scalen_center_range(float val, float center, float range) + { + float min = center - (range / 2); + float ret = min + (val * range); + return ret; + } +} // namespace Heck + +namespace Heck { + void SWTimer::set_period(u32 time_units) + { + time_period_ = time_units; + } + + void SWTimer::set_callback(const Callback& cb) + { + callback_ = cb; + } + + 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_) { + //todo: constexpr if metrics + callback_(); + } + return true; + } + return false; + } +} // namespace Heck diff --git a/src/utils.hh b/src/utils.hh new file mode 100644 index 0000000..aa7426c --- /dev/null +++ b/src/utils.hh @@ -0,0 +1,60 @@ +#ifndef HECK_DAISY_UTILS_HH +#define HECK_DAISY_UTILS_HH + +#include "daisy_seed.h" +#include "types.hh" + +namespace Heck { + void GetMidiTypeAsString(const ld::MidiEvent& msg, char* str); + void GetMidiRTTypeAsString(const ld::MidiEvent& msg, char* str); + + float scalen_min_max(float val, float min, float max); + float scalen_center_range(float val, float center, float range); +} // namespace Heck + + +namespace Heck { + class SWTimer { + public: + using Callback = std::function; + + 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