From 382631d7d775565b25fa34a217996af733076101 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Tue, 1 Oct 2019 06:29:32 +0200 Subject: [PATCH] Effect#loop now gets the time since the last run of the loop in ms. This enables the effects to show animations that stay fluid independent of the current frame rate. --- include/Effect.h | 2 +- include/effect_analogclock.h | 2 +- include/effect_animation.h | 2 +- include/effect_bell.h | 2 +- include/effect_big_clock.h | 2 +- include/effect_blur2d.h | 2 +- include/effect_clock.h | 4 ++-- include/effect_confetti.h | 2 +- include/effect_cycle.h | 2 +- include/effect_dvd.h | 2 +- include/effect_dynamic.h | 6 +++--- include/effect_fire.h | 2 +- include/effect_firework.h | 2 +- include/effect_gol.h | 2 +- include/effect_marquee.h | 2 +- include/effect_matrix.h | 6 +++--- include/effect_pixelclock.h | 2 +- include/effect_sinematrix3.h | 2 +- include/effect_sines.h | 4 ++-- include/effect_snake.h | 2 +- include/effect_static.h | 2 +- include/effect_twirl.h | 2 +- src/effect_analogclock.cpp | 2 +- src/effect_animation.cpp | 2 +- src/effect_bell.cpp | 2 +- src/effect_big_clock.cpp | 2 +- src/effect_blur2d.cpp | 2 +- src/effect_clock.cpp | 4 ++-- src/effect_confetti.cpp | 2 +- src/effect_cycle.cpp | 4 ++-- src/effect_dvd.cpp | 2 +- src/effect_dynamic.cpp | 6 +++--- src/effect_fire.cpp | 2 +- src/effect_firework.cpp | 2 +- src/effect_gol.cpp | 2 +- src/effect_marquee.cpp | 3 +-- src/effect_matrix.cpp | 18 +++++++++--------- src/effect_pixelclock.cpp | 2 +- src/effect_sinematrix3.cpp | 2 +- src/effect_sines.cpp | 6 +++--- src/effect_snake.cpp | 2 +- src/effect_static.cpp | 2 +- src/effect_twirl.cpp | 2 +- src/pitrix.cpp | 18 +++++++++++++++--- 44 files changed, 78 insertions(+), 67 deletions(-) diff --git a/include/Effect.h b/include/Effect.h index 00fc48f..4940a64 100644 --- a/include/Effect.h +++ b/include/Effect.h @@ -9,7 +9,7 @@ protected: Window* window = Window::getFullWindow(); // Use a full screen window per default. public: virtual ~Effect() {}; - virtual void loop() = 0; + virtual void loop(uint16_t ms) = 0; virtual String get_name() = 0; boolean supports_window = false; virtual boolean can_be_shown_with_clock() { return false; }; diff --git a/include/effect_analogclock.h b/include/effect_analogclock.h index b6bb04a..bbe21d8 100644 --- a/include/effect_analogclock.h +++ b/include/effect_analogclock.h @@ -4,6 +4,6 @@ class AnalogClockEffect : public Effect { public: - void loop(); + void loop(uint16_t ms); String get_name() override { return "analog_clock"; } }; diff --git a/include/effect_animation.h b/include/effect_animation.h index 55e208b..82d91db 100644 --- a/include/effect_animation.h +++ b/include/effect_animation.h @@ -17,6 +17,6 @@ public: AnimationEffect(const char* name, uint32_t bg_color, int x, int y); ~AnimationEffect(); AnimationEffect* setFgColor(uint32_t c); - void loop(); + void loop(uint16_t ms); String get_name() override; }; diff --git a/include/effect_bell.h b/include/effect_bell.h index 7612e5f..bfe77ef 100644 --- a/include/effect_bell.h +++ b/include/effect_bell.h @@ -9,7 +9,7 @@ private: CRGB color_off = CRGB(0x000000); boolean invert = false; public: - void loop(); + void loop(uint16_t ms); String get_name() override { return "bell"; } }; diff --git a/include/effect_big_clock.h b/include/effect_big_clock.h index dcf063d..adfc6ed 100644 --- a/include/effect_big_clock.h +++ b/include/effect_big_clock.h @@ -11,6 +11,6 @@ private: void _draw_border_pixel(uint8_t second, CRGB* color); public: - void loop(); + void loop(uint16_t ms); String get_name() override { return "big_clock"; } }; diff --git a/include/effect_blur2d.h b/include/effect_blur2d.h index 3f4b038..9dd03c2 100644 --- a/include/effect_blur2d.h +++ b/include/effect_blur2d.h @@ -11,7 +11,7 @@ public: ~Blur2DEffect(); boolean supports_window = true; boolean can_be_shown_with_clock(); - void loop(); + void loop(uint16_t ms); String get_name() override { return "blur2d"; } }; diff --git a/include/effect_clock.h b/include/effect_clock.h index 73e0f8d..770fc40 100644 --- a/include/effect_clock.h +++ b/include/effect_clock.h @@ -11,7 +11,7 @@ protected: public: ~ClockEffect(); - virtual void loop(); + virtual void loop(uint16_t ms); String get_name() override { return "clock"; } void loop_with_invert(bool invert); void loop(boolean invert, CRGB fg_color, CRGB bg_color, uint8_t y); @@ -20,5 +20,5 @@ public: class NightClockEffect : public ClockEffect { public: NightClockEffect(); - void loop() override; + void loop(uint16_t ms) override; }; diff --git a/include/effect_confetti.h b/include/effect_confetti.h index 87392d7..604d7f3 100644 --- a/include/effect_confetti.h +++ b/include/effect_confetti.h @@ -7,7 +7,7 @@ class ConfettiEffect : public Effect { protected: virtual CRGB _getColor(); public: - void loop(); + void loop(uint16_t ms); boolean can_be_shown_with_clock(); String get_name() override { return "confetti"; } }; diff --git a/include/effect_cycle.h b/include/effect_cycle.h index 96c0889..0f4dd22 100644 --- a/include/effect_cycle.h +++ b/include/effect_cycle.h @@ -17,5 +17,5 @@ public: boolean clock_as_mask(); String get_name() override; - void loop(); + void loop(uint16_t ms); }; diff --git a/include/effect_dvd.h b/include/effect_dvd.h index ca316c2..39fbbc5 100644 --- a/include/effect_dvd.h +++ b/include/effect_dvd.h @@ -12,7 +12,7 @@ private: public: DvdEffect(); ~DvdEffect(); - void loop() override; + void loop(uint16_t ms) override; bool can_be_shown_with_clock() override; String get_name() override { return "dvd"; } }; diff --git a/include/effect_dynamic.h b/include/effect_dynamic.h index cba8af2..3cc0d97 100644 --- a/include/effect_dynamic.h +++ b/include/effect_dynamic.h @@ -13,14 +13,14 @@ public: SingleDynamicEffect(); void init(); boolean can_be_shown_with_clock(); - virtual void loop(); + virtual void loop(uint16_t ms); void draw(); String get_name() override { return "single_dynamic"; } }; class MultiDynamicEffect : public SingleDynamicEffect { public: - void loop(); + void loop(uint16_t ms); String get_name() override { return "multi_dynamic"; } }; @@ -28,7 +28,7 @@ class BigDynamicEffect : public Effect { private: Window* window = new Window(0, 0, LED_WIDTH, LED_HEIGHT-6); public: - void loop(); + void loop(uint16_t ms); ~BigDynamicEffect(); boolean can_be_shown_with_clock() override; String get_name() override { return "big_dynamic"; } diff --git a/include/effect_fire.h b/include/effect_fire.h index 2b7bbd8..9784701 100644 --- a/include/effect_fire.h +++ b/include/effect_fire.h @@ -15,6 +15,6 @@ private: public: FireEffect(); ~FireEffect(); - void loop(); + void loop(uint16_t ms); String get_name() override { return "fire"; } }; diff --git a/include/effect_firework.h b/include/effect_firework.h index 26ea2e9..b7ee0a5 100644 --- a/include/effect_firework.h +++ b/include/effect_firework.h @@ -56,7 +56,7 @@ public: void skyburst(accum88 x, accum88 y, saccum78 xv, saccum78 yv, CRGB c); boolean supports_window = true; boolean can_be_shown_with_clock(); - void loop(); + void loop(uint16_t ms); String get_name() override { return "firework"; } }; diff --git a/include/effect_gol.h b/include/effect_gol.h index 57ff641..a40b428 100644 --- a/include/effect_gol.h +++ b/include/effect_gol.h @@ -17,7 +17,7 @@ private: public: GolEffect(); ~GolEffect(); - void loop(); + void loop(uint16_t ms); bool can_be_shown_with_clock(); String get_name() override { return "gol"; } }; diff --git a/include/effect_marquee.h b/include/effect_marquee.h index fa272c8..57c4f69 100644 --- a/include/effect_marquee.h +++ b/include/effect_marquee.h @@ -12,7 +12,7 @@ private: public: boolean supports_window = true; boolean can_be_shown_with_clock(); - void loop(); + void loop(uint16_t ms); void apply_option(String key, String value) override; String get_name() override { return "marquee"; } }; diff --git a/include/effect_matrix.h b/include/effect_matrix.h index 783aee8..9105525 100644 --- a/include/effect_matrix.h +++ b/include/effect_matrix.h @@ -27,9 +27,9 @@ public: MatrixEffectColumn(Window* win, uint8_t direction=0, bool random_direction=false); virtual ~MatrixEffectColumn() {}; - void advance(); + void advance(uint16_t ms); void draw(); - void loop(); + void loop(uint16_t ms); }; class RainbowMatrixEffectColumn : public MatrixEffectColumn { @@ -55,7 +55,7 @@ public: boolean can_be_shown_with_clock(); MatrixEffect(); virtual ~MatrixEffect(); - void loop(); + void loop(uint16_t ms); String get_name() override { return "matrix"; } }; diff --git a/include/effect_pixelclock.h b/include/effect_pixelclock.h index a0e3082..9e8e1df 100644 --- a/include/effect_pixelclock.h +++ b/include/effect_pixelclock.h @@ -10,7 +10,7 @@ private: public: PixelClockEffect(); ~PixelClockEffect(); - void loop(); + void loop(uint16_t ms); bool can_be_shown_with_clock(); String get_name() override { return "pixel_clock"; } }; diff --git a/include/effect_sinematrix3.h b/include/effect_sinematrix3.h index ca59f2b..5eef7d7 100644 --- a/include/effect_sinematrix3.h +++ b/include/effect_sinematrix3.h @@ -30,7 +30,7 @@ public: boolean supports_window = true; boolean can_be_shown_with_clock(); boolean clock_as_mask(); - void loop(); + void loop(uint16_t ms); String get_name() override { return "sinematrix3"; } }; diff --git a/include/effect_sines.h b/include/effect_sines.h index fbcd4f0..b6407c9 100644 --- a/include/effect_sines.h +++ b/include/effect_sines.h @@ -15,7 +15,7 @@ private: CRGB _color; public: SinesEffectSinus(Window* w); - void loop(); + void loop(uint16_t ms); }; class SinesEffect : public Effect { @@ -27,7 +27,7 @@ public: ~SinesEffect(); boolean supports_window = true; boolean can_be_shown_with_clock(); - void loop(); + void loop(uint16_t ms); String get_name() override { return "sines"; } }; diff --git a/include/effect_snake.h b/include/effect_snake.h index 7e7cb3c..3529ea2 100644 --- a/include/effect_snake.h +++ b/include/effect_snake.h @@ -17,7 +17,7 @@ private: public: SnakeEffect(); ~SnakeEffect(); - void loop(); + void loop(uint16_t ms); boolean valid_position(Coords c); Coords update_position(Coords c, uint8_t direction); boolean can_be_shown_with_clock(); diff --git a/include/effect_static.h b/include/effect_static.h index 13be43d..c4b9557 100644 --- a/include/effect_static.h +++ b/include/effect_static.h @@ -9,7 +9,7 @@ private: public: StaticEffect(CRGB col); boolean supports_window = true; - void loop(); + void loop(uint16_t ms); String get_name() override { return "static"; } }; diff --git a/include/effect_twirl.h b/include/effect_twirl.h index 34437e8..be23e3c 100644 --- a/include/effect_twirl.h +++ b/include/effect_twirl.h @@ -10,7 +10,7 @@ private: double _real_center_x = LED_WIDTH / 2; double _real_center_y = LED_HEIGHT / 2; public: - void loop(); + void loop(uint16_t ms); boolean can_be_shown_with_clock() override; boolean clock_as_mask() override; String get_name() override { return "twirl"; } diff --git a/src/effect_analogclock.cpp b/src/effect_analogclock.cpp index 4e91e2a..0d7f729 100644 --- a/src/effect_analogclock.cpp +++ b/src/effect_analogclock.cpp @@ -2,7 +2,7 @@ #include "my_fastled.h" #include "ntp.h" -void AnalogClockEffect::loop() { +void AnalogClockEffect::loop(uint16_t ms) { window->clear(); CRGB white(0xFFFFFF); CRGB red(0xFF0000); diff --git a/src/effect_animation.cpp b/src/effect_animation.cpp index 40b056c..cb4c8a3 100644 --- a/src/effect_animation.cpp +++ b/src/effect_animation.cpp @@ -20,7 +20,7 @@ AnimationEffect::~AnimationEffect() { delete this->animation; } -void AnimationEffect::loop() { +void AnimationEffect::loop(uint16_t ms) { this->animation->drawFrame(); this->animation->advance(); } diff --git a/src/effect_bell.cpp b/src/effect_bell.cpp index e37e1d2..ea2336b 100644 --- a/src/effect_bell.cpp +++ b/src/effect_bell.cpp @@ -3,7 +3,7 @@ #include "effect_bell.h" #include "sprites.h" -void BellEffect::loop() { +void BellEffect::loop(uint16_t ms) { Serial.println("This is Bell.loop()"); for (int y = 0; y < 16; y++) { for (int x = 0; x < 2; x++) { diff --git a/src/effect_big_clock.cpp b/src/effect_big_clock.cpp index c95a992..727c808 100644 --- a/src/effect_big_clock.cpp +++ b/src/effect_big_clock.cpp @@ -3,7 +3,7 @@ #include "fonts.h" #include "ntp.h" -void BigClockEffect::loop() { +void BigClockEffect::loop(uint16_t ms) { window->clear(); uint8_t h = ntpClient.getHours(); window->drawChar(&font_numbers3x5_blocky, 6<<8, 2<<8, '0' + (h / 10), &_color_font); diff --git a/src/effect_blur2d.cpp b/src/effect_blur2d.cpp index bb02a21..27740e8 100644 --- a/src/effect_blur2d.cpp +++ b/src/effect_blur2d.cpp @@ -4,7 +4,7 @@ boolean Blur2DEffect::can_be_shown_with_clock() { return true; } -void Blur2DEffect::loop() { +void Blur2DEffect::loop(uint16_t ms) { uint8_t blur_amount = dim8_raw(beatsin8(3, 128, 224)); window->blur(blur_amount); diff --git a/src/effect_clock.cpp b/src/effect_clock.cpp index 9bb8474..2a90d1f 100644 --- a/src/effect_clock.cpp +++ b/src/effect_clock.cpp @@ -8,7 +8,7 @@ NightClockEffect::NightClockEffect() { window = Window::getFullWindow(); } -void NightClockEffect::loop() { +void NightClockEffect::loop(uint16_t ms) { uint16_t minutes = minutes16(); //uint8_t y = minutes % ((window->height - 5) * 2 - 2); //if (y > window->height - 5) y = 2*window->height - 2*y; @@ -16,7 +16,7 @@ void NightClockEffect::loop() { ClockEffect::loop(false, CRGB(0x200000), CRGB(0x000000), y); } -void ClockEffect::loop() { +void ClockEffect::loop(uint16_t ms) { loop_with_invert(false); } diff --git a/src/effect_confetti.cpp b/src/effect_confetti.cpp index e48cebf..e83dba4 100644 --- a/src/effect_confetti.cpp +++ b/src/effect_confetti.cpp @@ -3,7 +3,7 @@ #include "functions.h" #include "prototypes.h" -void ConfettiEffect::loop() { +void ConfettiEffect::loop(uint16_t ms) { window->fadeToBlackBy(3); for (int i=0; iclock_as_mask(); }; -void CycleEffect::loop() { +void CycleEffect::loop(uint16_t ms) { if (!effect) changeEffect(); // If this is the first run, we have to select an effect first! - effect->loop(); + effect->loop(ms); // Don't use EVERY_N_SECONDS(config_effect_cycle_time) here because that function isn't relly made // to be used with changing values. EVERY_N_SECONDS(1) { diff --git a/src/effect_dvd.cpp b/src/effect_dvd.cpp index ead0091..5f23079 100644 --- a/src/effect_dvd.cpp +++ b/src/effect_dvd.cpp @@ -1,7 +1,7 @@ #include "effect_dvd.h" #include "my_fastled.h" -void DvdEffect::loop() { +void DvdEffect::loop(uint16_t ms) { bool dir_changed = false; EVERY_N_MILLISECONDS( 250 ) { _x += _x_dir; diff --git a/src/effect_dynamic.cpp b/src/effect_dynamic.cpp index 9ce7068..ed02044 100644 --- a/src/effect_dynamic.cpp +++ b/src/effect_dynamic.cpp @@ -10,7 +10,7 @@ void SingleDynamicEffect::init() { for (int i=0; iwidth - EFFECT_BIG_DYNAMIC_SIZE + 1); uint8_t y = random8(0, window->height - EFFECT_BIG_DYNAMIC_SIZE + 1); diff --git a/src/effect_fire.cpp b/src/effect_fire.cpp index b1b553b..3db457b 100644 --- a/src/effect_fire.cpp +++ b/src/effect_fire.cpp @@ -14,7 +14,7 @@ FireEffect::~FireEffect() { delete [] this->data; } -void FireEffect::loop() { +void FireEffect::loop(uint16_t ms) { cooldown(); spark(); propagate(); diff --git a/src/effect_firework.cpp b/src/effect_firework.cpp index 9acb901..31c9e77 100644 --- a/src/effect_firework.cpp +++ b/src/effect_firework.cpp @@ -139,7 +139,7 @@ boolean FireworkEffect::can_be_shown_with_clock() { return true; } -void FireworkEffect::loop() { +void FireworkEffect::loop(uint16_t ms) { window->clear(); _dot->move(); _dot->draw(); diff --git a/src/effect_gol.cpp b/src/effect_gol.cpp index 51e3102..691cce7 100644 --- a/src/effect_gol.cpp +++ b/src/effect_gol.cpp @@ -29,7 +29,7 @@ GolEffect::~GolEffect() { delete window; } -void GolEffect::loop() { +void GolEffect::loop(uint16_t ms) { if (EFFECT_GOL_BLEND_SPEED + _blend > 255) { _blend = 0; _advance(); diff --git a/src/effect_marquee.cpp b/src/effect_marquee.cpp index 2eec8bd..81d84b6 100644 --- a/src/effect_marquee.cpp +++ b/src/effect_marquee.cpp @@ -5,8 +5,7 @@ boolean MarqueeEffect::can_be_shown_with_clock() { return true; } -void MarqueeEffect::loop() { - static int loop_counter = 0; +void MarqueeEffect::loop(uint16_t ms) { window->clear(); CRGB color = CHSV(0, 255, 255); uint16_t width = _text.length() * 6; diff --git a/src/effect_matrix.cpp b/src/effect_matrix.cpp index 381a14c..28ae6e3 100644 --- a/src/effect_matrix.cpp +++ b/src/effect_matrix.cpp @@ -43,22 +43,22 @@ void MatrixEffectColumn::restart(bool completely_random) { speed = random8(EFFECT_MATRIX_SPEED_MIN, EFFECT_MATRIX_SPEED_MAX); } -void MatrixEffectColumn::advance() { +void MatrixEffectColumn::advance(uint16_t ms) { switch(_direction) { case DIR_NORTH: - y-=speed; + y-=speed * ms; if ((y>>8) > window->height && (y>>8) + length > window->height) running=false; break; case DIR_EAST: - x+=speed; + x+=speed * ms; if ((x>>8) - length > window->width) running=false; break; case DIR_SOUTH: - y+=speed; + y+=speed * ms; if ((y>>8) - length > window->height) running=false; break; case DIR_WEST: - x-=speed; + x-=speed * ms; if ((x>>8) > window->width && (y>>8) + length > window->width) running=false; break; } @@ -79,14 +79,14 @@ void MatrixEffectColumn::draw() { } } -void MatrixEffectColumn::loop() { +void MatrixEffectColumn::loop(uint16_t ms) { if (!running) { if (random8() < 20) { // Start the column again. restart(false); } } else { - advance(); + advance(ms); draw(); } } @@ -160,7 +160,7 @@ MatrixEffect::~MatrixEffect() { delete[] _columns; } -void MatrixEffect::loop() { +void MatrixEffect::loop(uint16_t ms) { window->clear(); - for (int i=0; iwidth; i++) _columns[i]->loop(); + for (int i=0; iwidth; i++) _columns[i]->loop(ms); } diff --git a/src/effect_pixelclock.cpp b/src/effect_pixelclock.cpp index d7369ec..677c977 100644 --- a/src/effect_pixelclock.cpp +++ b/src/effect_pixelclock.cpp @@ -13,7 +13,7 @@ PixelClockEffect::~PixelClockEffect() { delete window; } -void PixelClockEffect::loop() { +void PixelClockEffect::loop(uint16_t ms) { uint8_t x, y; // Temporary variables for calculating positions window->clear(); // Seconds diff --git a/src/effect_sinematrix3.cpp b/src/effect_sinematrix3.cpp index 56d1082..709e282 100644 --- a/src/effect_sinematrix3.cpp +++ b/src/effect_sinematrix3.cpp @@ -5,7 +5,7 @@ boolean Sinematrix3Effect::can_be_shown_with_clock() { return true; }; boolean Sinematrix3Effect::clock_as_mask() { return true; }; -void Sinematrix3Effect::loop() { +void Sinematrix3Effect::loop(uint16_t ms) { pangle = addmodpi( pangle, 0.0133 + (angle / 256) ); angle = cos(pangle) * PI; sx = addmodpi( sx, 0.00673 ); diff --git a/src/effect_sines.cpp b/src/effect_sines.cpp index 69d145c..cebe57f 100644 --- a/src/effect_sines.cpp +++ b/src/effect_sines.cpp @@ -9,7 +9,7 @@ SinesEffectSinus::SinesEffectSinus(Window* w) { _color = CHSV(random8(), 255, 255); } -void SinesEffectSinus::loop() { +void SinesEffectSinus::loop(uint16_t ms) { _value += _frequency; if ((_value == 0 || _value==128) && random8(16)==0) { int8_t sign = _value == 0 ? -1 : +1; @@ -38,11 +38,11 @@ boolean SinesEffect::can_be_shown_with_clock() { return true; } -void SinesEffect::loop() { +void SinesEffect::loop(uint16_t ms) { // do stuff if (_step++ % 4) return; // Skip 3 out of 4 steps. window->shift_down_and_blur(); for (int i=0; iloop(); + _sinus[i]->loop(ms); } } diff --git a/src/effect_snake.cpp b/src/effect_snake.cpp index a44cd3e..1007d3e 100644 --- a/src/effect_snake.cpp +++ b/src/effect_snake.cpp @@ -10,7 +10,7 @@ SnakeEffect::~SnakeEffect() { delete window; } -void SnakeEffect::loop() { +void SnakeEffect::loop(uint16_t ms) { if (run++ % EFFECT_SNAKE_SLOWDOWN == 0) { // Change the coordinates only on every n-th run. if (random8(EFFECT_SNAKE_DIRECTION_CHANGE)==0 || is_turn_needed()) turn_random(); diff --git a/src/effect_static.cpp b/src/effect_static.cpp index 89fd688..9bf406e 100644 --- a/src/effect_static.cpp +++ b/src/effect_static.cpp @@ -6,7 +6,7 @@ StaticEffect::StaticEffect(CRGB col) { color = col; } -void StaticEffect::loop() { +void StaticEffect::loop(uint16_t ms) { EVERY_N_SECONDS(1) { window->clear(&color); } diff --git a/src/effect_twirl.cpp b/src/effect_twirl.cpp index aaa9065..794b324 100644 --- a/src/effect_twirl.cpp +++ b/src/effect_twirl.cpp @@ -4,7 +4,7 @@ boolean TwirlEffect::can_be_shown_with_clock() { return true; }; boolean TwirlEffect::clock_as_mask() { return true; }; -void TwirlEffect::loop() { +void TwirlEffect::loop(uint16_t ms) { double center_x = _real_center_x; // - (cos8(_center_offset_angle)>>6); double center_y = _real_center_y; // + (sin8(_center_offset_angle)>>6); for (int x=0; xwidth; x++) for (int y=0; yheight; y++) { diff --git a/src/pitrix.cpp b/src/pitrix.cpp index 9ade2a6..65b4e2c 100644 --- a/src/pitrix.cpp +++ b/src/pitrix.cpp @@ -16,6 +16,7 @@ int loop_timeouts = 0; long loop_started_at = 0; uint8_t baseHue = 0; // defined as extern in prototypes.h char hostname[30]; // defined as extern in prototypes.h +unsigned long _last_effect_loop_finished_at = 0; #ifdef RECORDER_ENABLE Recorder* recorder; #endif @@ -87,9 +88,20 @@ void loop() { } EVERY_N_MILLISECONDS(1000 / FPS) { - //LOGln("Core * loop running"); - current_effect->loop(); - //LOGln("Core * loop ran"); + // Calculate the delay since the last time loop() was called. + // This way, the effect can handle varying frame rates. + uint16_t last_loop_ago; + unsigned long now = millis(); + if (now > _last_effect_loop_finished_at && _last_effect_loop_finished_at) { + last_loop_ago = now - _last_effect_loop_finished_at; + } else { + last_loop_ago = 0; + } + + current_effect->loop(last_loop_ago); + + // Save the time for the next run. + _last_effect_loop_finished_at = now; if (current_effect->can_be_shown_with_clock()) { effect_clock.loop_with_invert(current_effect->clock_as_mask());