Functions
Use functions to create re-usable circuits with parameters. The default library std.cst uses functions for common circuit components like res, cap, etc.
Functions are executed at the current cursor point. When the function has completed execution, the current cursor point may have changed. The branch command can be used before the function is called to restore the previous cursor point.
from "std" import *
v5v = supply("net")
gnd = dgnd()
def my_pull_down(value):
wire down 100
add res(value)
wire down 100
to gnd
at v5v
wire down 100
branch:
my_pull_down(20k)
wire right 200
branch:
my_pull_down(10k)
wire right 200
branch:
my_pull_down(1k)
Function scope
Variables or values declared within a function will be scoped only within that function. Functions are able to access variables defined in an outer scope.
from "std" import *
v5v = supply("5v")
gnd = dgnd()
my_cap = cap(100n)
def my_circuit():
wire down 100
add my_cap
wire down 100
to gnd
def my_circuit2():
my_cap = cap(200n)
wire down 100
add my_cap
wire down 100
to gnd
at v5v
wire down 100
branch:
my_circuit()
wire right 400
branch:
my_circuit2()
Default and keyword arguments
Function parameters can be given a default value with =. Parameters with a default value must come after all parameters without one. When calling a function, arguments can be passed positionally, by keyword (name=value), or as a mix of both — keyword arguments can be given in any order.
from "std" import *
def voltage_divider(r1=10k, r2=10k):
wire down 100
add res(r1)
wire down 100
add res(r2)
wire down 100
voltage_divider()
voltage_divider(r2=1k)
voltage_divider(r2=1k, r1=4.7k)
Calling a function with a missing required argument, an unknown keyword argument name, or multiple values for the same argument raises an error.
Passing components as arguments
A component reference can be passed into a function like any other value, giving access to its properties (e.g. value, params) inside the function.
from "std" import *
def get_value(r):
return r.value
add res(10k)
r1 = res(10k)
print(get_value(r1))