diff --git a/esphome/components/lvgl/automation.py b/esphome/components/lvgl/automation.py index a987cf4097..7db6e1f045 100644 --- a/esphome/components/lvgl/automation.py +++ b/esphome/components/lvgl/automation.py @@ -4,7 +4,7 @@ from esphome import automation import esphome.codegen as cg import esphome.config_validation as cv from esphome.const import CONF_ACTION, CONF_GROUP, CONF_ID, CONF_TIMEOUT -from esphome.cpp_generator import get_variable +from esphome.cpp_generator import TemplateArguments, get_variable from esphome.cpp_types import nullptr from .defines import ( @@ -23,6 +23,7 @@ from .lvcode import ( UPDATE_EVENT, LambdaContext, LocalVariable, + LvglComponent, ReturnStatement, add_line_marks, lv, @@ -93,7 +94,11 @@ async def lvgl_is_paused(config, condition_id, template_arg, args): lvgl = config[CONF_LVGL_ID] async with LambdaContext(LVGL_COMP_ARG, return_type=cg.bool_) as context: lv_add(ReturnStatement(lvgl_comp.is_paused())) - var = cg.new_Pvariable(condition_id, template_arg, await context.get_lambda()) + var = cg.new_Pvariable( + condition_id, + TemplateArguments(LvglComponent, *template_arg), + await context.get_lambda(), + ) await cg.register_parented(var, lvgl) return var @@ -114,7 +119,11 @@ async def lvgl_is_idle(config, condition_id, template_arg, args): timeout = await cg.templatable(config[CONF_TIMEOUT], [], cg.uint32) async with LambdaContext(LVGL_COMP_ARG, return_type=cg.bool_) as context: lv_add(ReturnStatement(lvgl_comp.is_idle(timeout))) - var = cg.new_Pvariable(condition_id, template_arg, await context.get_lambda()) + var = cg.new_Pvariable( + condition_id, + TemplateArguments(LvglComponent, *template_arg), + await context.get_lambda(), + ) await cg.register_parented(var, lvgl) return var diff --git a/esphome/components/lvgl/lvgl_esphome.cpp b/esphome/components/lvgl/lvgl_esphome.cpp index 61bdfe9755..5abeead9d8 100644 --- a/esphome/components/lvgl/lvgl_esphome.cpp +++ b/esphome/components/lvgl/lvgl_esphome.cpp @@ -119,6 +119,7 @@ void LvglComponent::add_event_cb(lv_obj_t *obj, event_callback_t callback, lv_ev } void LvglComponent::add_page(LvPageType *page) { this->pages_.push_back(page); + page->set_parent(this); page->setup(this->pages_.size() - 1); } void LvglComponent::show_page(size_t index, lv_scr_load_anim_t anim, uint32_t time) { @@ -143,6 +144,8 @@ void LvglComponent::show_prev_page(lv_scr_load_anim_t anim, uint32_t time) { } while (this->pages_[this->current_page_]->skip); // skip empty pages() this->show_page(this->current_page_, anim, time); } +size_t LvglComponent::get_current_page() const { return this->current_page_; } +bool LvPageType::is_showing() const { return this->parent_->get_current_page() == this->index; } void LvglComponent::draw_buffer_(const lv_area_t *area, lv_color_t *ptr) { auto width = lv_area_get_width(area); auto height = lv_area_get_height(area); diff --git a/esphome/components/lvgl/lvgl_esphome.h b/esphome/components/lvgl/lvgl_esphome.h index 8e89b02db9..69fa808d53 100644 --- a/esphome/components/lvgl/lvgl_esphome.h +++ b/esphome/components/lvgl/lvgl_esphome.h @@ -94,7 +94,9 @@ class LvCompound { lv_obj_t *obj{}; }; -class LvPageType { +class LvglComponent; + +class LvPageType : public Parented { public: LvPageType(bool skip) : skip(skip) {} @@ -102,6 +104,9 @@ class LvPageType { this->index = index; this->obj = lv_obj_create(nullptr); } + + bool is_showing() const; + lv_obj_t *obj{}; size_t index{}; bool skip; @@ -188,6 +193,7 @@ class LvglComponent : public PollingComponent { void show_next_page(lv_scr_load_anim_t anim, uint32_t time); void show_prev_page(lv_scr_load_anim_t anim, uint32_t time); void set_page_wrap(bool wrap) { this->page_wrap_ = wrap; } + size_t get_current_page() const; void set_focus_mark(lv_group_t *group) { this->focus_marks_[group] = lv_group_get_focused(group); } void restore_focus_mark(lv_group_t *group) { auto *mark = this->focus_marks_[group]; @@ -251,14 +257,13 @@ template class LvglAction : public Action, public Parente std::function action_{}; }; -template class LvglCondition : public Condition, public Parented { +template class LvglCondition : public Condition, public Parented { public: - LvglCondition(std::function &&condition_lambda) - : condition_lambda_(std::move(condition_lambda)) {} + LvglCondition(std::function &&condition_lambda) : condition_lambda_(std::move(condition_lambda)) {} bool check(Ts... x) override { return this->condition_lambda_(this->parent_); } protected: - std::function condition_lambda_{}; + std::function condition_lambda_{}; }; #ifdef USE_LVGL_TOUCHSCREEN diff --git a/esphome/components/lvgl/widgets/page.py b/esphome/components/lvgl/widgets/page.py index a754a9cb9a..23c162e010 100644 --- a/esphome/components/lvgl/widgets/page.py +++ b/esphome/components/lvgl/widgets/page.py @@ -2,6 +2,7 @@ from esphome import automation, codegen as cg from esphome.automation import Trigger import esphome.config_validation as cv from esphome.const import CONF_ID, CONF_PAGES, CONF_TIME, CONF_TRIGGER_ID +from esphome.cpp_generator import MockObj, TemplateArguments from ..defines import ( CONF_ANIMATION, @@ -17,18 +18,28 @@ from ..lvcode import ( EVENT_ARG, LVGL_COMP_ARG, LambdaContext, + ReturnStatement, add_line_marks, lv_add, lvgl_comp, lvgl_static, ) from ..schemas import LVGL_SCHEMA -from ..types import LvglAction, lv_page_t -from . import Widget, WidgetType, add_widgets, get_widgets, set_obj_properties +from ..types import LvglAction, LvglCondition, lv_page_t +from . import ( + Widget, + WidgetType, + add_widgets, + get_widgets, + set_obj_properties, + wait_for_widgets, +) CONF_ON_LOAD = "on_load" CONF_ON_UNLOAD = "on_unload" +PAGE_ARG = "_page" + PAGE_SCHEMA = cv.Schema( { cv.Optional(CONF_SKIP, default=False): lv_bool, @@ -86,6 +97,30 @@ async def page_next_to_code(config, action_id, template_arg, args): return var +@automation.register_condition( + "lvgl.page.is_showing", + LvglCondition, + cv.maybe_simple_value( + cv.Schema({cv.Required(CONF_ID): cv.use_id(lv_page_t)}), + key=CONF_ID, + ), +) +async def page_is_showing_to_code(config, condition_id, template_arg, args): + await wait_for_widgets() + page = await cg.get_variable(config[CONF_ID]) + async with LambdaContext( + [(lv_page_t.operator("ptr"), PAGE_ARG)], return_type=cg.bool_ + ) as context: + lv_add(ReturnStatement(MockObj(PAGE_ARG, "->").is_showing())) + var = cg.new_Pvariable( + condition_id, + TemplateArguments(lv_page_t, *template_arg), + await context.get_lambda(), + ) + await cg.register_parented(var, page) + return var + + @automation.register_action( "lvgl.page.previous", LvglAction, diff --git a/tests/components/lvgl/lvgl-package.yaml b/tests/components/lvgl/lvgl-package.yaml index 512045d748..234fd78678 100644 --- a/tests/components/lvgl/lvgl-package.yaml +++ b/tests/components/lvgl/lvgl-package.yaml @@ -138,6 +138,11 @@ lvgl: - logger.log: page loaded - lvgl.widget.focus: action: restore + - if: + condition: + lvgl.page.is_showing: page1 + then: + logger.log: "Yes, page1 showing" on_unload: - logger.log: page unloaded - lvgl.widget.focus: mark