Skip to content

Commit

Permalink
transition optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Oct 1, 2024
1 parent f5ce8d6 commit eb9782d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 71 deletions.
95 changes: 50 additions & 45 deletions transitions/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 T> class TrHelper2 {};

template<class TR>
class TrWrapper : public TransitionInterface {
class TrHelper2<TypeList<TR>> : 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<class A, class B>
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... TR> class TrHelper {};
template<class TR>
class TrHelper<TR> {
public:
TransitionInterface* get(int n) { return &tr_; }
template<class T1, class T2, class... REST>
class TrHelper2<TypeList<T1, T2, REST...>> {
private:
TrWrapper<TR> tr_;
PONUA TrHelper2<typename SplitTypeList<TypeList<T1, T2, REST...>>::first_half> a_;
PONUA TrHelper2<typename SplitTypeList<TypeList<T1, T2, REST...>>::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<class A, class B>
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 TR, class... REST>
class TrHelper<TR, REST...> {
template<class... TR>
class TrHelper3 {
protected:
TrHelper2<TypeList<TR...>> 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<class A, class B>
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> tr_;
TrHelper<REST...> rest_;
};


template<class... TRANSITION>
class TrRandom {
class TrRandom : public TrHelper3<TRANSITION...> {
public:
void begin() {
selected_ = transitions_.get(random(sizeof...(TRANSITION)));
selected_->begin();
this->selected_ = random(sizeof...(TRANSITION));
TrHelper3<TRANSITION...>::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<TRANSITION...> transitions_;
TransitionInterface* selected_;
};

#endif
19 changes: 5 additions & 14 deletions transitions/select.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// with Int<0> representing first transition

template<class F, class... TRANSITION>
class TrSelect {
class TrSelect : public TrHelper3<TRANSITION...> {
public:
void begin() {
begin_ = true;
Expand All @@ -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<TRANSITION...>::begin();
}
selected_->run(blade);
TrHelper3<TRANSITION...>::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<TRANSITION...>::done();
}

private:
bool begin_ = false;
PONUA SVFWrapper<F> f_;
PONUA TrHelper<TRANSITION...> transitions_;
TransitionInterface* selected_ = nullptr;
};

#endif
17 changes: 5 additions & 12 deletions transitions/sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,28 @@
// wraps back around to TR1.

template<class... TRANSITION>
class TrSequence {
class TrSequence : public TrHelper3<TRANSITION...>{
public:
void begin() {
begin_ = true;
}
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<TRANSITION...>::begin();
}
selected_->run(blade);
TrHelper3<TRANSITION...>::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<TRANSITION...>::done();
}

private:
bool begin_ = false;
int n_ = -1;
PONUA TrHelper<TRANSITION...> transitions_;
TransitionInterface* selected_ = nullptr;
};

#endif

0 comments on commit eb9782d

Please sign in to comment.