From 73d9997b5404c2565116e39df3606be54b9ca005 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 28 Apr 2018 06:07:43 +0200 Subject: [PATCH] Use timers instead of ticks to time when to update the effects Ticks depend on the speed of the main loop, and as such, are not a reliable way to time animations. For this reason, use proper timers instead. The update delay is set to 40ms, which appears to be a slow, relaxing animation, and should be roughly in the ballpark the tick-based timing was, before speeding up the main loop considerably. Fixes #3. Signed-off-by: Gergely Nagy --- src/Kaleidoscope-LEDEffect-Rainbow.cpp | 10 ++++++---- src/Kaleidoscope-LEDEffect-Rainbow.h | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Kaleidoscope-LEDEffect-Rainbow.cpp b/src/Kaleidoscope-LEDEffect-Rainbow.cpp index 2de7618e..5fdbcc5f 100644 --- a/src/Kaleidoscope-LEDEffect-Rainbow.cpp +++ b/src/Kaleidoscope-LEDEffect-Rainbow.cpp @@ -3,10 +3,11 @@ namespace kaleidoscope { void LEDRainbowEffect::update(void) { - if (rainbow_current_ticks++ < rainbow_ticks) { + uint16_t now = millis(); + if ((now - rainbow_last_update) < rainbow_update_delay) { return; } else { - rainbow_current_ticks = 0; + rainbow_last_update = now; } cRGB rainbow = hsvToRgb(rainbow_hue, rainbow_saturation, rainbow_value); @@ -26,10 +27,11 @@ void LEDRainbowEffect::brightness(byte brightness) { // --------- void LEDRainbowWaveEffect::update(void) { - if (rainbow_current_ticks++ < rainbow_wave_ticks) { + uint16_t now = millis(); + if ((now - rainbow_last_update) < rainbow_update_delay) { return; } else { - rainbow_current_ticks = 0; + rainbow_last_update = now; } for (uint8_t i = 0; i < LED_COUNT; i++) { diff --git a/src/Kaleidoscope-LEDEffect-Rainbow.h b/src/Kaleidoscope-LEDEffect-Rainbow.h index 98307109..e3a66210 100644 --- a/src/Kaleidoscope-LEDEffect-Rainbow.h +++ b/src/Kaleidoscope-LEDEffect-Rainbow.h @@ -15,8 +15,8 @@ class LEDRainbowEffect : public LEDMode { uint16_t rainbow_hue = 0; // stores 0 to 614 uint8_t rainbow_steps = 1; // number of hues we skip in a 360 range per update - uint16_t rainbow_current_ticks = 0; - uint16_t rainbow_ticks = 10; // delays between update + uint16_t rainbow_last_update = 0; + uint16_t rainbow_update_delay = 40; // delay between updates (ms) byte rainbow_saturation = 255; byte rainbow_value = 50; @@ -34,8 +34,8 @@ class LEDRainbowWaveEffect : public LEDMode { uint16_t rainbow_hue = 0; // stores 0 to 614 uint8_t rainbow_wave_steps = 1; // number of hues we skip in a 360 range per update - uint16_t rainbow_current_ticks = 0; - uint16_t rainbow_wave_ticks = 10; // delays between update + uint16_t rainbow_last_update = 0; + uint16_t rainbow_update_delay = 40; // delay between updates (ms) byte rainbow_saturation = 255; byte rainbow_value = 50;