[lvgl] Add initial_focus for encoders (#7256)

This commit is contained in:
Clyde Stubbs 2024-08-13 18:06:01 +10:00 committed by GitHub
parent 506e69addf
commit 3598560472
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 29 additions and 9 deletions

View file

@ -23,7 +23,7 @@ from esphome.helpers import write_file_if_changed
from . import defines as df, helpers, lv_validation as lvalid from . import defines as df, helpers, lv_validation as lvalid
from .automation import disp_update, update_to_code from .automation import disp_update, update_to_code
from .defines import CONF_SKIP from .defines import CONF_SKIP
from .encoders import ENCODERS_CONFIG, encoders_to_code from .encoders import ENCODERS_CONFIG, encoders_to_code, initial_focus_to_code
from .lv_validation import lv_bool, lv_images_used from .lv_validation import lv_bool, lv_images_used
from .lvcode import LvContext, LvglComponent from .lvcode import LvContext, LvglComponent
from .schemas import ( from .schemas import (
@ -272,6 +272,7 @@ async def to_code(config):
templ = await cg.templatable(conf[CONF_TIMEOUT], [], cg.uint32) templ = await cg.templatable(conf[CONF_TIMEOUT], [], cg.uint32)
idle_trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], lv_component, templ) idle_trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], lv_component, templ)
await build_automation(idle_trigger, [], conf) await build_automation(idle_trigger, [], conf)
await initial_focus_to_code(config)
for comp in helpers.lvgl_components_required: for comp in helpers.lvgl_components_required:
CORE.add_define(f"USE_LVGL_{comp.upper()}") CORE.add_define(f"USE_LVGL_{comp.upper()}")

View file

@ -413,6 +413,7 @@ CONF_GRID_ROW_ALIGN = "grid_row_align"
CONF_GRID_ROWS = "grid_rows" CONF_GRID_ROWS = "grid_rows"
CONF_HEADER_MODE = "header_mode" CONF_HEADER_MODE = "header_mode"
CONF_HOME = "home" CONF_HOME = "home"
CONF_INITIAL_FOCUS = "initial_focus"
CONF_KEY_CODE = "key_code" CONF_KEY_CODE = "key_code"
CONF_LAYOUT = "layout" CONF_LAYOUT = "layout"
CONF_LEFT_BUTTON = "left_button" CONF_LEFT_BUTTON = "left_button"

View file

