042 Pull-Up Helper Function
042-pull-helper-function.mc models a dual pull-up resistor network. Each method configures one resistor channel and connects one normally-high button input. It reuses the component pin access introduced in 041:
text
func PullA(input, source, switch_pin)
{
input -> INPUT_A
SOURCE_A -> source
input -> switch_pin
return this
}input -> INPUT_Aconnects the caller's input node to the first resistor channel.SOURCE_A -> sourceconnects the other end to the supply rail.- Reusing
inputininput -> switch_pincreates the branch to the button, so the resistor pin and switch common pin share one input node. return thisreturns the currentDUAL_PULL_RESISTORinstance. Unlike an ordinary connection node, that returned instance still has component methods.
The call consumes that return value immediately:
text
RN_PULLS.PullA(BUTTON_A, V3V3, SW_A.COM).PullB(BUTTON_B, V3V3, SW_B.COM)Read the expression from left to right. PullA(...) runs on RN_PULLS and returns that same instance; the following .PullB(...) is then called on the returned instance. PullB omits return because no call follows it. The resulting circuit has two independent normally-high button inputs, while both pull-up channels share V3V3.
bash
eval "$(../mcc/scripts/mcc-slot.sh)"
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 04-functions-and-reuse/042-pull-helper-function.mc --lib mcode --pass1 --pass2
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 04-functions-and-reuse/042-pull-helper-function.mc --lib mcode --viz -o 04-functions-and-reuse/042-pull-helper-function.htmlSource
text
// Example: Pull-Up Helper Function
// Goal: Configure both channels of a pull-up network in one method chain.
// Language focus: return this, chained method calls.
component DUAL_PULL_RESISTOR
{
name = "Dual Pull-Up Resistor"
pins = [
1 = INPUT_A
2 = SOURCE_A
3 = INPUT_B
4 = SOURCE_B
]
func PullA(input, source, switch_pin)
{
input -> INPUT_A
SOURCE_A -> source
input -> switch_pin
return this
}
func PullB(input, source, switch_pin)
{
input -> INPUT_B
SOURCE_B -> source
input -> switch_pin
}
}
module main
{
io BUTTON_A
io BUTTON_B
io GND
io V3V3
DC.SRC PWR(3.3V, 50mA)
DUAL_PULL_RESISTOR RN_PULLS
SWITCH.MOM SW_A
SWITCH.MOM SW_B
PWR.1 -> V3V3
PWR.2 -> GND
RN_PULLS.PullA(BUTTON_A, V3V3, SW_A.COM).PullB(BUTTON_B, V3V3, SW_B.COM)
SW_A.NO -> GND
SW_B.NO -> GND
}