From ddafb7380380d2f5ae15ada2a972c1ab4e0ae25f Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Fri, 1 Mar 2019 15:23:35 -0600 Subject: [PATCH] Speed up LED-Stalker plugin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The LED-Stalker plugin was looping through its table of `step` values (one for each key) on every call to `update()`, and calling `compute()` on every value there, but those table entries were only updated once every 50ms (by default), resulting in a lot of repeated computation. This resulted in the mean idle cycle time increasing from 565µs to 1317µs on the Model01 with an (almost) unmodified standard sketch. This change moves the check for the timeout before the loop through the `step` values, and aborts processing there if not enough time has elapsed. The resulting mean idle cycle time becomes 577µs -- an almost negligible increase. It also reduces the PROGMEM footprint by 44 bytes. Signed-off-by: Michael Richters --- src/kaleidoscope/plugin/LED-Stalker.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/kaleidoscope/plugin/LED-Stalker.cpp b/src/kaleidoscope/plugin/LED-Stalker.cpp index df4e2e0c..fb1c83c9 100644 --- a/src/kaleidoscope/plugin/LED-Stalker.cpp +++ b/src/kaleidoscope/plugin/LED-Stalker.cpp @@ -53,8 +53,9 @@ void StalkerEffect::update(void) { if (!variant) return; - uint16_t now = millis(); uint16_t elapsed = Kaleidoscope.millisAtCycleStart() - step_start_time_; + if (elapsed < step_length) + return; for (byte r = 0; r < ROWS; r++) { for (byte c = 0; c < COLS; c++) { @@ -63,17 +64,14 @@ void StalkerEffect::update(void) { ::LEDControl.setCrgbAt(r, c, variant->compute(&step)); } - if (elapsed > step_length) { - map_[r][c] = step; - } + map_[r][c] = step; if (!map_[r][c]) ::LEDControl.setCrgbAt(r, c, inactive_color); } } - if (elapsed > step_length) - step_start_time_ = Kaleidoscope.millisAtCycleStart(); + step_start_time_ = Kaleidoscope.millisAtCycleStart(); } namespace stalker {