Skip to content

Commit

Permalink
make it possible to select which blades to affect
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Jan 3, 2024
1 parent 0d9a94c commit a91b406
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 14 deletions.
1 change: 0 additions & 1 deletion blades/abstract_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class AbstractBlade : public BladeBase, public SaberBase {
}

void SB_Effect2(BladeEffectType type, EffectLocation location) {
if (!location.on_blade(GetBladeNumber(this))) return;
switch (type) {
default: break;
case EFFECT_LOCKUP_BEGIN:
Expand Down
2 changes: 0 additions & 2 deletions blades/fastled_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class FASTLED_Blade : public AbstractBlade, CommandParser, Looper {
if (on_) *on = true;
}
void SB_On(EffectLocation location) override {
if (!location.on_blade(GetBladeNumber(this))) return;
AbstractBlade::SB_On();
Power(true);
delay(10);
Expand All @@ -113,7 +112,6 @@ class FASTLED_Blade : public AbstractBlade, CommandParser, Looper {
}

void SB_Off(OffType off_type, EffectType location) override {
if (!location.on_blade(GetBladeNumber(this))) return;
AbstractBlade::SB_Off(off_type, location);
on_ = false;
}
Expand Down
3 changes: 0 additions & 3 deletions blades/saviblade.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class SaviBlade : public AbstractBlade, Looper {
}

void SB_On(EffectLocation location) override {
if (!location.on_blade(GetBladeNumber(this))) return;
AbstractBlade::SB_On(location);
battery_monitor.SetLoad(true);
on_ = true;
Expand All @@ -118,7 +117,6 @@ class SaviBlade : public AbstractBlade, Looper {
}

void SB_Off(OffType off_type, EffectLocation location) override {
if (!location.on_blade(GetBladeNumber(this))) return;
AbstractBlade::SB_Off(off_type, location);
battery_monitor.SetLoad(false);
on_ = false;
Expand All @@ -132,7 +130,6 @@ class SaviBlade : public AbstractBlade, Looper {

void SB_Effect(BladeEffectType type, EffectLocation location) {
AbstractBlade::SB_Effect(type, location);
if (!location.on_blade(GetBladeNumber(this))) return;
switch (type) {
default: break;
case EFFECT_CLASH:
Expand Down
1 change: 0 additions & 1 deletion blades/simple_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ class Simple_Blade : public AbstractBlade, CommandParser, Looper {
Power(true);
}
void SB_Off(OffType off_type, EffectLocation location) override {
if (!location.on_blade(GetBladeNumber(this))) return;
AbstractBlade::SB_Off(off_type, location);
battery_monitor.SetLoad(false);
on_ = false;
Expand Down
1 change: 0 additions & 1 deletion blades/ws2811_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ WS2811_Blade(WS2811PIN* pin,
}
void SB_Off(OffType off_type, EffectLocation location) override {
TRACE(BLADE, "SB_Off");
if (!location.on_blade(GetBladeNumber(this))) return;
AbstractBlade::SB_Off(off_type, location);
on_ = false;
if (off_type == OFF_IDLE) {
Expand Down
22 changes: 16 additions & 6 deletions common/saber_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class EffectLocation {
public:
enum BladeBit : uint16_t {
ALL_BLADES = 0xffff,
NON_BLADES = 1 << 0,
BLADE1 = 1 << 1,
BLADE2 = 1 << 2,
BLADE3 = 1 << 3,
Expand All @@ -141,9 +142,9 @@ class EffectLocation {
BLADE14 = 1 << 14,
BLADE15 = 1 << 15,
};
EffectLocation() : location_(0) {}
EffectLocation(uint16_t location, uint16_t blades) : location_(location + ((~blades) << 16)) {}
EffectLocation(float f) :location_(f * 32768) {}
constexpr EffectLocation() : location_(0) {}
constexpr EffectLocation(uint16_t location, uint16_t blades) : location_(location + ((~blades) << 16)) {}
constexpr EffectLocation(float f) :location_(f * 32768) {}
static EffectLocation rnd() {
return EffectLocation(65535 + random(22937), ALL_BLADES);
}
Expand All @@ -158,6 +159,9 @@ class EffectLocation {
uint32_t location_;
};

class BladeBase;
extern int GetBladeNumber(BladeBase *blade);

class SaberBase {
protected:
void Link(SaberBase* x) {
Expand Down Expand Up @@ -200,7 +204,7 @@ class SaberBase {
}
static void TurnOn(EffectLocation location) {
on_ = true;
SaberBase::DoOn(locatin);
SaberBase::DoOn(location);
}
static void TurnOff(OffType off_type) {
on_ = false;
Expand Down Expand Up @@ -264,16 +268,20 @@ class SaberBase {
sound_number = -1;
}

static EffectLocation location; // fallback

#define SABERFUN(NAME, TYPED_ARGS, ARGS) \
public: \
static void Do##NAME TYPED_ARGS { \
ClearSoundInfo(); \
CHECK_LL(SaberBase, saberbases, next_saber_); \
for (SaberBase *p = saberbases; p; p = p->next_saber_) { \
p->SB_##NAME ARGS; \
if (location.on_blade(GetBladeNumber((BladeBase*)p))) \
p->SB_##NAME ARGS; \
} \
for (SaberBase *p = saberbases; p; p = p->next_saber_) { \
p->SB_##NAME##2 ARGS; \
if (location.on_blade(GetBladeNumber((BladeBase*)p))) \
p->SB_##NAME##2 ARGS; \
} \
CHECK_LL(SaberBase, saberbases, next_saber_); \
} \
Expand Down Expand Up @@ -420,4 +428,6 @@ public: \
SaberBase* next_saber_;
};

EffectLocation SaberBase::location;

#endif
3 changes: 3 additions & 0 deletions styles/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ int32_t clampi32(int32_t x, int32_t a, int32_t b) {
}

int random(int x) { return (rand() & 0x7fffff) % x; }
class BladeBase;
int GetBladeNumber(BladeBase* blad) { return 0; }

class Looper {
public:
static void DoHFLoop() {}
Expand Down

0 comments on commit a91b406

Please sign in to comment.