Custom components
A custom component is a part you make yourself out of shop parts. It's one Python file in your library that holds a Module for circuit.py, plus driver code for main.py if you like. Once it's in your library, any workspace can bring it along: the daily problem, practice and the sandbox.
Why bother? A component you've already built and tested helps every build that uses it:
- Cost. A component costs exactly what its parts cost. Design it lean once, and every build that uses it fits its budget more easily.
- Time. You import it in one line instead of wiring from scratch.
- Mistakes. Wiring and driver code you've tested once don't need debugging again.
Making one
Open the Components tab in any workspace and fill in New component, or use Start a component on the component library page. You pick:
- a module name: lowercase letters, digits and underscores, like
traffic_light. It becomes the filetraffic_light.pyand the name you import. Names Python or the installed libraries already use (time,math,gpiozero,wiring…) are taken. - a title, like Traffic light, shown in lists.
The new component starts as a working example (an LED with its resistor) to change. It opens in a tab next to circuit.py and main.py. Edits stay in that workspace until you press Save to library (the tab shows ● while there are unsaved changes). On the component's own page you can edit the title, the description and the code, and share it.
What goes in the file
"""A traffic light: three LEDs with their resistors and a shared ground."""
from wiring import *
class TrafficLight(Module):
def build(self):
red, yellow, green = LED("red"), LED("yellow"), LED("green")
r1, r2, r3 = Resistor(330), Resistor(330), Resistor(330)
r1 >> red
r2 >> yellow
r3 >> green
red.cathode >> yellow.cathode
yellow.cathode >> green.cathode
self.expose(red=r1.pin1, yellow=r2.pin1, green=r3.pin1, gnd=red.cathode)
# Driver code for main.py can live in the same file.
from gpiozero import LED as _Led
class TrafficLightDriver:
def __init__(self, red, yellow, green):
self.lights = {"red": _Led(red), "yellow": _Led(yellow), "green": _Led(green)}
def show(self, colour):
for name, light in self.lights.items():
light.value = name == colour
circuit.py uses the Module:
from wiring import *
from traffic_light import TrafficLight
pi = Pi()
lights = TrafficLight()
connect(pi.GPIO17, lights.red)
connect(pi.GPIO27, lights.yellow)
connect(pi.GPIO22, lights.green)
connect(lights.gnd, pi.GND)
and main.py uses the driver:
from traffic_light import TrafficLightDriver
from time import sleep
lights = TrafficLightDriver(red=17, yellow=27, green=22)
for colour in ["red", "yellow", "green"]:
lights.show(colour)
sleep(1)
Everything about Modules (build(), expose(), chain(), parameters) is on the Modules page. Two things to watch:
- An exposed pin can't share its name with an attribute: with
self.red = LED("red")andexpose(red=...),lights.redwould be ambiguous, so the build stops with an error. Keep inner parts in local variables (as above) or give them other names (self.red_led). - Importing
wiringfrommain.pyis fine: defining the classes buys nothing, only creating a Module incircuit.pydoes.
In a build
The Components tab lists the components in this build, each with its import line. Your library is underneath: Add puts one in the build, Remove takes it out (it stays in your library). A build can bring up to 8 components. Run and Run tests both use them, and the board groups each Module's parts together.
The bill counts every part a component buys at its shop price, exactly as if you'd written the parts in circuit.py. A component is never a shop item with a price of its own (unlike kits).
Where it's kept
Your library is kept in this browser, so it works signed out. When you are signed in it is also kept in your account, so the same components are there on your other devices.
Sharing
Share on a component's page publishes a read-only copy and gives you a link ending /c/<id>. Anyone with the link can read the code and press Add to my library to get their own copy, which they can change freely; their copy never changes yours. Press Update shared copy after you edit to update what the link shows. Sharing needs an account; there is no public list of shared components, so a link is the only way in.
Limits
- up to 50 components in a library, 8 in one build;
- up to 50,000 characters per component;
- module names: 2–40 characters,
a–z,0–9and_, starting with a letter.