Manage animations via a central controller

This commit is contained in:
Thomas Kolb 2019-12-02 23:06:04 +01:00
parent 862ad18d24
commit e4a8c86180
3 changed files with 55 additions and 5 deletions

View File

@ -0,0 +1,21 @@
#pragma once
#include <memory>
#include "Animation.h"
class AnimationController
{
public:
AnimationController(Fader *fader);
void setAnimation(std::unique_ptr<Animation> anim, bool transition = true);
void loop(void);
private:
Fader *m_fader;
std::unique_ptr<Animation> m_animation;
uint32_t m_transitionTime;
uint64_t m_frame;
};

View File

@ -0,0 +1,30 @@
#include "Animation/AnimationController.h"
AnimationController::AnimationController(Fader *fader)
: m_fader(fader), m_transitionTime(0), m_frame(0)
{}
void AnimationController::setAnimation(std::unique_ptr<Animation> anim, bool transition)
{
m_frame = 0;
m_animation = std::move(anim);
if(transition) {
m_transitionTime = 256/8;
m_fader->set_fadestep(8);
m_fader->fade_color(Fader::Color{0, 0, 0, 0});
}
}
void AnimationController::loop(void)
{
if(m_transitionTime > 0) {
m_transitionTime--;
} else if(m_animation) {
m_animation->loop(m_frame);
}
m_frame++;
}

View File

@ -13,6 +13,7 @@
#include "Config.h"
#include "Animation/AllAnimations.h"
#include "Animation/AnimationController.h"
#include <esp32_digital_led_lib.h>
#include <esp32_digital_led_funcs.h>
@ -35,7 +36,7 @@ Fader ledFader(NUM_STRIPS, NUM_LEDS, 1, FLIP_STRIPS_MASK);
UDPProto udpProto(&ledFader);
UpdateServer *updateServer;
std::unique_ptr<Animation> currentAnimation = nullptr;
AnimationController animController(&ledFader);
bool initLEDs()
{
@ -93,9 +94,7 @@ static void ledTask( void * parameter )
uint32_t start_time = micros();
if(currentAnimation) {
currentAnimation->loop(frame);
}
animController.loop();
if((WiFi.status() == WL_CONNECTED) || (WiFi.getMode() == WIFI_MODE_AP)) {
udpProto.loop();
@ -210,7 +209,7 @@ void setup()
Serial.println();
Serial.println();
currentAnimation = std::unique_ptr<Animation>(new ConnectingAnimation(&ledFader));
animController.setAnimation(std::unique_ptr<Animation>(new ConnectingAnimation(&ledFader)));
xTaskCreate(
ledTask, /* Task function. */