[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 .automation import disp_update, update_to_code
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 .lvcode import LvContext, LvglComponent
from .schemas import (
@ -272,6 +272,7 @@ async def to_code(config):
templ = await cg.templatable(conf[CONF_TIMEOUT], [], cg.uint32)
idle_trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], lv_component, templ)
await build_automation(idle_trigger, [], conf)
await initial_focus_to_code(config)
for comp in helpers.lvgl_components_required:
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_HEADER_MODE = "header_mode"
CONF_HOME = "home"
CONF_INITIAL_FOCUS = "initial_focus"
CONF_KEY_CODE = "key_code"
CONF_LAYOUT = "layout"
CONF_LEFT_BUTTON = "left_button"

View File

@ -8,6 +8,7 @@ from .defines import (
CONF_DEFAULT_GROUP,
CONF_ENCODERS,
CONF_ENTER_BUTTON,
CONF_INITIAL_FOCUS,
CONF_LEFT_BUTTON,
CONF_LONG_PRESS_REPEAT_TIME,
CONF_LONG_PRESS_TIME,
@ -67,3 +68,10 @@ async def encoders_to_code(var, config):
else:
group = default_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.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 .lv_validation import lv_color, lv_font, lv_image
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.
WIDGET_TYPES: dict = {}
@ -46,7 +54,7 @@ TEXT_SCHEMA = cv.Schema(
LIST_ACTION_SCHEMA = cv.ensure_list(
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,
)
@ -59,9 +67,10 @@ PRESS_TIME = cv.All(
ENCODER_SCHEMA = cv.Schema(
{
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(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_REPEAT_TIME, default="100ms"): PRESS_TIME,
}
@ -161,7 +170,7 @@ STYLE_REMAP = {
# Complete object style schema
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(
"LV_SCROLLBAR_MODE_", "OFF", "ON", "ACTIVE", "AUTO"
).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:
events = df.LV_EVENT_TRIGGERS + (CONF_ON_VALUE,)
else:
events = df.LV_EVENT_TRIGGERS
if isinstance(typ, ty.LvType):
if isinstance(typ, LvType):
template = Trigger.template(typ.get_arg_type())
else:
template = Trigger.template()
@ -261,7 +270,7 @@ LAYOUT_SCHEMAS = {}
ALIGN_TO_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.Optional(df.CONF_X, 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:
encoders:
group: switches
initial_focus: button_button
enter_button: select_button
sensor:
left_button: up_button