Skip to content

Commit

Permalink
Merge branch 'master' into v4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed May 7, 2020
2 parents d67d268 + 98f3a69 commit 3e56a95
Show file tree
Hide file tree
Showing 24 changed files with 888 additions and 162 deletions.
5 changes: 5 additions & 0 deletions blades/blade_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ DEFINE_ALL_EFFECTS();
HANDLED_FEATURE_DRAG = 1 << 3,
HANDLED_FEATURE_MELT = 1 << 4,
HANDLED_FEATURE_LIGHTNING_BLOCK = 1 << 5,
HANDLED_FEATURE_PREON = 1 << 6,
};

#include "../styles/blade_style.h"
Expand Down Expand Up @@ -135,6 +136,10 @@ class OneshotEffectDetector {
switch (effect) {
case EFFECT_STAB:
BladeBase::HandleFeature(HANDLED_FEATURE_STAB);
break;
case EFFECT_PREON:
BladeBase::HandleFeature(HANDLED_FEATURE_PREON);
break;
default:
break;
}
Expand Down
8 changes: 8 additions & 0 deletions blades/fastled_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ class FASTLED_Blade : public AbstractBlade, CommandParser, Looper {
delay(10);
on_ = true;
}
void SB_PreOn(float* d) override {
AbstractBlade::SB_PreOn(d);
// This blade uses EFFECT_PREON, so we need to turn the power on now.
if (IsHandled(HANDLED_FEATURE_PREON)) {
Power(true);
delay(10);
}
}
void SB_Off(OffType off_type) override {
AbstractBlade::SB_Off(off_type);
on_ = false;
Expand Down
8 changes: 8 additions & 0 deletions blades/simple_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ class Simple_Blade : public AbstractBlade, CommandParser, Looper {
on_ = true;
Power(true);
}
void SB_PreOn(float* delay) override {
AbstractBlade::SB_PreOn(delay);
// This blade uses EFFECT_PREON, so we need to turn the power on now.
if (IsHandled(HANDLED_FEATURE_PREON)) {
battery_monitor.SetLoad(true);
Power(true);
}
}
void SB_Off(OffType off_type) override {
AbstractBlade::SB_Off(off_type);
battery_monitor.SetLoad(false);
Expand Down
85 changes: 85 additions & 0 deletions blades/spiled_pin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef BLADES_SPILEDPIN_H
#define BLADES_SPILEDPIN_H

template<int CLOCK_PIN>
class SpiLedPin : public Looper {
public:
SpiLedPin(int pin,
int num_leds,
Color8::Byteorder byteorder,
int frequency,
int reset_us,
int t0h_us = 294,
int t1h_us = 862) :
pin_(pin),
num_leds_(num_leds),
frequency_(frequency),
byteorder_(byteorder) {
pinMode(CLOCK_PIN, OUTPUT);
digitalWrite(CLOCK_PIN, HIGH);
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}

bool IsReadyForBeginFrame() { return true; }
bool IsReadyForEndFrame() { return true; }

static inline void delay_nanos(uint32_t nanos) {
#ifdef TEENSYDUINO
uint32_t scale = F_CPU / 1000000;
#else
uint32_t scale = SystemCoreClock / 1000000;
#endif
uint32_t n = (nanos * scale + 2) / 3000;

__asm__ __volatile__(
"1: subs %0, #1 \n"
" bne 1b \n"
: "+r" (n));
}

void OutByte(uint8_t output) {
for (int bit = 0; bit < 8; bit++) {
digitalWrite(CLOCK_PIN, LOW);
digitalWrite(pin_, output & 0x80);
delay_nanos(500000000 / frequency_);
output <<= 1;
digitalWrite(CLOCK_PIN, HIGH);
delay_nanos(500000000 / frequency_);
}
}

void BeginFrame() {
for (int i = Color8::num_bytes(byteorder_); i >= 0; i--) OutByte(0);
}
void EndFrame() {
for (int i = Color8::num_bytes(byteorder_); i >= 0; i--) OutByte(0xff);
}

void Set(int led, Color16 color) {
uint8_t *output = ((uint8_t *)displayMemory) + (led + 1) * (1 + Color8::num_bytes(byteorder_));
int MAX = std::max(color.r, std::max(color.g, color.b));
int output_level = (MAX + 2113) / 2117;
int mult = output_level == 0 ? 0 : 7930 / output_level;
OutByte(0xE0 | output_level);
for (int i = Color8::num_bytes(byteorder_) - 1; i >= 0; i--) {
OutByte(color.getShort(byteorder_, i) * mult >> 16);
}
}

void Set(int led, Color8 color) {
Set(led, Color16(color));
}

int num_leds() const { return num_leds_; }
Color8::Byteorder get_byteorder() const { return byteorder_; }
uint8_t pin() const { return pin_; }

private:
uint8_t pin_;
uint32_t num_leds_;
uint32_t frequency_;
Color8::Byteorder byteorder_;
};

#endif
9 changes: 9 additions & 0 deletions blades/ws2811_blade.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ DMAMEM int displayMemory[maxLedsPerStrip * 24 / 4 + 1];
#include "ws2811_serial.h"
#define DefaultPinClass MonopodWSPin
#endif
#include "spiled_pin.h"

Color16 color_buffer[maxLedsPerStrip];
BladeBase* current_blade = NULL;
Expand Down Expand Up @@ -130,6 +131,14 @@ class WS2811_Blade : public AbstractBlade, CommandParser, Looper {
on_ = true;
power_off_requested_ = false;
}
void SB_PreOn(float* delay) override {
AbstractBlade::SB_PreOn(delay);
// This blade uses EFFECT_PREON, so we need to turn the power on now.
if (IsHandled(HANDLED_FEATURE_PREON)) {
Power(true);
power_off_requested_ = false;
}
}
void SB_Off(OffType off_type) override {
TRACE("SB_Off");
AbstractBlade::SB_Off(off_type);
Expand Down
6 changes: 4 additions & 2 deletions buttons/button_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class ButtonBase : public Looper,
// may double-presses there has been. The count starts at FIRST, that way
// the first event can be distinguished from the rest.
void Send(uint32_t event) {
prop.Event(button_, (EVENT)event);
prop.Event(button_, (EVENT)(event + (EVENT_SECOND_PRESSED - EVENT_FIRST_PRESSED) * press_count_));
if (!prop.Event(button_, (EVENT)(event + (EVENT_SECOND_PRESSED - EVENT_FIRST_PRESSED) * press_count_))) {
// Only send the second event if the first event didn't trigger a response.
prop.Event(button_, (EVENT)event);
}
}

// We send the click immediately, but we also hold a "saved" event
Expand Down
5 changes: 4 additions & 1 deletion common/current_preset.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ class CurrentPreset {
FileReader f, out;
if (!OpenPresets(&f, "presets.ini")) {
if (!UpdateINI()) CreateINI();
OpenPresets(&f, "presets.ini");
if (!OpenPresets(&f, "presets.ini")) {
STDOUT << "SAVING FAILED!!!!\n";
return;
}
}
PathHelper tmp_fn(GetSaveDir(), "presets.tmp");
LSFS::Remove(tmp_fn);
Expand Down
2 changes: 1 addition & 1 deletion functions/blast.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class OriginalBlastF {
mix += std::max(0.0, 2.0 * sinf(x / (t*t)) / x);
}
}
return std::min(mix, 1.0) * 32768;
return std::min<float>(mix, 1.0) * 32768;
}
private:
int num_leds_;
Expand Down
30 changes: 29 additions & 1 deletion functions/ifon.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Ifon {
template<class OUT_MILLIS, class IN_MILLIS>
class InOutFuncX {
public:
void run(BladeBase* blade) {
FunctionRunResult run(BladeBase* blade) {
out_millis_.run(blade);
in_millis_.run(blade);
uint32_t now = micros();
Expand All @@ -58,6 +58,8 @@ class InOutFuncX {
extension = std::max(extension, 0.0f);
}
ret_ = extension * 32768.0;
if (!blade->is_on() && ret_ == 0) return FunctionRunResult::ZERO_UNTIL_IGNITION;
return FunctionRunResult::UNKNOWN;
}
int getInteger(int led) { return ret_; }

Expand All @@ -69,6 +71,32 @@ class InOutFuncX {
int ret_;
};


template<class EXTENSION, bool ALLOW_DISABLE=1 >
class InOutHelperF {
public:
FunctionRunResult run(BladeBase* blade) __attribute__((warn_unused_result)) {
FunctionRunResult ret = RunFunction(&extension_, blade);
thres = (extension_.getInteger(0) * blade->num_leds());
if (ALLOW_DISABLE) {
switch (ret) {
case FunctionRunResult::ZERO_UNTIL_IGNITION: return FunctionRunResult::ONE_UNTIL_IGNITION;
case FunctionRunResult::ONE_UNTIL_IGNITION: return FunctionRunResult::ZERO_UNTIL_IGNITION;
case FunctionRunResult::UNKNOWN: break;
}
}
return FunctionRunResult::UNKNOWN;
}
int getInteger(int led) {
return 32768 - clampi32(thres - led * 32768, 0, 32768);
}
private:
EXTENSION extension_;
int thres = 0;
};



#include "trigger.h"
#include "scale.h"

Expand Down
8 changes: 7 additions & 1 deletion functions/int.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
template<int N>
class Int {
public:
void run(BladeBase* base) {}
FunctionRunResult run(BladeBase* base) {
switch (N) {
case 0: return FunctionRunResult::ZERO_UNTIL_IGNITION;
case 32768: return FunctionRunResult::ONE_UNTIL_IGNITION;
default: return FunctionRunResult::UNKNOWN;
}
}
int getInteger(int led) { return N; }
};

Expand Down
3 changes: 2 additions & 1 deletion props/prop_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,8 @@ class PropBase : CommandParser, Looper, protected SaberBase {
if (player) {
STDOUT.print("Playing ");
STDOUT.println(arg);
player->Play(arg);
if (!player->PlayInCurrentDir(arg))
player->Play(arg);
} else {
STDOUT.println("No available WAV players.");
}
Expand Down
22 changes: 11 additions & 11 deletions props/saber.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ class Saber : public PropBase {
#endif
return true;

// Handle double-click with preon
// Handle double-click with preon
case EVENTID(BUTTON_POWER, EVENT_DOUBLE_CLICK, MODE_OFF):
if (on_pending_) {
if (on_pending_) {
if (SetMute(true)) {
unmute_on_deactivation_ = true;
}
return true;
}
return false;
return true;
}
return false;

case EVENTID(BUTTON_POWER, EVENT_DOUBLE_CLICK, MODE_ON):
if (millis() - activated_ < 500) {
if (SetMute(true)) {
Expand All @@ -85,7 +85,7 @@ class Saber : public PropBase {
}
return true;

case EVENTID(BUTTON_POWER, EVENT_CLICK_SHORT, MODE_ON):
case EVENTID(BUTTON_POWER, EVENT_FIRST_CLICK_SHORT, MODE_ON):
case EVENTID(BUTTON_POWER, EVENT_LATCH_OFF, MODE_ON):
case EVENTID(BUTTON_AUX, EVENT_LATCH_OFF, MODE_ON):
case EVENTID(BUTTON_AUX2, EVENT_LATCH_OFF, MODE_ON):
Expand Down Expand Up @@ -120,7 +120,7 @@ class Saber : public PropBase {
#ifndef DISABLE_COLOR_CHANGE
case EVENTID(BUTTON_POWER, EVENT_CLICK_SHORT, MODE_ON | BUTTON_AUX):
ToggleColorChangeMode();
break;
return true;
#endif

// Lockup
Expand All @@ -138,9 +138,9 @@ class Saber : public PropBase {
break;

case EVENTID(BUTTON_AUX, EVENT_CLICK_SHORT, MODE_ON | BUTTON_POWER):
SaberBase::SetLockup(SaberBase::LOCKUP_LIGHTNING_BLOCK);
SaberBase::DoBeginLockup();
return true;
SaberBase::SetLockup(SaberBase::LOCKUP_LIGHTNING_BLOCK);
SaberBase::DoBeginLockup();
return true;

case EVENTID(BUTTON_NONE, EVENT_STAB, MODE_ON | BUTTON_POWER):
case EVENTID(BUTTON_NONE, EVENT_STAB, MODE_ON | BUTTON_AUX):
Expand Down
Loading

0 comments on commit 3e56a95

Please sign in to comment.