Merge pull request #550 from keyboardio/plugin/Chase/assorted-fixes

Assorted Chase fixes
pull/541/head
Jesse Vincent 6 years ago committed by GitHub
commit 9a4e7cca55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,8 +21,23 @@ void setup() {
## Plugin methods ## Plugin methods
The plugin provides the `LEDChaseEffect` object, which has no public methods or The plugin provides the `LEDChaseEffect` object, which has the following methods
properties, outside of those provided by all LED modes. outside of those provided by all LED modes:
### `.update_delay([delay])`
> Accessor for the update delay, the time between each step of the animation.
> When called without an argument, returns the current setting. When called with
> one, sets it.
>
> Defaults to 150 (milliseconds).
### `.distance([pixels])`
> Accessor for the distance between the two pixels. When called without an
> argument, returns the current setting. When called with one, sets it.
>
> Defaults to 5.
## Dependencies ## Dependencies

@ -18,28 +18,29 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
void LEDChaseEffect::update(void) { void LEDChaseEffect::update(void) {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return; return;
// Check to see if it's time to change the positions of the red and blue lights uint16_t now = Kaleidoscope.millisAtCycleStart();
if (current_chase_counter++ < chase_threshold) { if ((now - last_update_) < update_delay_) {
return; return;
} }
current_chase_counter = 0; last_update_ = now;
// The red LED is at `pos`; the blue one follows behind. `chase_sign` is either +1 or // The red LED is at `pos_`; the blue one follows behind. `direction_` is
// -1; `chase_pixels` is the gap between them. // either +1 or -1; `distance_` is the gap between them.
int8_t pos2 = pos - (chase_sign * chase_pixels); int8_t pos2 = pos_ - (direction_ * distance_);
// First, we turn off the LEDs that were turned on in the previous update. `pos` is // First, we turn off the LEDs that were turned on in the previous update.
// always in the valid range (0 <= pos < LED_COUNT), but after it changes direction, for // `pos_` is always in the valid range (0 <= pos_ < LED_COUNT), but after it
// the first few updates, `pos2` will be out of bounds. Since it's an unsigned integer, // changes direction, for the first few updates, `pos2` will be out of bounds.
// even when it would have a value below zero, it underflows and so one test is good for // Since it's an unsigned integer, even when it would have a value below zero,
// both ends of the range. // it underflows and so one test is good for both ends of the range.
::LEDControl.setCrgbAt(pos, {0, 0, 0}); ::LEDControl.setCrgbAt(pos_, CRGB(0, 0, 0));
if (pos2 < LED_COUNT) if (pos2 < LED_COUNT)
::LEDControl.setCrgbAt(pos2, {0, 0, 0}); ::LEDControl.setCrgbAt(pos2, CRGB(0, 0, 0));
// Next, we adjust the red light's position. If the direction hasn't changed (the red // Next, we adjust the red light's position. If the direction hasn't changed (the red
// light isn't out of bounds), we also adjust the blue light's position to match the red // light isn't out of bounds), we also adjust the blue light's position to match the red
@ -47,20 +48,20 @@ void LEDChaseEffect::update(void) {
// it back in bounds. When this happens, the blue light "jumps" behind the red one, and // it back in bounds. When this happens, the blue light "jumps" behind the red one, and
// will be out of bounds. The simplest way to do this is to assign it a value that is // will be out of bounds. The simplest way to do this is to assign it a value that is
// known to be invalid (LED_COUNT). // known to be invalid (LED_COUNT).
pos += chase_sign; pos_ += direction_;
if (pos < LED_COUNT && pos > 0) { if (pos_ < LED_COUNT && pos_ > 0) {
pos2 += chase_sign; pos2 += direction_;
} else { } else {
chase_sign = -chase_sign; direction_ = -direction_;
pos += chase_sign; pos_ += direction_;
pos2 = LED_COUNT; pos2 = LED_COUNT;
} }
// Last, we turn on the LEDs at their new positions. As before, the blue light (pos2) is // Last, we turn on the LEDs at their new positions. As before, the blue light (pos2) is
// only set if it's in the valid LED range. // only set if it's in the valid LED range.
::LEDControl.setCrgbAt(pos, {0, 0, 255}); ::LEDControl.setCrgbAt(pos_, CRGB(255, 0, 0));
if (pos2 < LED_COUNT) if (pos2 < LED_COUNT)
::LEDControl.setCrgbAt(pos2, {255, 0, 0}); ::LEDControl.setCrgbAt(pos2, CRGB(0, 0, 255));
} }
} }

@ -24,15 +24,28 @@ class LEDChaseEffect : public LEDMode {
public: public:
LEDChaseEffect(void) {} LEDChaseEffect(void) {}
uint16_t update_delay() {
return update_delay_;
}
void update_delay(uint16_t delay) {
update_delay_ = delay;
}
uint8_t distance() {
return distance_;
}
void distance(uint8_t value) {
distance_ = value;
}
protected: protected:
void update(void) final; void update(void) final;
private: private:
int8_t pos = 0; int8_t pos_ = 0;
int8_t chase_sign = 1; //negative values when it's going backwar int8_t direction_ = 1;
uint8_t chase_pixels = 5; uint8_t distance_ = 5;
uint8_t current_chase_counter = 0; uint16_t update_delay_ = 150;
static const uint8_t chase_threshold = 150; uint16_t last_update_ = 0;
}; };
} }
} }

Loading…
Cancel
Save