Skip to content

Commit

Permalink
Add actions to animation
Browse files Browse the repository at this point in the history
Add actions to be able to easily change the current frame to the animation
component.
  • Loading branch information
guillempages committed Jun 17, 2023
1 parent 1a7f121 commit 9a2dbc7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
45 changes: 43 additions & 2 deletions esphome/components/animation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from esphome import core
from esphome import automation, core
from esphome.components import display, font
import esphome.components.image as espImage
from esphome.components.image import CONF_USE_TRANSPARENCY
Expand All @@ -18,15 +18,28 @@

_LOGGER = logging.getLogger(__name__)

CODEOWNERS = ["@syndlex"]
DEPENDENCIES = ["display"]
MULTI_CONF = True

CONF_LOOP = "loop"
CONF_START_FRAME = "start_frame"
CONF_END_FRAME = "end_frame"
CONF_FRAME = "frame"

Animation_ = display.display_ns.class_("Animation", espImage.Image_)

# Actions
NextFrameAction = display.display_ns.class_(
"AnimationNextFrameAction", automation.Action, cg.Parented.template(Animation_)
)
PrevFrameAction = display.display_ns.class_(
"AnimationPrevFrameAction", automation.Action, cg.Parented.template(Animation_)
)
SetFrameAction = display.display_ns.class_(
"AnimationSetFrameAction", automation.Action, cg.Parented.template(Animation_)
)


def validate_cross_dependencies(config):
"""
Expand Down Expand Up @@ -74,7 +87,35 @@ def validate_cross_dependencies(config):

CONFIG_SCHEMA = cv.All(font.validate_pillow_installed, ANIMATION_SCHEMA)

CODEOWNERS = ["@syndlex"]
NEXT_FRAME_SCHEMA = automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(Animation_),
}
)
PREV_FRAME_SCHEMA = automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(Animation_),
}
)
SET_FRAME_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.use_id(Animation_),
cv.Required(CONF_FRAME): cv.positive_int,
}
)


@automation.register_action("animation.next_frame", NextFrameAction, NEXT_FRAME_SCHEMA)
@automation.register_action("animation.prev_frame", PrevFrameAction, PREV_FRAME_SCHEMA)
@automation.register_action("animation.set_frame", SetFrameAction, SET_FRAME_SCHEMA)
async def animation_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)

if CONF_FRAME in config:
template_ = await cg.templatable(config[CONF_FRAME], args, cg.uint16)
cg.add(var.set_frame(template_))
return var


async def to_code(config):
Expand Down
1 change: 1 addition & 0 deletions esphome/components/display/animation.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "animation.h"

#include "esphome/core/hal.h"
#include "esphome/core/log.h"

namespace esphome {
namespace display {
Expand Down
27 changes: 27 additions & 0 deletions esphome/components/display/animation.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "image.h"

#include "esphome/core/automation.h"

namespace esphome {
namespace display {

Expand Down Expand Up @@ -33,5 +35,30 @@ class Animation : public Image {
int loop_current_iteration_;
};

template<typename... Ts> class AnimationNextFrameAction : public Action<Ts...> {
public:
AnimationNextFrameAction(Animation *parent) : parent_(parent) {}
void play(Ts... x) override { this->parent_->next_frame(); }
protected:
Animation *parent_;
};

template<typename... Ts> class AnimationPrevFrameAction : public Action<Ts...> {
public:
AnimationPrevFrameAction(Animation *parent) : parent_(parent) {}
void play(Ts... x) override { this->parent_->prev_frame(); }
protected:
Animation *parent_;
};

template<typename... Ts> class AnimationSetFrameAction : public Action<Ts...> {
public:
AnimationSetFrameAction(Animation *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(uint16_t, frame)
void play(Ts... x) override { this->parent_->set_frame(this->frame_.value(x...)); }
protected:
Animation *parent_;
};

} // namespace display
} // namespace esphome

0 comments on commit 9a2dbc7

Please sign in to comment.