@ -8,6 +8,7 @@ from .defines import (
CONF_DEFAULT_GROUP, CONF_DEFAULT_GROUP,
CONF_ENCODERS, CONF_ENCODERS,
CONF_ENTER_BUTTON, CONF_ENTER_BUTTON,
CONF_INITIAL_FOCUS,
CONF_LEFT_BUTTON, CONF_LEFT_BUTTON,
CONF_LONG_PRESS_REPEAT_TIME, CONF_LONG_PRESS_REPEAT_TIME,
CONF_LONG_PRESS_TIME, CONF_LONG_PRESS_TIME,
@ -67,3 +68,10 @@ async def encoders_to_code(var, config):
else: else:
group = default_group group = default_group
lv.indev_set_group(lv_expr.indev_drv_register(listener.get_drv()), group) lv.indev_set_group(lv_expr.indev_drv_register(listener.get_drv()), group)
async def initial_focus_to_code(config):
for enc_conf in config[CONF_ENCODERS]:
if default_focus := enc_conf.get(CONF_INITIAL_FOCUS):
obj = await cg.get_variable(default_focus)
lv.group_focus_obj(obj)

View file

@ -14,11 +14,19 @@ from esphome.const import (
from esphome.core import TimePeriod from esphome.core import TimePeriod
from esphome.schema_extractors import SCHEMA_EXTRACT from esphome.schema_extractors import SCHEMA_EXTRACT
from . import defines as df, lv_validation as lvalid, types as ty from . import defines as df, lv_validation as lvalid
from .helpers import add_lv_use, requires_component, validate_printf from .helpers import add_lv_use, requires_component, validate_printf
from .lv_validation import lv_color, lv_font, lv_image from .lv_validation import lv_color, lv_font, lv_image
from .lvcode import LvglComponent from .lvcode import LvglComponent
from .types import WidgetType, lv_group_t from .types import (
LVEncoderListener,
LvType,
WidgetType,
lv_group_t,
lv_obj_t,
lv_pseudo_button_t,
lv_style_t,
)
# this will be populated later, in __init__.py to avoid circular imports. # this will be populated later, in __init__.py to avoid circular imports.
WIDGET_TYPES: dict = {} WIDGET_TYPES: dict = {}
@ -46,7 +54,7 @@ TEXT_SCHEMA = cv.Schema(
LIST_ACTION_SCHEMA = cv.ensure_list( LIST_ACTION_SCHEMA = cv.ensure_list(
cv.maybe_simple_value( cv.maybe_simple_value(
{ {
cv.Required(CONF_ID): cv.use_id(ty.lv_pseudo_button_t), cv.Required(CONF_ID): cv.use_id(lv_pseudo_button_t),
}, },
key=CONF_ID, key=CONF_ID,
) )
@ -59,9 +67,10 @@ PRESS_TIME = cv.All(
ENCODER_SCHEMA = cv.Schema( ENCODER_SCHEMA = cv.Schema(
{ {
cv.GenerateID(): cv.All( cv.GenerateID(): cv.All(
cv.declare_id(ty.LVEncoderListener), requires_component("binary_sensor") cv.declare_id(LVEncoderListener), requires_component("binary_sensor")
), ),
cv.Optional(CONF_GROUP): cv.declare_id(lv_group_t), cv.Optional(CONF_GROUP): cv.declare_id(lv_group_t),
cv.Optional(df.CONF_INITIAL_FOCUS): cv.use_id(lv_obj_t),
cv.Optional(df.CONF_LONG_PRESS_TIME, default="400ms"): PRESS_TIME, cv.Optional(df.CONF_LONG_PRESS_TIME, default="400ms"): PRESS_TIME,
cv.Optional(df.CONF_LONG_PRESS_REPEAT_TIME, default="100ms"): PRESS_TIME, cv.Optional(df.CONF_LONG_PRESS_REPEAT_TIME, default="100ms"): PRESS_TIME,
} }
@ -161,7 +170,7 @@ STYLE_REMAP = {
# Complete object style schema # Complete object style schema
STYLE_SCHEMA = cv.Schema({cv.Optional(k): v for k, v in STYLE_PROPS.items()}).extend( STYLE_SCHEMA = cv.Schema({cv.Optional(k): v for k, v in STYLE_PROPS.items()}).extend(
{ {
cv.Optional(df.CONF_STYLES): cv.ensure_list(cv.use_id(ty.lv_style_t)), cv.Optional(df.CONF_STYLES): cv.ensure_list(cv.use_id(lv_style_t)),
cv.Optional(df.CONF_SCROLLBAR_MODE): df.LvConstant( cv.Optional(df.CONF_SCROLLBAR_MODE): df.LvConstant(
"LV_SCROLLBAR_MODE_", "OFF", "ON", "ACTIVE", "AUTO" "LV_SCROLLBAR_MODE_", "OFF", "ON", "ACTIVE", "AUTO"
).one_of, ).one_of,
@ -193,12 +202,12 @@ def part_schema(widget_type: WidgetType):
) )
def automation_schema(typ: ty.LvType): def automation_schema(typ: LvType):
if typ.has_on_value: if typ.has_on_value:
events = df.LV_EVENT_TRIGGERS + (CONF_ON_VALUE,) events = df.LV_EVENT_TRIGGERS + (CONF_ON_VALUE,)
else: else:
events = df.LV_EVENT_TRIGGERS events = df.LV_EVENT_TRIGGERS
if isinstance(typ, ty.LvType): if isinstance(typ, LvType):
template = Trigger.template(typ.get_arg_type()) template = Trigger.template(typ.get_arg_type())
else: else:
template = Trigger.template() template = Trigger.template()
@ -261,7 +270,7 @@ LAYOUT_SCHEMAS = {}
ALIGN_TO_SCHEMA = { ALIGN_TO_SCHEMA = {
cv.Optional(df.CONF_ALIGN_TO): cv.Schema( cv.Optional(df.CONF_ALIGN_TO): cv.Schema(
{ {
cv.Required(CONF_ID): cv.use_id(ty.lv_obj_t), cv.Required(CONF_ID): cv.use_id(lv_obj_t),
cv.Required(df.CONF_ALIGN): df.ALIGN_ALIGNMENTS.one_of, cv.Required(df.CONF_ALIGN): df.ALIGN_ALIGNMENTS.one_of,
cv.Optional(df.CONF_X, default=0): lvalid.pixels_or_percent, cv.Optional(df.CONF_X, default=0): lvalid.pixels_or_percent,
cv.Optional(df.CONF_Y, default=0): lvalid.pixels_or_percent, cv.Optional(df.CONF_Y, default=0): lvalid.pixels_or_percent,

View file

@ -46,6 +46,7 @@ binary_sensor:
lvgl: lvgl:
encoders: encoders:
group: switches group: switches
initial_focus: button_button
enter_button: select_button enter_button: select_button
sensor: sensor:
left_button: up_button left_button: up_button