mirror of
https://github.com/esphome/esphome.git
synced 2024-12-19 16:07:47 +01:00
Declare Color objects in main.cpp (#1395)
This commit is contained in:
parent
dedf343bd5
commit
818a7f1c78
@ -21,6 +21,7 @@ from esphome.cpp_generator import ( # noqa
|
||||
progmem_array,
|
||||
statement,
|
||||
variable,
|
||||
new_variable,
|
||||
Pvariable,
|
||||
new_Pvariable,
|
||||
add,
|
||||
|
@ -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)),
|
||||
)
|
||||
|
@ -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.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user