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.
This commit is contained in:
Fabian Schlenz 2019-10-01 06:29:32 +02:00
parent 096d13438a
commit 382631d7d7
44 changed files with 78 additions and 67 deletions

View File

@ -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; };

View File

@ -4,6 +4,6 @@
class AnalogClockEffect : public Effect {
public:
void loop();
void loop(uint16_t ms);
String get_name() override { return "analog_clock"; }
};

View File

@ -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;
};

View File

@ -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"; }
};

View File

@ -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"; }
};

View File

@ -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"; }
};

View File

@ -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;
};

View File

@ -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"; }
};

View File

@ -17,5 +17,5 @@ public:
boolean clock_as_mask();
String get_name() override;
void loop();
void loop(uint16_t ms);
};

View File

@ -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"; }
};

View File

@ -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"; }

View File

@ -15,6 +15,6 @@ private:
public:
FireEffect();
~FireEffect();
void loop();
void loop(uint16_t ms);
String get_name() override { return "fire"; }
};

View File

@ -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"; }
};

View File

@ -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"; }
};

View File

@ -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"; }
};

View File

@ -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"; }
};

View File

@ -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"; }
};

View File

@ -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"; }
};

View File

@ -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"; }
};

View File

@ -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();

View File

@ -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"; }
};

View File

@ -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"; }

View File

@ -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);

View File

@ -20,7 +20,7 @@ AnimationEffect::~AnimationEffect() {
delete this->animation;
}
void AnimationEffect::loop() {
void AnimationEffect::loop(uint16_t ms) {
this->animation->drawFrame();
this->animation->advance();
}

View File

@ -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++) {

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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; i<EFFECT_CONFETTI_PIXELS_PER_LOOP; i++) {
CRGB color = _getColor();

View File

@ -41,9 +41,9 @@ boolean CycleEffect::clock_as_mask() {
return effect->clock_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) {

View File

@ -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;

View File

@ -10,7 +10,7 @@ void SingleDynamicEffect::init() {
for (int i=0; i<tile_count; i++) tiles[i] = CHSV(baseHue + random8(64), 180, 255);
}
void SingleDynamicEffect::loop() {
void SingleDynamicEffect::loop(uint16_t ms) {
EVERY_N_MILLISECONDS( EFFECT_SINGLE_DYNAMIC_LOOP_TIME ) {
tiles[random8(tile_count)] = CHSV(baseHue + random8(64), 180, 255);
}
@ -28,7 +28,7 @@ boolean SingleDynamicEffect::can_be_shown_with_clock() {
return true;
}
void MultiDynamicEffect::loop() {
void MultiDynamicEffect::loop(uint16_t ms) {
EVERY_N_MILLISECONDS( EFFECT_MULTI_DYNAMIC_LOOP_TIME ) {
for (int i=0; i<tile_count; i++) tiles[i] = CHSV(baseHue + random8(64), 180, 255);
}
@ -39,7 +39,7 @@ BigDynamicEffect::~BigDynamicEffect() {
delete window;
}
void BigDynamicEffect::loop() {
void BigDynamicEffect::loop(uint16_t ms) {
EVERY_N_MILLISECONDS( EFFECT_BIG_DYNAMIC_LOOP_TIME ) {
uint8_t x = random8(0, window->width - EFFECT_BIG_DYNAMIC_SIZE + 1);
uint8_t y = random8(0, window->height - EFFECT_BIG_DYNAMIC_SIZE + 1);

View File

@ -14,7 +14,7 @@ FireEffect::~FireEffect() {
delete [] this->data;
}
void FireEffect::loop() {
void FireEffect::loop(uint16_t ms) {
cooldown();
spark();
propagate();

View File

@ -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();

View File

@ -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();

View File

@ -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;

View File

@ -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; i<window->width; i++) _columns[i]->loop();
for (int i=0; i<window->width; i++) _columns[i]->loop(ms);
}

View File

@ -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

View File

@ -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 );

View File

@ -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; i<EFFECT_SINES_COUNT; i++) {
_sinus[i]->loop();
_sinus[i]->loop(ms);
}
}

View File

@ -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();

View File

@ -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);
}

View File

@ -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; x<window->width; x++) for (int y=0; y<window->height; y++) {

View File

@ -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());