mirror of
https://github.com/esphome/esphome.git
synced 2025-09-06 05:12:21 +01:00
[lvgl] Stage 4 (#7166)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from typing import Union
|
||||
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.binary_sensor import BinarySensor
|
||||
from esphome.components.color import ColorStruct
|
||||
@@ -6,7 +8,7 @@ from esphome.components.image import Image_
|
||||
from esphome.components.sensor import Sensor
|
||||
from esphome.components.text_sensor import TextSensor
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ARGS, CONF_COLOR, CONF_FORMAT
|
||||
from esphome.const import CONF_ARGS, CONF_COLOR, CONF_FORMAT, CONF_VALUE
|
||||
from esphome.core import HexInt
|
||||
from esphome.cpp_generator import MockObj
|
||||
from esphome.cpp_types import uint32
|
||||
@@ -14,7 +16,14 @@ from esphome.helpers import cpp_string_escape
|
||||
from esphome.schema_extractors import SCHEMA_EXTRACT, schema_extractor
|
||||
|
||||
from . import types as ty
|
||||
from .defines import LV_FONTS, ConstantLiteral, LValidator, LvConstant, literal
|
||||
from .defines import (
|
||||
CONF_END_VALUE,
|
||||
CONF_START_VALUE,
|
||||
LV_FONTS,
|
||||
LValidator,
|
||||
LvConstant,
|
||||
literal,
|
||||
)
|
||||
from .helpers import (
|
||||
esphome_fonts_used,
|
||||
lv_fonts_used,
|
||||
@@ -60,6 +69,13 @@ def color_retmapper(value):
|
||||
return lv_expr.color_from(MockObj(value))
|
||||
|
||||
|
||||
def option_string(value):
|
||||
value = cv.string(value).strip()
|
||||
if value.find("\n") != -1:
|
||||
raise cv.Invalid("Options strings must not contain newlines")
|
||||
return value
|
||||
|
||||
|
||||
lv_color = LValidator(color, ty.lv_color_t, retmapper=color_retmapper)
|
||||
|
||||
|
||||
@@ -156,6 +172,12 @@ lv_bool = LValidator(
|
||||
)
|
||||
|
||||
|
||||
def lv_pct(value: Union[int, float]):
|
||||
if isinstance(value, float):
|
||||
value = int(value * 100)
|
||||
return literal(f"lv_pct({value})")
|
||||
|
||||
|
||||
def lvms_validator_(value):
|
||||
if value == "never":
|
||||
value = "2147483647ms"
|
||||
@@ -189,13 +211,16 @@ class TextValidator(LValidator):
|
||||
args = [str(x) for x in value[CONF_ARGS]]
|
||||
arg_expr = cg.RawExpression(",".join(args))
|
||||
format_str = cpp_string_escape(value[CONF_FORMAT])
|
||||
return f"str_sprintf({format_str}, {arg_expr}).c_str()"
|
||||
return literal(f"str_sprintf({format_str}, {arg_expr}).c_str()")
|
||||
return await super().process(value, args)
|
||||
|
||||
|
||||
lv_text = TextValidator()
|
||||
lv_float = LValidator(cv.float_, cg.float_, Sensor, "get_state()")
|
||||
lv_int = LValidator(cv.int_, cg.int_, Sensor, "get_state()")
|
||||
lv_brightness = LValidator(
|
||||
cv.percentage, cg.float_, Sensor, "get_state()", retmapper=lambda x: int(x * 255)
|
||||
)
|
||||
|
||||
|
||||
def is_lv_font(font):
|
||||
@@ -222,8 +247,33 @@ class LvFont(LValidator):
|
||||
|
||||
async def process(self, value, args=()):
|
||||
if is_lv_font(value):
|
||||
return ConstantLiteral(f"&lv_font_{value}")
|
||||
return ConstantLiteral(f"{value}_engine->get_lv_font()")
|
||||
return literal(f"&lv_font_{value}")
|
||||
return literal(f"{value}_engine->get_lv_font()")
|
||||
|
||||
|
||||
lv_font = LvFont()
|
||||
|
||||
|
||||
def animated(value):
|
||||
if isinstance(value, bool):
|
||||
value = "ON" if value else "OFF"
|
||||
return LvConstant("LV_ANIM_", "OFF", "ON").one_of(value)
|
||||
|
||||
|
||||
def key_code(value):
|
||||
value = cv.Any(cv.All(cv.string_strict, cv.Length(min=1, max=1)), cv.uint8_t)(value)
|
||||
if isinstance(value, str):
|
||||
return ord(value[0])
|
||||
return value
|
||||
|
||||
|
||||
async def get_end_value(config):
|
||||
return await lv_int.process(config.get(CONF_END_VALUE))
|
||||
|
||||
|
||||
async def get_start_value(config):
|
||||
if CONF_START_VALUE in config:
|
||||
value = config[CONF_START_VALUE]
|
||||
else:
|
||||
value = config.get(CONF_VALUE)
|
||||
return await lv_int.process(value)
|
||||
|
Reference in New Issue
Block a user