051 LED Package Variant
051-led-package-variant.mc selects package metadata from a constructor parameter:
text
component CONFIG_LED(package_style::STRING)
{
if (package_style == "0603")
{
package = "0603"
}
else if (package_style == "1206")
{
package = "1206"
}
else
{
package = "unspecified"
}
}package_style::STRINGdeclares a required string parameter. Each instance passes the package style that its conditions should evaluate.if (...)evaluates a condition;==compares values rather than assigning one.else ifprovides another condition when the first one is false.elseis the final fallback. It runs only when every preceding condition is false, so an unexpected package string still receives defined metadata.- Attribute assignments inside a selected block apply only for that variant.
D_SMALL passes "0603", while D_LARGE passes "1206", so the two instances select different explicit branches. The fallback is not selected by either instance, but it shows how the component handles any other value. Both LEDs are connected from V3V3 through their own current-limiting resistors to GND, so they are complete comparable instances rather than floating type demonstrations. Their electrical topology is the same; the selected package metadata is different.
bash
eval "$(../mcc/scripts/mcc-slot.sh)"
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 05-dynamic-pins-and-conditions/051-led-package-variant.mc --lib mcode --pass1 --pass2
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 05-dynamic-pins-and-conditions/051-led-package-variant.mc --lib mcode --viz -o 05-dynamic-pins-and-conditions/051-led-package-variant.htmlSource
text
// Example: LED Package Variant
// Goal: Select attributes from a parameter value.
// Language focus: typed parameters, if, else if, else, conditional attributes.
component CONFIG_LED(package_style::STRING)
{
name = "Configurable LED"
pins = [
io [1, 2] = DIO{A, K} // A = anode, K = cathode (DIO face adoption)
]
if (package_style == "0603")
{
package = "0603"
description = "Compact status LED"
}
else if (package_style == "1206")
{
package = "1206"
description = "Large status LED"
}
else
{
package = "unspecified"
description = "Status LED with an unspecified package"
}
}
module main
{
io GND
io V3V3
DC.SRC PWR(3.3V, 20mA)
CONFIG_LED("0603") D_SMALL
CONFIG_LED("1206") D_LARGE
RES R_SMALL(330R, 50V)
RES R_LARGE(330R, 50V)
PWR.1 -> V3V3
PWR.2 -> GND
V3V3 -> R_SMALL -> D_SMALL.DIO.A
D_SMALL.DIO.K -> GND
V3V3 -> R_LARGE -> D_LARGE.DIO.A
D_LARGE.DIO.K -> GND
}