Building Circuits
Circuit schema
A circuit is a plain TypeScript object conforming to the Circuit type. It usually lives in components.ts within an experiment folder. It imports no renderer code, and can be generated entirely by an AI given the component reference.
The Circuit type
type Circuit = {
id: string; // kebab-case, unique — used as the URL slug
title: string; // display name
description: string; // 2–3 sentences: what it does, which gates it uses
components: ComponentInstance[];
steps: Step[];
truthTable?: TruthTable; // optional
};Building circuits using the CB fluent API
The recommended way to define circuits is using the CB fluent builder, which simplifies component placement and handles step cumulative arrays automatically:
// components.ts
import { CB } from '@/labs/builder';
export const SrLatchCircuit = new CB(
'sr-latch',
'SR Latch',
'A Set-Reset latch built from two cross-coupled NAND gates.',
)
.board()
.gate('nand1', 'nand-gate', 5)
.gate('nand2', 'nand-gate', 14)
.wire('w_q', 'green', { ic: 'nand1', pin: 'Y' }, { ic: 'nand2', pin: 'A' })
.wire('w_qn', 'yellow', { ic: 'nand2', pin: 'Y' }, { ic: 'nand1', pin: 'B' })
.step('Place breadboard', 'Your build surface.').show('bb')
.step('Place NAND gates', 'Two cross-coupled gates.').show('nand1', 'nand2').highlight('nand1')
.step('Cross-couple outputs', 'Feedback loop.').show('w_q', 'w_qn').highlight('w_q')
.build();Manual working example
You can also define the Circuit object manually if you prefer:
import { type Circuit } from '@/labs/types';
export const SrLatch: Circuit = {
id: 'sr-latch',
title: 'SR Latch',
description: 'A Set-Reset latch built from two cross-coupled NAND gates.',
components: [
{ id: 'bb', type: 'breadboard' },
{ id: 'nand1', type: 'nand-gate', mountedAt: { board: 'bb', col: 5, row: 'e' } },
{ id: 'nand2', type: 'nand-gate', mountedAt: { board: 'bb', col: 14, row: 'e' } },
{ id: 'w_q', type: 'wire', color: 'green',
from: { ic: 'nand1', pin: 'Y' }, to: { ic: 'nand2', pin: 'A' } },
{ id: 'w_qn', type: 'wire', color: 'yellow',
from: { ic: 'nand2', pin: 'Y' }, to: { ic: 'nand1', pin: 'B' } },
],
steps: [
{ title: 'Place the breadboard', body: 'Your build surface.', show: ['bb'] },
{ title: 'Place NAND gates', body: 'Two cross-coupled gates make memory.',
show: ['bb', 'nand1', 'nand2'], highlight: 'nand1' },
{ title: 'Cross-couple outputs', body: 'Q feeds back to NAND2 input; Q-bar to NAND1.',
show: ['bb', 'nand1', 'nand2', 'w_q', 'w_qn'], highlight: 'w_q' },
],
};id
kebab-case string, unique across all circuits. Used as the route slug: /labs/sr-latch. Must match the folder name src/labs/semesters/.../sr-latch/.
components
An array of ComponentInstance values. Order doesn't matter for rendering — the renderer iterates all of them. Conventional order:
- Breadboard
- ICs (left to right)
- Passives (resistors, capacitors)
- LEDs
- Input wires
- Signal wires
- Ground wires
See Component types for every supported type and its fields.
steps
An ordered array of Step values that define the assembly walkthrough. See Steps & highlighting for the full step authoring guide.
truthTable (optional)
truthTable: {
inputs: ['A', 'B'],
outputs: ['Sum', 'Carry'],
rows: [
{ inputs: { A:0, B:0 }, outputs: { Sum:0, Carry:0 } },
{ inputs: { A:0, B:1 }, outputs: { Sum:1, Carry:0 } },
{ inputs: { A:1, B:0 }, outputs: { Sum:1, Carry:0 } },
{ inputs: { A:1, B:1 }, outputs: { Sum:0, Carry:1 } },
],
}Drives the truth table panel in the lab UI. Input/output keys must be consistent with activeInputs keys in the steps. Omit entirely for circuits without a meaningful truth table.