From eb9782dcbda9984ce2ee052ae0f2045243377b2b Mon Sep 17 00:00:00 2001 From: profezzorn Date: Tue, 1 Oct 2024 05:23:24 +0000 Subject: [PATCH] transition optimizations --- transitions/random.h | 95 ++++++++++++++++++++++-------------------- transitions/select.h | 19 +++------ transitions/sequence.h | 17 +++----- 3 files changed, 60 insertions(+), 71 deletions(-) diff --git a/transitions/random.h b/transitions/random.h index 72c2c98f3..631ee3eb9 100644 --- a/transitions/random.h +++ b/transitions/random.h @@ -8,69 +8,74 @@ // transition is picked from the specified list of // transitions. -class TransitionInterface { -public: - virtual bool done() = 0; - virtual void begin() = 0; - virtual void run(BladeBase* blade) = 0; - virtual RGBA getColor(const RGBA& a, const RGBA& b, int led) = 0; -}; +template class TrHelper2 {}; template -class TrWrapper : public TransitionInterface { +class TrHelper2> : public TR { +private: + TR tr_; public: - bool done() override { return tr_.done(); } - void begin() override { tr_.begin(); } - void run(BladeBase* blade) override { tr_.run(blade); } - RGBA getColor(const RGBA& a, - const RGBA& b, - int led) override { - return tr_.getColor(a, b, led); + size_t size() { return 1; } + bool done(int N) { return tr_.done(); } + void begin(int N) { tr_.begin(); } + void run(int N, BladeBase* blade) { tr_.run(blade); } + template + auto getColor(int N, A a, B b, int led) -> decltype(tr_.getColor(a, b, led)) { + return TR::getColor(a, b, led); } -public: - TR tr_; }; -// This could be made generic for any interface. -template class TrHelper {}; -template -class TrHelper { -public: - TransitionInterface* get(int n) { return &tr_; } +template +class TrHelper2> { private: - TrWrapper tr_; + PONUA TrHelper2>::first_half> a_; + PONUA TrHelper2>::second_half> b_; +public: + size_t size() { return sizeof...(REST) + 2; } + + bool done(int N) { + if (N < a_.size()) return a_.done(N); + return b_.done(N - a_.size()); + } + void begin(int N) { + if (N < a_.size()) return a_.begin(N); + return b_.begin(N - a_.size()); + } + void run(int N, BladeBase* blade) { + if (N < a_.size()) return a_.run(N, blade); + return b_.run(N - a_.size(), blade); + } + template + auto getColor(int N, A a, B b, int led) -> decltype(MixColors(a_.getColor(0, a, b, led), b_.getColor(0, a, b, led), 1, 1)) { + if (N < a_.size()) return a_.getColor(N, a, b, led); + return b_.getColor(N - a_.size(), a, b, led); + } }; -template -class TrHelper { +template +class TrHelper3 { +protected: + TrHelper2> tr_; + uint16_t selected_; public: - TransitionInterface* get(int n) { - if (!n) return &tr_; - return rest_.get(n-1); + bool done() { return tr_.done(selected_); } + void begin() { tr_.begin(selected_); } + void run(BladeBase* blade) { tr_.run(selected_, blade); } + + template + auto getColor(A a, B b, int led) -> decltype(tr_.getColor(0, a, b, led)) { + return tr_.getColor(selected_, a, b, led); } -private: - TrWrapper tr_; - TrHelper rest_; }; template -class TrRandom { +class TrRandom : public TrHelper3 { public: void begin() { - selected_ = transitions_.get(random(sizeof...(TRANSITION))); - selected_->begin(); + this->selected_ = random(sizeof...(TRANSITION)); + TrHelper3::begin(); } - bool done() { return selected_->done(); } - void run(BladeBase* blade) { selected_->run(blade); } - RGBA getColor(const RGBA& a, - const RGBA& b, - int led) { - return selected_->getColor(a, b, led); - } -private: - TrHelper transitions_; - TransitionInterface* selected_; }; #endif diff --git a/transitions/select.h b/transitions/select.h index f27aee328..6726cea54 100644 --- a/transitions/select.h +++ b/transitions/select.h @@ -12,7 +12,7 @@ // with Int<0> representing first transition template -class TrSelect { +class TrSelect : public TrHelper3 { public: void begin() { begin_ = true; @@ -21,29 +21,20 @@ class TrSelect { f_.run(blade); if (begin_) { begin_ = false; - int f = f_.calculate(blade); - while(f < 0) f += sizeof...(TRANSITION) << 8; - uint8_t selection = f % sizeof...(TRANSITION); - selected_ = transitions_.get(selection % sizeof...(TRANSITION)); - selected_->begin(); + this->selected_ = MOD(f_.calculate(blade), sizeof...(TRANSITION)); + TrHelper3::begin(); } - selected_->run(blade); + TrHelper3::run(blade); } - RGBA getColor(const RGBA& a, const RGBA& b, int led) { - return selected_->getColor(a, b, led); - } bool done() { if (begin_) return false; - if (!selected_) return true; - return selected_->done(); + return TrHelper3::done(); } private: bool begin_ = false; PONUA SVFWrapper f_; - PONUA TrHelper transitions_; - TransitionInterface* selected_ = nullptr; }; #endif diff --git a/transitions/sequence.h b/transitions/sequence.h index 1276d9ef2..4ec3a4aea 100644 --- a/transitions/sequence.h +++ b/transitions/sequence.h @@ -13,7 +13,7 @@ // wraps back around to TR1. template -class TrSequence { +class TrSequence : public TrHelper3{ public: void begin() { begin_ = true; @@ -21,27 +21,20 @@ class TrSequence { void run(BladeBase* blade) { if (begin_) { begin_ = false; - n_ = (n_ + 1) % sizeof...(TRANSITION); - selected_ = transitions_.get(n_); - selected_->begin(); + this->selected_ = (this->selected_ + 1) % sizeof...(TRANSITION); + TrHelper3::begin(); } - selected_->run(blade); + TrHelper3::run(blade); } - RGBA getColor(const RGBA& a, const RGBA& b, int led) { - return selected_->getColor(a, b, led); - } bool done() { if (begin_) return false; - if (!selected_) return true; - return selected_->done(); + return TrHelper3::done(); } private: bool begin_ = false; int n_ = -1; - PONUA TrHelper transitions_; - TransitionInterface* selected_ = nullptr; }; #endif