[lvgl] Implement keypads (#7719)

This commit is contained in:
Clyde Stubbs 2024-11-11 14:07:48 +11:00 committed by GitHub
parent d885d65c9b
commit ffee2f0e88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 123 additions and 20 deletions

View file

@ -7,6 +7,7 @@ import esphome.config_validation as cv
from esphome.const import (
CONF_AUTO_CLEAR_ENABLED,
CONF_BUFFER_SIZE,
CONF_GROUP,
CONF_ID,
CONF_LAMBDA,
CONF_ON_IDLE,
@ -23,9 +24,15 @@ from esphome.helpers import write_file_if_changed
from . import defines as df, helpers, lv_validation as lvalid
from .automation import disp_update, focused_widgets, update_to_code
from .defines import add_define
from .encoders import ENCODERS_CONFIG, encoders_to_code, initial_focus_to_code
from .encoders import (
ENCODERS_CONFIG,
encoders_to_code,
get_default_group,
initial_focus_to_code,
)
from .gradient import GRADIENT_SCHEMA, gradients_to_code
from .hello_world import get_hello_world
from .keypads import KEYPADS_CONFIG, keypads_to_code
from .lv_validation import lv_bool, lv_images_used
from .lvcode import LvContext, LvglComponent, lvgl_static
from .schemas import (
@ -158,6 +165,13 @@ def multi_conf_validate(configs: list[dict]):
display_list = [disp for disps in displays for disp in disps]
if len(display_list) != len(set(display_list)):
raise cv.Invalid("A display ID may be used in only one LVGL instance")
for config in configs:
for item in (df.CONF_ENCODERS, df.CONF_KEYPADS):
for enc in config.get(item, ()):
if CONF_GROUP not in enc:
raise cv.Invalid(
f"'{item}' must have an explicit group set when using multiple LVGL instances"
)
base_config = configs[0]
for config in configs[1:]:
for item in (
@ -173,7 +187,8 @@ def multi_conf_validate(configs: list[dict]):
def final_validation(configs):
multi_conf_validate(configs)
if len(configs) != 1:
multi_conf_validate(configs)
global_config = full_config.get()
for config in configs:
if pages := config.get(CONF_PAGES):
@ -275,6 +290,7 @@ async def to_code(configs):
else:
add_define("LV_FONT_DEFAULT", await lvalid.lv_font.process(default_font))
cg.add(lvgl_static.esphome_lvgl_init())
default_group = get_default_group(config_0)
for config in configs:
frac = config[CONF_BUFFER_SIZE]
@ -303,7 +319,8 @@ async def to_code(configs):
lv_scr_act = get_scr_act(lv_component)
async with LvContext():
await touchscreens_to_code(lv_component, config)
await encoders_to_code(lv_component, config)
await encoders_to_code(lv_component, config, default_group)
await keypads_to_code(lv_component, config, default_group)
await theme_to_code(config)
await styles_to_code(config)
await gradients_to_code(config)
@ -430,6 +447,7 @@ LVGL_SCHEMA = (
cv.Optional(df.CONF_GRADIENTS): GRADIENT_SCHEMA,
cv.Optional(df.CONF_TOUCHSCREENS, default=None): touchscreen_schema,
cv.Optional(df.CONF_ENCODERS, default=None): ENCODERS_CONFIG,
cv.Optional(df.CONF_KEYPADS, default=None): KEYPADS_CONFIG,
cv.GenerateID(df.CONF_DEFAULT_GROUP): cv.declare_id(lv_group_t),
cv.Optional(df.CONF_RESUME_ON_INPUT, default=True): cv.boolean,
}

View file

@ -438,6 +438,7 @@ CONF_HEADER_MODE = "header_mode"
CONF_HOME = "home"
CONF_INITIAL_FOCUS = "initial_focus"
CONF_KEY_CODE = "key_code"
CONF_KEYPADS = "keypads"
CONF_LAYOUT = "layout"
CONF_LEFT_BUTTON = "left_button"
CONF_LINE_WIDTH = "line_width"

View file

@ -17,7 +17,7 @@ from .defines import (
from .helpers import lvgl_components_required, requires_component
from .lvcode import lv, lv_add, lv_assign, lv_expr, lv_Pvariable
from .schemas import ENCODER_SCHEMA
from .types import lv_group_t, lv_indev_type_t
from .types import lv_group_t, lv_indev_type_t, lv_key_t
ENCODERS_CONFIG = cv.ensure_list(
ENCODER_SCHEMA.extend(
@ -39,10 +39,13 @@ ENCODERS_CONFIG = cv.ensure_list(
)
async def encoders_to_code(var, config):
default_group = lv_Pvariable(lv_group_t, config[CONF_DEFAULT_GROUP])
lv_assign(default_group, lv_expr.group_create())
lv.group_set_default(default_group)
def get_default_group(config):
default_group = cg.Pvariable(config[CONF_DEFAULT_GROUP], lv_expr.group_create())
cg.add(lv.group_set_default(default_group))
return default_group
async def encoders_to_code(var, config, default_group):
for enc_conf in config[CONF_ENCODERS]:
lvgl_components_required.add("KEY_LISTENER")
lpt = enc_conf[CONF_LONG_PRESS_TIME].total_milliseconds
@ -54,14 +57,14 @@ async def encoders_to_code(var, config):
if sensor_config := enc_conf.get(CONF_SENSOR):
if isinstance(sensor_config, dict):
b_sensor = await cg.get_variable(sensor_config[CONF_LEFT_BUTTON])
cg.add(listener.set_left_button(b_sensor))
cg.add(listener.add_button(b_sensor, lv_key_t.LV_KEY_LEFT))
b_sensor = await cg.get_variable(sensor_config[CONF_RIGHT_BUTTON])
cg.add(listener.set_right_button(b_sensor))
cg.add(listener.add_button(b_sensor, lv_key_t.LV_KEY_RIGHT))
else:
sensor_config = await cg.get_variable(sensor_config)
lv_add(listener.set_sensor(sensor_config))
b_sensor = await cg.get_variable(enc_conf[CONF_ENTER_BUTTON])
cg.add(listener.set_enter_button(b_sensor))
cg.add(listener.add_button(b_sensor, lv_key_t.LV_KEY_ENTER))
if group := enc_conf.get(CONF_GROUP):
group = lv_Pvariable(lv_group_t, group)
lv_assign(group, lv_expr.group_create())

View file

@ -0,0 +1,77 @@
import esphome.codegen as cg
from esphome.components.binary_sensor import BinarySensor
import esphome.config_validation as cv
from esphome.const import CONF_GROUP, CONF_ID
from .defines import (
CONF_ENCODERS,
CONF_INITIAL_FOCUS,
CONF_KEYPADS,
CONF_LONG_PRESS_REPEAT_TIME,
CONF_LONG_PRESS_TIME,
literal,
)
from .helpers import lvgl_components_required
from .lvcode import lv, lv_assign, lv_expr, lv_Pvariable
from .schemas import ENCODER_SCHEMA
from .types import lv_group_t, lv_indev_type_t
KEYPAD_KEYS = (
"up",
"down",
"right",
"left",
"esc",
"del",
"backspace",
"enter",
"next",
"prev",
"home",
"end",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"#",
"*",
)
KEYPADS_CONFIG = cv.ensure_list(
ENCODER_SCHEMA.extend(
{cv.Optional(key): cv.use_id(BinarySensor) for key in KEYPAD_KEYS}
)
)
async def keypads_to_code(var, config, default_group):
for enc_conf in config[CONF_KEYPADS]:
lvgl_components_required.add("KEY_LISTENER")
lpt = enc_conf[CONF_LONG_PRESS_TIME].total_milliseconds
lprt = enc_conf[CONF_LONG_PRESS_REPEAT_TIME].total_milliseconds
listener = cg.new_Pvariable(
enc_conf[CONF_ID], lv_indev_type_t.LV_INDEV_TYPE_KEYPAD, lpt, lprt
)
await cg.register_parented(listener, var)
for key in [x for x in enc_conf if x in KEYPAD_KEYS]:
b_sensor = await cg.get_variable(enc_conf[key])
cg.add(listener.add_button(b_sensor, literal(f"LV_KEY_{key.upper()}")))
if group := enc_conf.get(CONF_GROUP):
group = lv_Pvariable(lv_group_t, group)
lv_assign(group, lv_expr.group_create())
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

@ -256,15 +256,8 @@ class LVEncoderListener : public Parented<LvglComponent> {
LVEncoderListener(lv_indev_type_t type, uint16_t lpt, uint16_t lprt);
#ifdef USE_BINARY_SENSOR
void set_left_button(binary_sensor::BinarySensor *left_button) {
left_button->add_on_state_callback([this](bool state) { this->event(LV_KEY_LEFT, state); });
}
void set_right_button(binary_sensor::BinarySensor *right_button) {
right_button->add_on_state_callback([this](bool state) { this->event(LV_KEY_RIGHT, state); });
}
void set_enter_button(binary_sensor::BinarySensor *enter_button) {
enter_button->add_on_state_callback([this](bool state) { this->event(LV_KEY_ENTER, state); });
void add_button(binary_sensor::BinarySensor *button, lv_key_t key) {
button->add_on_state_callback([this, key](bool state) { this->event(key, state); });
}
#endif

View file

@ -40,6 +40,7 @@ void_ptr = cg.void.operator("ptr")
lv_coord_t = cg.global_ns.namespace("lv_coord_t")
lv_event_code_t = cg.global_ns.enum("lv_event_code_t")
lv_indev_type_t = cg.global_ns.enum("lv_indev_type_t")
lv_key_t = cg.global_ns.enum("lv_key_t")
FontEngine = lvgl_ns.class_("FontEngine")
IdleTrigger = lvgl_ns.class_("IdleTrigger", automation.Trigger.template())
PauseTrigger = lvgl_ns.class_("PauseTrigger", automation.Trigger.template())

View file

@ -11,6 +11,12 @@ substitutions:
check: "\U000F012C"
arrow_down: "\U000F004B"
binary_sensor:
- id: enter_sensor
platform: template
- id: left_sensor
platform: template
lvgl:
log_level: debug
resume_on_input: true
@ -93,6 +99,10 @@ lvgl:
- touchscreen_id: tft_touch
long_press_repeat_time: 200ms
long_press_time: 500ms
keypads:
- initial_focus: button_button
enter: enter_sensor
next: left_sensor
msgboxes:
- id: message_box