[lvgl] Implement qrcode (#7623)

This commit is contained in:
Clyde Stubbs 2024-10-29 09:05:51 +11:00 committed by GitHub
parent 1e2497748d
commit 22f30d42a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 68 additions and 0 deletions

View file

@ -71,6 +71,7 @@ from .widgets.meter import meter_spec
from .widgets.msgbox import MSGBOX_SCHEMA, msgboxes_to_code
from .widgets.obj import obj_spec
from .widgets.page import add_pages, generate_page_triggers, page_spec
from .widgets.qrcode import qr_code_spec
from .widgets.roller import roller_spec
from .widgets.slider import slider_spec
from .widgets.spinbox import spinbox_spec
@ -109,6 +110,7 @@ for w_type in (
spinbox_spec,
keyboard_spec,
tileview_spec,
qr_code_spec,
):
WIDGET_TYPES[w_type.name] = w_type

View file

@ -0,0 +1,54 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_SIZE, CONF_TEXT
from esphome.cpp_generator import MockObjClass
from ..defines import CONF_MAIN, literal
from ..lv_validation import color, color_retmapper, lv_text
from ..lvcode import LocalVariable, lv, lv_expr
from ..schemas import TEXT_SCHEMA
from ..types import WidgetType, lv_obj_t
from . import Widget
CONF_QRCODE = "qrcode"
CONF_DARK_COLOR = "dark_color"
CONF_LIGHT_COLOR = "light_color"
QRCODE_SCHEMA = TEXT_SCHEMA.extend(
{
cv.Optional(CONF_DARK_COLOR, default="black"): color,
cv.Optional(CONF_LIGHT_COLOR, default="white"): color,
cv.Required(CONF_SIZE): cv.int_,
}
)
class QrCodeType(WidgetType):
def __init__(self):
super().__init__(
CONF_QRCODE,
lv_obj_t,
(CONF_MAIN,),
QRCODE_SCHEMA,
modify_schema=TEXT_SCHEMA,
)
def get_uses(self):
return ("canvas", "img")
def obj_creator(self, parent: MockObjClass, config: dict):
dark_color = color_retmapper(config[CONF_DARK_COLOR])
light_color = color_retmapper(config[CONF_LIGHT_COLOR])
size = config[CONF_SIZE]
return lv_expr.call("qrcode_create", parent, size, dark_color, light_color)
async def to_code(self, w: Widget, config):
if (value := config.get(CONF_TEXT)) is not None:
value = await lv_text.process(value)
with LocalVariable(
"qr_text", cg.const_char_ptr, value, modifier=""
) as str_obj:
lv.qrcode_update(w.obj, str_obj, literal(f"strlen({str_obj})"))
qr_code_spec = QrCodeType()

View file

@ -458,6 +458,18 @@ lvgl:
- id: page2
widgets:
- qrcode:
id: lv_qr
align: left_mid
size: 100
light_color: whitesmoke
dark_color: steelblue
text: esphome.io
on_click:
lvgl.qrcode.update:
id: lv_qr
text: homeassistant.io
- slider:
min_value: 0
max_value: 255