#pragma once #include #include namespace rack_plugin_DHE_Modules { class Latch { public: bool is_high() const { return state==State::HIGH; } /** * Suspends firing events. */ void disable() { enabled = false; } /** * Resumes firing events. */ void enable() { enabled = true; } /** * Registers an action to be called on each rising edge. * @param action called on each rising edge */ void on_rise(std::function action) { rise_actions.push_back(std::move(action)); } /** * Registers an action to be called on each falling edge. * @param action called on each falling edge */ void on_fall(std::function action) { fall_actions.push_back(std::move(action)); } protected: enum class State { UNKNOWN, LOW, HIGH } state = State::UNKNOWN; void set_state(State incoming_state) { if (state==incoming_state) return; state = incoming_state; fire(state==State::HIGH ? rise_actions : fall_actions); } private: bool enabled = true; std::vector> rise_actions; std::vector> fall_actions; void fire(const std::vector> &actions) const { if (!enabled) return; for (const auto &action : actions) action(); } }; }