Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add actions to animation #4959

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.uint16_t,
}
)


@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
30 changes: 30 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,33 @@ 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