Skip to content

Commit

Permalink
move effect queue from blades to SaberBase
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Jan 7, 2024
1 parent 3bfcc59 commit 3dbc1ed
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 121 deletions.
80 changes: 7 additions & 73 deletions blades/abstract_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@ int GetBladeNumber(BladeBase *blade);
class AbstractBlade : public BladeBase, public SaberBase {
public:
AbstractBlade() : SaberBase(NOLINK) {}
void Activate() override {
void Activate(int blade_number) override {
blade_number_ = blade_number;
SaberBase::Link(this);
}
void Deactivate() override {
SaberBase::Unlink(this);
}

size_t GetEffects(BladeEffect** blade_effects) override {
*blade_effects = effects_;
while (num_effects_ &&
micros() - effects_[num_effects_-1].start_micros > 7000000) {
num_effects_--;
}
return num_effects_;
}

void SetStyle(BladeStyle* style) override {
// current_style should be nullptr;
current_style_ = style;
Expand All @@ -36,7 +28,6 @@ class AbstractBlade : public BladeBase, public SaberBase {
BladeStyle *ret = current_style_;
if (ret) {
ret->deactivate();
num_effects_ = 0;
}
current_style_ = nullptr;
return ret;
Expand All @@ -46,75 +37,18 @@ class AbstractBlade : public BladeBase, public SaberBase {
return current_style_;
}

bool CheckBlade(EffectLocation location) override {
return location.on_blade(GetBladeNumber(this));
}

virtual void SB_Effect2(BladeEffectType type, EffectLocation location) override {
switch (type) {
default: break;
case EFFECT_LOCKUP_BEGIN:
switch (SaberBase::Lockup()) {
case LOCKUP_DRAG:
type = EFFECT_DRAG_BEGIN;
case LOCKUP_NORMAL:
break;
default: return;
}
break;
case EFFECT_LOCKUP_END:
switch (SaberBase::Lockup()) {
case LOCKUP_DRAG:
type = EFFECT_DRAG_END;
case LOCKUP_NORMAL:
break;
default: return;
}
break;
case EFFECT_CLASH_UPDATE:
return;
}
for (size_t i = NELEM(effects_) - 1; i; i--) {
effects_[i] = effects_[i-1];
}
effects_[0].type = type;
effects_[0].start_micros = micros();
effects_[0].location = location;
effects_[0].sound_length = SaberBase::sound_length;
effects_[0].wavnum = SaberBase::sound_number;
num_effects_ = std::min(num_effects_ + 1, NELEM(effects_));
}


bool IsPrimary() override {
return GetPrimaryBlade() == this;
int GetBladeNumber() override {
return blade_number_;
}

void SB_On2(EffectLocation location) override {
SB_Effect2(EFFECT_IGNITION, location);
}

void SB_Off2(OffType off_type, EffectLocation location) override {
switch (off_type) {
case OFF_BLAST:
SB_Effect2(EFFECT_BLAST, location);
break;
case OFF_NORMAL:
case OFF_FAST:
SB_Effect2(EFFECT_RETRACTION, location);
break;
case OFF_IDLE:
case OFF_CANCEL_PREON:
// do nothing
break;
}
bool CheckBlade(EffectLocation location) override {
return location.on_blade(GetBladeNumber());
}

protected:
BladeStyle *current_style_ = nullptr;
private:
size_t num_effects_ = 0;
BladeEffect effects_[6];
int blade_number_;
};

#ifdef BLADE_ID_SCAN_MILLIS
Expand Down
23 changes: 7 additions & 16 deletions blades/blade_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,13 @@ using BladeEffectType = EffectType;

#include "../styles/blade_style.h"

struct BladeEffect {
BladeEffectType type;

uint32_t start_micros;
EffectLocation location; // 0 = base, 1 = tip
float sound_length;
int wavnum;
};

class BladeBase {
public:
// Returns number of LEDs in this blade.
virtual int num_leds() const = 0;

virtual int GetBladeNumber() = 0;

// Returns the byte order of this blade.
virtual Color8::Byteorder get_byteorder() const = 0;

Expand All @@ -42,9 +35,6 @@ class BladeBase {

virtual bool is_powered() const = 0;

// Return how many effects are in effect.
virtual size_t GetEffects(BladeEffect** blade_effects) = 0;

// Set led 'led' to color 'c'.
virtual void set(int led, Color16 c) = 0;

Expand All @@ -61,9 +51,8 @@ class BladeBase {
// disable power now. (Usually called after is_on()
// has returned false for some period of time.)
virtual void allow_disable() = 0;
virtual bool IsPrimary() = 0;

virtual void Activate() = 0;
virtual void Activate(int blade_number) = 0;
virtual void Deactivate() = 0;

virtual BladeStyle* UnSetStyle() = 0;
Expand Down Expand Up @@ -120,13 +109,15 @@ class OneshotEffectDetector {
}
BladeEffect* Find(BladeBase* blade) {
BladeEffect* effects;
size_t n = blade->GetEffects(&effects);
size_t n = SaberBase::GetEffects(&effects);
int blade_number = blade->GetBladeNumber();
// If no other thing is handling stab, treat it like a clash.
// But only for the primary blade...
bool match_stab = effect == EFFECT_CLASH &&
!blade->current_style()->IsHandled(HANDLED_FEATURE_STAB) &&
blade->IsPrimary();
blade_number == 1;
for (size_t i = 0; i < n; i++) {
if (!effects[i].location.on_blade(blade_number)) continue;
if (effect == effects[i].type ||
(match_stab && effects[i].type == EFFECT_STAB)) {
return effects + i;
Expand Down
8 changes: 2 additions & 6 deletions blades/blade_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ class BladeWrapper : public BladeBase {
void set_overdrive(int led, Color16 c) override {
return blade_->set_overdrive(led, c);
}
size_t GetEffects(BladeEffect** blade_effects) override {
return blade_->GetEffects(blade_effects);
}
void clear() override { return blade_->clear(); }
void allow_disable() override { blade_->allow_disable(); }
void Activate() override { blade_->Activate(); }
void Activate(int blade_number) override { blade_->Activate(blade_number); }
void Deactivate() override { blade_->Deactivate(); }
bool IsPrimary() override { return blade_->IsPrimary(); }
void SetStyle(BladeStyle* style) override { blade_->SetStyle(style); }
BladeStyle* UnSetStyle() override { return blade_->UnSetStyle(); }
BladeStyle* current_style() const override { return blade_->current_style(); }

int GetBladeNumber() { return blade_->GetBladeNumber(); }

BladeBase* blade_;
};
Expand Down
4 changes: 2 additions & 2 deletions blades/fastled_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class FASTLED_Blade : public AbstractBlade, CommandParser, Looper {

// No need for a "deactivate", the blade stays active until
// you take it out, which also cuts the power.
void Activate() override {
void Activate(int blade_number) override {
STDOUT.print("FASTLED Blade with ");
STDOUT.print(num_leds_);
STDOUT.println(" leds");
Expand All @@ -61,7 +61,7 @@ class FASTLED_Blade : public AbstractBlade, CommandParser, Looper {
Show();
CommandParser::Link();
Looper::Link();
AbstractBlade::Activate();
AbstractBlade::Activate(blade_number);
}

void Deactivate() override {
Expand Down
4 changes: 2 additions & 2 deletions blades/saviblade.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SaviBlade : public AbstractBlade, Looper {

const char* name() override { return "Savi_Blade"; }

void Activate() override {
void Activate(int blade_number) override {
STDOUT.println("Savi Blade");
#ifdef TIM7_BASE
stm32l4_timer_create(&timer_, TIMER_INSTANCE_TIM7, STM32L4_TONE_IRQ_PRIORITY, 0);
Expand All @@ -26,7 +26,7 @@ class SaviBlade : public AbstractBlade, Looper {
#endif
Power(true);
Looper::Link();
AbstractBlade::Activate();
AbstractBlade::Activate(blade_number);
digitalWrite(pin_, HIGH);
pinMode(pin_, OUTPUT);
}
Expand Down
4 changes: 2 additions & 2 deletions blades/simple_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ class Simple_Blade : public AbstractBlade, CommandParser, Looper {
}
const char* name() override { return "Simple_Blade"; }

void Activate() override {
void Activate(int blade_number) override {
STDOUT.println("Simple Blade");
Power(true);
CommandParser::Link();
Looper::Link();
AbstractBlade::Activate();
AbstractBlade::Activate(blade_number);
}

void Deactivate() override {
Expand Down
10 changes: 8 additions & 2 deletions blades/sub_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ class SubBladeWrapper : public BladeWrapper, BladeStyle {
} while (tmp != this);
return some_subblade_is_active;
}
void Activate() override {
void Activate(int blade_number) override {
blade_number_ = blade_number;
if (!active_) {
if (!SomeSubBladeIsActive()) BladeWrapper::Activate();
if (!SomeSubBladeIsActive()) BladeWrapper::Activate(0);
active_ = true;
}
}
Expand Down Expand Up @@ -142,12 +143,17 @@ class SubBladeWrapper : public BladeWrapper, BladeStyle {
return false;
}

int GetBladeNumber() override {
return blade_number_;
}

protected:
BladeStyle *current_style_ = nullptr;
int num_leds_;
int offset_;
bool allow_disable_;
SubBladeWrapper* next_;
int blade_number_;
};

SubBladeWrapper* first_subblade_wrapper = NULL;
Expand Down
4 changes: 2 additions & 2 deletions blades/ws2811_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ WS2811_Blade(WS2811PIN* pin,
allow_disable_ = false;
}

void Activate() override {
void Activate(int blade_number) override {
TRACE(BLADE, "Activate");
STDOUT.print("WS2811 Blade with ");
STDOUT.print(pin_->num_leds());
STDOUT.println(" leds.");
run_ = true;
CommandParser::Link();
Looper::Link();
AbstractBlade::Activate();
AbstractBlade::Activate(blade_number);
}

void Deactivate() override {
Expand Down
Loading

0 comments on commit 3dbc1ed

Please sign in to comment.