Declare Color objects in main.cpp (#1395)

This commit is contained in:
Keith Burzinski 2021-03-19 05:40:05 -05:00 committed by GitHub
parent dedf343bd5
commit 818a7f1c78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View File

@ -21,6 +21,7 @@ from esphome.cpp_generator import ( # noqa
progmem_array,
statement,
variable,
new_variable,
Pvariable,
new_Pvariable,
add,

View File

@ -51,7 +51,7 @@ def to_code(config):
elif CONF_WHITE_INT in config:
w = config[CONF_WHITE_INT]
cg.variable(
cg.new_variable(
config[CONF_ID],
cg.StructInitializer(ColorStruct, ("r", r), ("g", g), ("b", b), ("w", w)),
)

View File

@ -448,6 +448,29 @@ def variable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj":
return obj
def new_variable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj":
"""Declare and define a new variable, not pointer type, in the code generation.
:param id_: The ID used to declare the variable.
:param rhs: The expression to place on the right hand side of the assignment.
:param type_: Manually define a type for the variable, only use this when it's not possible
to do so during config validation phase (for example because of template arguments).
:returns The new variable as a MockObj.
"""
assert isinstance(id_, ID)
rhs = safe_exp(rhs)
obj = MockObj(id_, ".")
if type_ is not None:
id_.type = type_
decl = VariableDeclarationExpression(id_.type, "", id_)
CORE.add_global(decl)
assignment = AssignmentExpression(None, "", id_, rhs, obj)
CORE.add(assignment)
CORE.register_variable(id_, obj)
return obj
def Pvariable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj":
"""Declare a new pointer variable in the code generation.