From 88abeacd5328ca86dd35b094fb43cbf3a17e3c6c Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 24 Jan 2019 14:29:19 +0100 Subject: [PATCH] Chase: Use timers for timing instead of counting cycles Counting cycles is an incredibly unreliable way of timing animations. Use timers instead. Fixes #545. Signed-off-by: Gergely Nagy --- src/kaleidoscope/plugin/LEDEffect-Chase.cpp | 7 ++++--- src/kaleidoscope/plugin/LEDEffect-Chase.h | 11 +++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/kaleidoscope/plugin/LEDEffect-Chase.cpp b/src/kaleidoscope/plugin/LEDEffect-Chase.cpp index 5a80cf56..b9a4e13e 100644 --- a/src/kaleidoscope/plugin/LEDEffect-Chase.cpp +++ b/src/kaleidoscope/plugin/LEDEffect-Chase.cpp @@ -18,15 +18,16 @@ namespace kaleidoscope { namespace plugin { + void LEDChaseEffect::update(void) { if (!Kaleidoscope.has_leds) return; - // Check to see if it's time to change the positions of the red and blue lights - if (current_chase_counter++ < chase_threshold) { + uint16_t now = Kaleidoscope.millisAtCycleStart(); + if ((now - last_update_) < update_delay_) { 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 // -1; `chase_pixels` is the gap between them. diff --git a/src/kaleidoscope/plugin/LEDEffect-Chase.h b/src/kaleidoscope/plugin/LEDEffect-Chase.h index 9aea1c3d..d7f885ae 100644 --- a/src/kaleidoscope/plugin/LEDEffect-Chase.h +++ b/src/kaleidoscope/plugin/LEDEffect-Chase.h @@ -24,6 +24,13 @@ class LEDChaseEffect : public LEDMode { public: LEDChaseEffect(void) {} + uint16_t update_delay() { + return update_delay_; + } + void update_delay(uint16_t delay) { + update_delay_ = delay; + } + protected: void update(void) final; @@ -31,8 +38,8 @@ class LEDChaseEffect : public LEDMode { int8_t pos = 0; int8_t chase_sign = 1; //negative values when it's going backwar uint8_t chase_pixels = 5; - uint8_t current_chase_counter = 0; - static const uint8_t chase_threshold = 150; + uint16_t update_delay_ = 150; + uint16_t last_update_ = 0; }; } }