Skip to content

Commit

Permalink
Update combo blade to cache blade2 results
Browse files Browse the repository at this point in the history
  • Loading branch information
KeeganMorrow committed Jul 11, 2024
1 parent dd955a8 commit 3e227c1
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions blades/combo_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,44 @@ class ComboBladeWrapper : public BladeWrapper, BladeStyle {
public:
ComboBladeWrapper(BladeBase* blade1, BladeBase* blade2):
blade1_num_leds_(blade1->num_leds()),
blade2_num_leds_(blade2->num_leds()),
blade2_(blade2),
real_style_(nullptr)
real_style_(nullptr),
blade2_cache_(new OverDriveColor[blade2_num_leds_])
{
blade_ = blade1;
}

// BladeStyle implementation
void activate() override {
virtual void activate() override {
real_style_->activate();
}
void deactivate() override {
virtual void deactivate() override {
real_style_->deactivate();
}
void run(BladeBase* blade) override {
virtual void run(BladeBase* blade) override {
if (blade == blade_) {
real_style_->run(this);
} else {
// On blade2_ we just apply cached values from when blade_ was run
for (int i = 0; i < blade2_num_leds_; i++){
if (blade2_cache_[i].overdrive) {
blade2_->set_overdrive(i, blade2_cache_[i].c);
} else {
blade2_->set(i, blade2_cache_[i].c);
}
}
}
}

bool NoOnOff() override { return real_style_->NoOnOff(); }
bool NoOnOff() override {
return real_style_->NoOnOff();
}

bool Charging() override { return real_style_->Charging(); }
virtual bool Charging() { return real_style_->Charging(); }

bool IsHandled(HandledFeature effect) override {
if (real_style_)
return real_style_->IsHandled(effect);
return false;
return real_style_->IsHandled(effect);
}

OverDriveColor getColor(int i) override { return real_style_->getColor(i); }
Expand All @@ -76,20 +87,23 @@ class ComboBladeWrapper : public BladeWrapper, BladeStyle {

// BladeBase implementation
int num_leds() const override {
return blade1_num_leds_ + blade2_->num_leds();
return blade1_num_leds_ + blade2_num_leds_;
}
void set(int led, Color16 c) override {
if (led < blade1_num_leds_) {
return blade_->set(led, c);
} else {
return blade2_->set(led - blade1_num_leds_, c);
blade2_cache_[led - blade1_num_leds_].c = c;
blade2_cache_[led - blade1_num_leds_].overdrive = false;
}
}

void set_overdrive(int led, Color16 c) override {
if (led < blade1_num_leds_) {
return blade_->set_overdrive(led, c);
} else {
return blade2_->set_overdrive(led - blade1_num_leds_, c);
blade2_cache_[led - blade1_num_leds_].c = c;
blade2_cache_[led - blade1_num_leds_].overdrive = true;
}
}

Expand Down Expand Up @@ -120,9 +134,12 @@ class ComboBladeWrapper : public BladeWrapper, BladeStyle {
}

BladeStyle* UnSetStyle() override {
BladeStyle* result = real_style_;
BladeStyle* ret = real_style_;
if (ret) {
ret->deactivate();
}
real_style_ = nullptr;
return result;
return ret;
}

BladeStyle* current_style() const override {
Expand All @@ -131,8 +148,10 @@ class ComboBladeWrapper : public BladeWrapper, BladeStyle {

private:
int blade1_num_leds_;
int blade2_num_leds_;
BladeBase* blade2_;
BladeStyle* real_style_;
OverDriveColor *blade2_cache_;
};

BladeBase* ComboBlade(BladeBase* blade1, BladeBase* blade2)
Expand Down

0 comments on commit 3e227c1

Please sign in to comment.