main
Caleb
Publish 0.3.0
4530b6d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
/* hwcalc schematics — one SVG drawing per circuit calculator, labelled with the live inputs and results.
* Shared by the browser UI and the server (GET /api/schematic). Pure string building, no DOM.
* Kit: two-terminal parts (R, NTC, C, L, crystal, diode, LED, switch) placed between two grid points, plus
* rails, grounds, terminals, op-amp, inverter, MOSFET, IC boxes, dimension arrows and PCB cross-section blocks. */
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = factory(require('./calcs.js'));
else root.HWSCHEM = factory(root.HWCALC);
})(typeof self !== 'undefined' ? self : this, function (H) {
'use strict';
const esc = s => String(s).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
// ---------------------------------------------------------------- drawing kit
function Kit() {
const el = [];
const k = {
el,
line(x1, y1, x2, y2, cls = 'w') { el.push(`<line class="${cls}" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}"/>`); return k; },
wire(...pts) { el.push(`<polyline class="w" points="${pts.map(p => p.join(',')).join(' ')}"/>`); return k; },
dot(x, y) { el.push(`<circle class="dot" cx="${x}" cy="${y}" r="2.6"/>`); return k; },
text(x, y, s, cls = 'n', anchor = 'middle') { if (s !== '' && s != null) el.push(`<text class="${cls}" x="${x}" y="${y}" text-anchor="${anchor}">${esc(s)}</text>`); return k; },
rect(x, y, w, h, cls) { el.push(`<rect class="${cls}" x="${x}" y="${y}" width="${w}" height="${h}"/>`); return k; },
raw(s) { el.push(s); return k; },
/** A two-terminal part between (x1,y1) and (x2,y2) (axis-aligned). name/value labels sit beside the body. */
part(kind, x1, y1, x2, y2, name = '', value = '', side = 'a') {
const len = Math.hypot(x2 - x1, y2 - y1), ang = Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI, cx = (x1 + x2) / 2, cy = (y1 + y2) / 2, h = len / 2;
const body = BODY[kind] || BODY.R, b = body.half;
el.push(`<g transform="translate(${cx},${cy}) rotate(${ang})"><line class="w" x1="${-h}" y1="0" x2="${-b}" y2="0"/><line class="w" x1="${b}" y1="0" x2="${h}" y2="0"/>${body.svg}</g>`);
const vert = Math.abs(x2 - x1) < 1;
if (vert) {
const dx = side === 'l' ? -16 : side === 'r2' ? 26 : 16, an = side === 'l' ? 'end' : 'start';
k.text(cx + dx, cy - 3, name, 'n', an); k.text(cx + dx, cy + 12, value, 'v', an);
} else {
const up = side !== 'b';
k.text(cx, up ? cy - 16 : cy + 24, name, 'n'); k.text(cx, up ? cy + 26 : cy + 38, value, 'v');
}
return k;
},
gnd(x, y) { el.push(`<g class="w"><line x1="${x}" y1="${y}" x2="${x}" y2="${y + 10}"/><line x1="${x - 10}" y1="${y + 10}" x2="${x + 10}" y2="${y + 10}"/><line x1="${x - 6}" y1="${y + 14}" x2="${x + 6}" y2="${y + 14}"/><line x1="${x - 2}" y1="${y + 18}" x2="${x + 2}" y2="${y + 18}"/></g>`); return k; },
rail(x, y, label, value = '') { el.push(`<g class="w"><line x1="${x}" y1="${y}" x2="${x}" y2="${y - 10}"/><line x1="${x - 10}" y1="${y - 10}" x2="${x + 10}" y2="${y - 10}"/></g>`);
k.text(x + 14, y - 7, label, 'n', 'start'); return value ? k.text(x + 14 + label.length * 8.5 + 6, y - 7, value, 'v', 'start') : k; },
/** Open terminal with a label; dir = side the label goes ('l' or 'r'). */
term(x, y, label, value = '', dir = 'l') { el.push(`<circle class="term" cx="${x}" cy="${y}" r="3.5"/>`);
const an = dir === 'l' ? 'end' : 'start', dx = dir === 'l' ? -9 : 9;
k.text(x + dx, y + 4, label, 'n', an); return value ? k.text(x + dx, y + 18, value, 'v', an) : k; },
/** Op-amp with its back edge at x, centred on y; top input at y−15, bottom y+15, output (x+56, y). */
opamp(x, y, plusTop, label = '') {
el.push(`<polygon class="w" points="${x},${y - 32} ${x},${y + 32} ${x + 56},${y}"/>`);
k.text(x + 8, y - 11, plusTop ? '+' : '−', 'n', 'start'); k.text(x + 8, y + 19, plusTop ? '−' : '+', 'n', 'start');
return label ? k.text(x + 28, y + 48, label, 'v', 'middle') : k;
},
/** Inverter between (x, y) and (x+60, y). */
inverter(x, y) { el.push(`<g class="w"><line x1="${x}" y1="${y}" x2="${x + 14}" y2="${y}"/><polygon points="${x + 14},${y - 14} ${x + 14},${y + 14} ${x + 40},${y}"/><circle cx="${x + 44}" cy="${y}" r="4"/><line x1="${x + 48}" y1="${y}" x2="${x + 60}" y2="${y}"/></g>`); return k; },
/** N-MOSFET: gate pin (x−22, y), drain pin (x+10, y−26), source pin (x+10, y+26). */
nmos(x, y, name = '', value = '') {
el.push(`<g class="w"><line x1="${x - 22}" y1="${y}" x2="${x - 8}" y2="${y}"/><line x1="${x - 8}" y1="${y - 12}" x2="${x - 8}" y2="${y + 12}"/>` +
`<line x1="${x - 3}" y1="${y - 15}" x2="${x - 3}" y2="${y - 6}"/><line x1="${x - 3}" y1="${y - 4}" x2="${x - 3}" y2="${y + 4}"/><line x1="${x - 3}" y1="${y + 6}" x2="${x - 3}" y2="${y + 15}"/>` +
`<polyline points="${x - 3},${y - 11} ${x + 10},${y - 11} ${x + 10},${y - 26}"/><polyline points="${x - 3},${y + 11} ${x + 10},${y + 11} ${x + 10},${y + 26}"/><polyline points="${x - 3},${y} ${x + 10},${y} ${x + 10},${y + 11}"/></g>` +
`<polygon class="fill" points="${x - 2},${y} ${x + 5},${y - 4} ${x + 5},${y + 4}"/>`);
k.text(x + 18, y - 3, name, 'n', 'start'); return k.text(x + 18, y + 12, value, 'v', 'start');
},
box(x, y, w, h, title, sub = '') { k.rect(x, y, w, h, 'box'); k.text(x + w / 2, y + h / 2 + (sub ? -2 : 4), title, 'n'); return sub ? k.text(x + w / 2, y + h / 2 + 13, sub, 'v') : k; },
pinLabel(x, y, s, anchor = 'start') { return k.text(x, y, s, 'pin', anchor); },
/** Dimension arrow between two points with a label at the middle (offset by dx, dy). */
dim(x1, y1, x2, y2, label, dx = 0, dy = -6, anchor = 'middle') {
el.push(`<line class="dim" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}" marker-start="url(#hwa)" marker-end="url(#hwa)"/>`);
return k.text((x1 + x2) / 2 + dx, (y1 + y2) / 2 + dy, label, 'v', anchor);
},
svg(w, h, caption = '') {
return `<svg xmlns="http://www.w3.org/2000/svg" class="hwschem" viewBox="0 0 ${w} ${h + (caption ? 18 : 0)}" role="img" aria-label="${esc(caption || 'schematic')}">` +
`<defs><marker id="hwa" viewBox="0 0 10 10" refX="5" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,1 L9,5 L0,9 z" class="fill"/></marker></defs>` +
`<style>${STYLE}</style>${el.join('')}` + (caption ? `<text class="cap" x="${w / 2}" y="${h + 12}" text-anchor="middle">${esc(caption)}</text>` : '') + `</svg>`;
},
};
return k;
}
const STYLE = '.hwschem .w,.hwschem .dim{stroke:#c9d1d9;stroke-width:1.6;fill:none;stroke-linejoin:round;stroke-linecap:round}' +
'.hwschem .dim{stroke:#7fd8d2;stroke-width:1.1}.hwschem .fill{fill:#c9d1d9;stroke:none}.hwschem .dot{fill:#c9d1d9}' +
'.hwschem .term{fill:#161b22;stroke:#c9d1d9;stroke-width:1.5}.hwschem .box{fill:#1b222c;stroke:#c9d1d9;stroke-width:1.5}' +
'.hwschem .dash{stroke:#8b949e;stroke-width:1.2;fill:none;stroke-dasharray:5 4}' +
'.hwschem text{font:12px Satoshi,system-ui,sans-serif;fill:#dfe6ee}.hwschem .v{fill:#2dd4c8;font-size:11.5px;font-weight:600}' +
'.hwschem .pin{fill:#8b949e;font-size:10.5px}.hwschem .cap{fill:#8b949e;font-size:11px}' +
'.hwschem .cu{fill:#c98a3e;stroke:#e0a458;stroke-width:1}.hwschem .fr4{fill:#24463c;stroke:#2f5a4d;stroke-width:1}.hwschem .air{fill:#0f141a}';
// Part bodies in local coordinates (horizontal, centred on the origin); half = half-length of the body.
const ZIG = '-20,0 -17.5,-6 -12.5,6 -7.5,-6 -2.5,6 2.5,-6 7.5,6 12.5,-6 17.5,6 20,0';
const BODY = {
R: { half: 20, svg: `<polyline class="w" points="${ZIG}"/>` },
NTC: { half: 20, svg: `<polyline class="w" points="${ZIG}"/><polyline class="w" points="-22,12 -15,12 15,-12"/>` },
C: { half: 4, svg: '<line class="w" x1="-4" y1="-12" x2="-4" y2="12"/><line class="w" x1="4" y1="-12" x2="4" y2="12"/>' },
L: { half: 20, svg: '<path class="w" d="M-20,0 a5,5 0 0 1 10,0 a5,5 0 0 1 10,0 a5,5 0 0 1 10,0 a5,5 0 0 1 10,0"/>' },
X: { half: 10, svg: '<line class="w" x1="-10" y1="-11" x2="-10" y2="11"/><line class="w" x1="10" y1="-11" x2="10" y2="11"/><rect class="box" x="-5" y="-13" width="10" height="26"/>' },
D: { half: 9, svg: '<polygon class="w" points="-9,-9 -9,9 7,0"/><line class="w" x1="7" y1="-9" x2="7" y2="9"/><line class="w" x1="-9" y1="0" x2="7" y2="0"/>' },
LED: { half: 9, svg: '<polygon class="w" points="-9,-9 -9,9 7,0"/><line class="w" x1="7" y1="-9" x2="7" y2="9"/><line class="w" x1="-9" y1="0" x2="7" y2="0"/>' +
'<g class="w"><line x1="-2" y1="-11" x2="4" y2="-19"/><line x1="4" y1="-11" x2="10" y2="-19"/></g><polygon class="fill" points="4,-19 0.8,-17.6 3.2,-15.8"/><polygon class="fill" points="10,-19 6.8,-17.6 9.2,-15.8"/>' },
SW: { half: 14, svg: '<circle class="term" cx="-12" cy="0" r="2.5"/><circle class="term" cx="12" cy="0" r="2.5"/><line class="w" x1="-10" y1="-1" x2="12" y2="-12"/>' },
};
// ---------------------------------------------------------------- value helpers
function ctx(id, r) {
const calc = H.CALCS.find(c => c.id === id);
const vin = key => {
const f = calc.inputs.find(q => q.key === key), x = r.inputs[key];
if (!f || x === undefined || (typeof x === 'number' && !Number.isFinite(x))) return '';
if (f.type === 'select' || f.type === 'text') return String(x);
return f.si === false ? `${H.fmtN(x, '', 3)}${f.unit ? ' ' + f.unit : ''}` : H.fmtSI(x, f.unit || '');
};
const vo = prefix => { const o = (r.outputs || []).find(q => q.label.startsWith(prefix)); return o ? o.text : ''; };
return { vin, vo, v: r.inputs, ok: r.ok };
}
// ---------------------------------------------------------------- drawings (id -> (kit, ctx) -> svg)
const D = {};
D.divider = (k, { vin, vo, v }) => {
const design = v.mode === 'design';
k.rail(140, 38, 'Vin', vin('Vin')).part('R', 140, 38, 140, 108, 'R1', design ? vo('Best R1') : vin('R1')).dot(140, 108)
.wire([140, 108], [330, 108]).term(330, 108, 'Vout', design ? vo('Output') : vo('Output voltage'), 'r')
.part('R', 140, 108, 140, 178, 'R2', design ? vo('Best R2') : vin('R2')).gnd(140, 178);
if (!design && Number.isFinite(v.RL)) k.dot(250, 108).part('R', 250, 108, 250, 178, 'RL', vin('RL')).gnd(250, 178);
return k.svg(440, 202, 'Vout = Vin · R2 / (R1 + R2)');
};
D.led = (k, { vin, vo, v }) => {
const n = Math.max(1, Math.round(v.n) || 1), show = Math.min(n, 2);
k.rail(160, 34, 'Vs', vin('Vs')).part('R', 160, 34, 160, 96, 'R', vo('Next'));
let y = 96; for (let i = 0; i < show; i++) { k.part('LED', 160, y, 160, y + 50, i ? '' : 'LED', i ? '' : `Vf ${vin('Vf')} · ${vin('If')}`, 'r2'); y += 50; }
if (n > 2) k.text(210, y - 22, `× ${n} in series`, 'pin', 'start');
return k.gnd(160, y).svg(440, y + 24, `R = (Vs − n·Vf) / If`);
};
D.filter = (k, { vin, vo, v }) => {
const [s, sh, sn, shn] = { RC: ['R', 'C', vin('R'), vin('C')], RL: ['L', 'R', vin('L'), vin('R')], LC: ['L', 'C', vin('L'), vin('C')] }[v.mode] || ['R', 'C', '', ''];
k.term(50, 70, 'in').part(s, 50, 70, 190, 70, s, sn).dot(230, 70).wire([190, 70], [340, 70]).term(340, 70, 'out', '', 'r')
.part(sh, 230, 70, 230, 150, sh, shn).gnd(230, 150);
return k.svg(440, 176, v.mode === 'LC' ? `LC low-pass · f0 ${vo('Resonant')} · √(L/C) ${vo('Characteristic')}` : `${v.mode} low-pass · f−3dB ${vo('−3 dB')} · τ ${vo('Time constant')}`);
};
D.ldo = (k, { vin, vo }) => {
k.term(40, 70, 'Vin', vin('Vin')).wire([40, 70], [150, 70]).box(150, 45, 110, 55, 'LDO', `Pd ${vo('Dissipation')}`).pinLabel(155, 64, 'IN').pinLabel(255, 64, 'OUT', 'end')
.wire([205, 100], [205, 150]).gnd(205, 150).pinLabel(210, 112, 'GND').wire([260, 70], [340, 70]).dot(340, 70)
.part('R', 340, 70, 340, 150, 'load', vin('Iout')).gnd(340, 150).wire([340, 70], [400, 70]).term(400, 70, '', '', 'r').text(400, 55, `Vout ${vin('Vout')}`, 'n', 'end');
return k.svg(440, 176, `Tj = Ta + Pd·θJA = ${vo('Junction')}`);
};
D.buck = (k, { vin, vo }) => {
k.term(70, 60, 'Vin', vin('Vin')).part('SW', 70, 60, 150, 60, 'Q1', `D = ${vo('Duty')}`).dot(150, 60).part('D', 150, 150, 150, 60, 'D1', '', 'l').gnd(150, 150)
.part('L', 150, 60, 270, 60, 'L', `≥ ${vo('Minimum inductance')}`).dot(290, 60).wire([270, 60], [400, 60])
.part('C', 290, 60, 290, 150, 'Cout', vin('Cout')).gnd(290, 150).dot(370, 60).part('R', 370, 60, 370, 150, 'load', vin('Iout')).gnd(370, 150).term(400, 60, '', '', 'r').text(400, 45, `Vout ${vin('Vout')}`, 'n', 'end');
return k.svg(440, 176, `ΔIL ${vo('Ripple current')} · Ipk ${vo('Peak')} · fsw ${vin('fsw')} (synchronous: D1 → low-side FET)`);
};
D.boost = (k, { vin, vo }) => {
k.term(70, 60, 'Vin', vin('Vin')).part('L', 70, 60, 160, 60, 'L', `≥ ${vo('Minimum inductance')}`).dot(170, 60).wire([160, 60], [170, 60])
.part('SW', 170, 60, 170, 150, 'Q1', `D = ${vo('Duty')}`, 'l').gnd(170, 150).part('D', 170, 60, 280, 60, 'D1').dot(290, 60).wire([280, 60], [400, 60])
.part('C', 290, 60, 290, 150, 'Cout', vin('Cout')).gnd(290, 150).dot(370, 60).part('R', 370, 60, 370, 150, 'load', vin('Iout')).gnd(370, 150).term(400, 60, '', '', 'r').text(400, 45, `Vout ${vin('Vout')}`, 'n', 'end');
return k.svg(440, 176, `Ipk ${vo('Peak switch')} · fsw ${vin('fsw')}`);
};
D.holdup = (k, { vin, vo, v }) => {
const C = v.mode === 'time' ? vin('C') : vo('Required capacitance'), load = v.load === 'constant power' ? vin('P') : vin('I');
k.term(80, 60, 'bus in').part('D', 80, 60, 150, 60, 'D (OR-ing)').dot(190, 60).wire([150, 60], [320, 60]).part('C', 190, 60, 190, 150, 'C hold', C).gnd(190, 150)
.dot(320, 60).part('R', 320, 60, 320, 150, 'load', load).gnd(320, 150);
return k.svg(440, 176, `bus falls from V1 ${vin('V1')} to V2 ${vin('V2')} after the input drops`);
};
D.i2c = (k, { vin, vo }) => {
k.box(20, 90, 64, 44, 'MCU').box(356, 90, 64, 44, 'device').rail(170, 30, 'VDD', vin('VDD')).part('R', 170, 30, 170, 112, 'Rp', vo('Suggested'))
.dot(170, 112).wire([84, 112], [356, 112]).text(250, 106, 'SDA / SCL', 'pin').dot(290, 112).raw('<line class="dash" x1="290" y1="112" x2="290" y2="134"/>')
.part('C', 290, 134, 290, 170, '', '').raw('<line class="dash" x1="290" y1="170" x2="290" y2="180"/>').gnd(290, 180).text(304, 150, `Cb ${vin('Cb')} (bus)`, 'v', 'start');
return k.svg(440, 208, `Rp ${vo('Minimum')} … ${vo('Maximum')} · one pull-up per line`);
};
D.crystal = (k, { vin, vo, v }) => {
const caps = v.mode === 'caps for target CL', C1 = caps ? vo('Nearest') : vin('C1'), C2 = caps ? vo('Nearest') : vin('C2');
k.rect(100, 12, 250, 78, 'dash').text(342, 28, 'MCU', 'pin', 'end')
.wire([130, 90], [130, 52], [175, 52]).inverter(175, 52).wire([235, 52], [310, 52], [310, 90]).dot(150, 52).dot(290, 52)
.wire([150, 52], [150, 30]).part('R', 150, 30, 290, 30).text(246, 26, 'Rf', 'pin', 'start').wire([290, 30], [290, 52])
.pinLabel(126, 104, 'OSC_IN', 'end').pinLabel(318, 104, 'OSC_OUT')
.wire([130, 90], [130, 150]).part('R', 310, 90, 310, 150, 'RExt', 'optional').part('X', 130, 150, 310, 150, 'Y1', `CL ${vin('CL')}${v.F ? ' · ' + vin('F') : ''}`, 'b')
.dot(130, 150).dot(310, 150).part('C', 130, 150, 130, 210, 'C1', C1, 'l').gnd(130, 210).part('C', 310, 150, 310, 210, 'C2', C2).gnd(310, 210);
return k.svg(440, 234, `CL = C1·C2/(C1+C2) + Cs = ${vo('Load the crystal')} (Cs ${vin('Cs')} stray)`);
};
D.shunt = (k, { vin, vo }) => {
k.term(70, 60, 'supply').part('R', 70, 60, 170, 60, 'Rshunt', vin('R')).dot(80, 60).dot(150, 60).wire([170, 60], [380, 60])
.wire([80, 60], [80, 146], [200, 146]).wire([150, 60], [150, 116], [200, 116]).opamp(200, 131, false, `G ${vin('G')}`).wire([256, 131], [270, 131])
.box(270, 111, 76, 40, 'ADC', `${vin('bits')} bit`).dot(380, 60).wire([380, 60], [410, 60]).part('R', 410, 60, 410, 180, 'load', vin('I'), 'l').gnd(410, 180);
return k.svg(440, 204, `Vsense ${vo('Sense voltage')} · Kelvin-connect the sense lines at the shunt pads`);
};
D.thermal = (k, { vin, vo, v }) => {
const heat = v.mode === 'junction temperature';
k.text(40, 50, 'Tj', 'n').text(40, 64, heat ? vo('Junction') : vin('Tj'), 'v').dot(40, 80).part('R', 40, 80, 150, 80, 'θJC', vin('jc'), 'b')
.dot(150, 80).text(150, 50, 'case', 'n').text(150, 64, vo('Case'), 'v').part('R', 150, 80, 260, 80, 'θCS', vin('cs'), 'b')
.dot(260, 80).text(260, 50, 'heatsink', 'n').text(260, 64, vo('Heatsink'), 'v').part('R', 260, 80, 370, 80, 'θSA', heat ? vin('sa') : vo('Required') || vo('Heatsink'), 'b')
.dot(370, 80).text(370, 50, 'ambient', 'n').text(370, 64, vin('Ta'), 'v').wire([370, 80], [370, 150]).gnd(370, 150)
.raw('<circle class="w" cx="40" cy="130" r="14"/><line class="w" x1="40" y1="138" x2="40" y2="122"/><polygon class="fill" points="40,118 36,125 44,125"/>')
.wire([40, 80], [40, 116]).wire([40, 144], [40, 150]).gnd(40, 150).text(62, 134, `P ${vin('P')}`, 'v', 'start');
return k.svg(440, 176, 'Heat flows like current: ΔT = P · Σθ');
};
D.regulator = (k, { vin, vo, v }) => {
const target = v.mode.startsWith('R2');
k.term(40, 60, 'Vin').wire([40, 60], [150, 60]).box(150, 38, 110, 56, 'LM317', `Vref ${vin('Vref')}`).pinLabel(155, 56, 'IN').pinLabel(255, 56, 'OUT', 'end')
.wire([260, 60], [380, 60]).dot(320, 60).term(400, 60, 'Vout', target ? vin('Vout') : vo('Output voltage'), 'r')
.part('R', 320, 60, 320, 140, 'R1', vin('R1')).dot(320, 140).wire([320, 140], [205, 140], [205, 94]).pinLabel(210, 108, 'ADJ')
.part('R', 320, 140, 320, 210, 'R2', target ? vo('Nearest') : vin('R2')).gnd(320, 210);
return k.svg(440, 234, 'Vout = Vref·(1 + R2/R1) + Iadj·R2');
};
D.mosfet = (k, { vin, vo }) => {
k.rail(200, 34, 'V', vin('V')).part('R', 200, 34, 200, 96, 'load', vin('I')).nmos(190, 122, 'Q1', `Rds(on) ${vin('Rds')}`).gnd(200, 148)
.term(100, 122, 'gate drive', `${vin('Vgs')} · ${vin('fsw')}`).wire([100, 122], [168, 122]);
return k.svg(440, 176, `Total loss ${vo('Total')} (conduction ${vo('Conduction')} + switching ${vo('Switching')})`);
};
D.opamp = (k, { vin, vo, v }) => {
if (v.cfg === 'inverting') {
k.term(40, 95, 'in').part('R', 40, 95, 150, 95, 'Rg', vin('Rg')).dot(160, 95).wire([150, 95], [200, 95]).opamp(200, 110, false)
.wire([200, 125], [185, 125], [185, 140]).gnd(185, 140).wire([160, 95], [160, 40]).part('R', 160, 40, 300, 40, 'Rf', vin('Rf')).wire([300, 40], [300, 110])
.wire([256, 110], [380, 110]).dot(300, 110).term(380, 110, 'out', `gain ${vo('Gain')}`, 'r');
return k.svg(440, 170, 'G = −Rf/Rg');
}
k.term(60, 95, 'in').wire([60, 95], [200, 95]).opamp(200, 110, true).wire([200, 125], [170, 125]).dot(170, 125).wire([170, 125], [170, 165])
.dot(170, 165).part('R', 170, 165, 300, 165, 'Rf', vin('Rf'), 'b').wire([300, 165], [300, 110]).part('R', 170, 165, 170, 230, 'Rg', vin('Rg'), 'l').gnd(170, 230)
.wire([256, 110], [380, 110]).dot(300, 110).term(380, 110, 'out', `gain ${vo('Gain')}`, 'r');
return k.svg(440, 256, 'G = 1 + Rf/Rg');
};
D.timer555 = (k, { vin, vo, v }) => {
k.box(170, 40, 100, 150, '555', '').pinLabel(176, 74, '7 DIS').pinLabel(176, 134, '6 THR').pinLabel(176, 164, '2 TRIG').pinLabel(264, 104, 'OUT 3', 'end')
.pinLabel(204, 55, '8').pinLabel(234, 55, '4').pinLabel(204, 184, '1').pinLabel(234, 184, '5')
.wire([240, 40], [240, 26], [200, 26]).dot(200, 26).wire([200, 40], [200, 26]).rail(200, 26, 'VCC').wire([200, 190], [200, 205]).gnd(200, 205)
.text(244, 204, 'CV: 10 nF to GND', 'pin', 'start').wire([270, 100], [360, 100]).term(360, 100, 'out', v.mode === 'astable' ? vo('Frequency') : vo('Pulse'), 'r');
if (v.mode === 'astable') {
k.rail(100, 26, 'VCC').part('R', 100, 26, 100, 70, 'RA', vin('RA'), 'l').dot(100, 70).wire([100, 70], [170, 70]).part('R', 100, 70, 100, 130, 'RB', vin('RB'), 'l')
.dot(100, 130).wire([100, 130], [170, 130]).wire([100, 130], [100, 160]).dot(100, 160).wire([100, 160], [170, 160]).part('C', 100, 160, 100, 215, 'C', vin('C'), 'l').gnd(100, 215);
return k.svg(440, 240, 'f = 1.44 / ((RA + 2·RB)·C)');
}
k.rail(100, 26, 'VCC').part('R', 100, 26, 100, 70, 'R', vin('R'), 'l').dot(100, 70).wire([100, 70], [170, 70]).wire([100, 70], [100, 130]).dot(100, 130).wire([100, 130], [170, 130])
.part('C', 100, 130, 100, 200, 'C', vin('C'), 'l').gnd(100, 200).wire([170, 160], [140, 160], [140, 222]).term(140, 222, 'trigger ↓', '', 'r');
return k.svg(440, 240, 't = 1.1 · R · C');
};
D.ntc = (k, { vin, vo, v }) => {
k.rail(150, 34, 'Vcc', vin('Vcc')).part('R', 150, 34, 150, 104, 'Rp', vin('Rp')).dot(150, 104).wire([150, 104], [320, 104]).term(320, 104, 'to ADC', vo('Divider'), 'r')
.part('NTC', 150, 104, 150, 174, 'NTC', `R25 ${vin('R25')} · β ${vin('B')}`).gnd(150, 174);
return k.svg(440, 198, `at ${v.mode.startsWith('resistance') ? vin('T') : vo('Temperature')}: R = ${v.mode.startsWith('resistance') ? vo('Resistance') : vin('R')}`);
};
D.attenuator = (k, { vin, vo }) => {
k.text(110, 22, 'Pi', 'n').term(34, 60, '').part('R', 50, 60, 160, 60, '', vo('Pi: series'), 'a').wire([34, 60], [50, 60]).wire([160, 60], [180, 60]).term(180, 60, '', '', 'r')
.dot(50, 60).dot(160, 60).part('R', 50, 60, 50, 140).gnd(50, 140).part('R', 160, 60, 160, 140, '', vo('Pi: shunt'), 'r').gnd(160, 140)
.text(325, 22, 'Tee', 'n').term(236, 60, '').part('R', 236, 60, 325, 60, '', vo('Tee: series')).part('R', 325, 60, 414, 60, '', '').term(414, 60, '', '', 'r')
.dot(325, 60).part('R', 325, 60, 325, 140, '', vo('Tee: shunt')).gnd(325, 140);
return k.svg(440, 166, `${vin('dB')} in ${vin('Z')} (matched both ends)`);
};
D.rlc = (k, { vin, vo, v }) => {
if (v.cfg === 'parallel') {
k.term(60, 40, 'a').term(60, 150, 'b').wire([60, 40], [320, 40]).wire([60, 150], [320, 150]);
[['R', 140, vin('R')], ['L', 230, vin('L')], ['C', 320, vin('C')]].forEach(([p, x, val], i) => { k.part(p, x, 40, x, 150, p, val); if (i < 2) k.dot(x, 40).dot(x, 150); });
return k.svg(440, 174, `f0 ${vo('Resonant')} · Q ${vo('Q')}`);
}
k.term(30, 80, 'a').part('R', 30, 80, 150, 80, 'R', vin('R')).part('L', 150, 80, 270, 80, 'L', vin('L')).part('C', 270, 80, 380, 80, 'C', vin('C')).term(380, 80, 'b', '', 'r');
return k.svg(440, 140, `f0 ${vo('Resonant')} · Q ${vo('Q')} · |Z| at ${vin('f')} = ${vo('|Z|')}`);
};
D.combine = (k, { vo, v }) => {
const p = { resistors: ['R', 'Ω'], capacitors: ['C', 'F'], inductors: ['L', 'H'] }[v.kind] || ['R', 'Ω'];
const vals = String(v.list || '').split(/[\s,;]+/).filter(Boolean), shown = vals.slice(0, 4), more = vals.length - shown.length;
const lab = s => { const x = H.parseNum(s); return Number.isFinite(x) ? H.fmtSI(x, p[1]) : s; };
if (v.cfg === 'parallel') {
const x0 = 110, dx = 90, xs = shown.map((_, i) => x0 + i * dx), xe = xs[xs.length - 1] || x0;
k.term(50, 40, 'a').term(50, 150, 'b').wire([50, 40], [xe, 40]).wire([50, 150], [xe, 150]);
xs.forEach((x, i) => { k.part(p[0], x, 40, x, 150, p[0] + (i + 1), lab(shown[i])); if (i < xs.length - 1) k.dot(x, 40).dot(x, 150); });
if (more > 0) k.text(xe + 50, 100, `+${more} more`, 'pin', 'start');
return k.svg(440, 174, `Equivalent ${vo('Equivalent')}`);
}
const seg = Math.min(100, 340 / Math.max(1, shown.length)); let x = 40;
k.term(x, 80, 'a'); shown.forEach((s, i) => { k.part(p[0], x, 80, x + seg, 80, p[0] + (i + 1), lab(s)); x += seg; }); k.term(x, 80, more > 0 ? `+${more}` : 'b', '', 'r');
return k.svg(440, 140, `Equivalent ${vo('Equivalent')}`);
};
D.rfind = (k, { vin, vo }) => {
const best = vo('Best'), par = /[∥‖]|\|\|/.test(best), [a, b] = best.split(/\s*(?:\+|[∥‖]|\|\|)\s*/);
if (par) k.term(80, 40, 'a').term(80, 150, 'b').wire([80, 40], [300, 40]).wire([80, 150], [300, 150]).dot(190, 40).dot(190, 150)
.part('R', 190, 40, 190, 150, 'R1', a || '').part('R', 300, 40, 300, 150, 'R2', b || '');
else k.term(60, 80, 'a').part('R', 60, 80, 210, 80, 'R1', a || '').part('R', 210, 80, 360, 80, 'R2', b || '').term(360, 80, 'b', '', 'r');
return k.svg(440, par ? 174 : 140, `target ${vin('target')} → ${vo('Gives')} (${vo('Error')})`);
};
// ---- PCB cross-sections (not to scale; labels are the entered dimensions)
D.impedance = (k, { vin, vo, v }) => {
const w = v.mode === 'width' ? vo('Trace width') : vin('w');
if (v.geom === 'stripline') {
k.rect(40, 40, 360, 8, 'cu').rect(40, 48, 360, 104, 'fr4').rect(40, 152, 360, 8, 'cu').rect(175, 94, 90, 12, 'cu')
.dim(175, 80, 265, 80, `w ${w}`).dim(372, 48, 372, 152, `b ${vin('h')}`, -8, 4, 'end').text(60, 70, `εr ${vin('er')}`, 'n', 'start').text(220, 186, 'plane', 'pin').text(220, 32, 'plane', 'pin');
return k.svg(440, 196, `${v.mode === 'width' ? 'for Z0 ' + vin('Z') : 'Z0 ' + vo('Impedance')} · trace centred between planes (not to scale)`);
}
k.rect(40, 90, 360, 70, 'fr4').rect(40, 160, 360, 8, 'cu').rect(175, 78, 90, 12, 'cu').dim(175, 62, 265, 62, `w ${w}`)
.dim(372, 90, 372, 160, `h ${vin('h')}`, -8, 4, 'end').text(60, 130, `εr ${vin('er')}`, 'n', 'start').text(220, 186, 'reference plane', 'pin').text(290, 74, `${vin('oz')} Cu`, 'pin', 'start');
return k.svg(440, 196, `${v.mode === 'width' ? 'for Z0 ' + vin('Z') : 'Z0 ' + vo('Impedance')} (not to scale)`);
};
D.diffpair = (k, { vin, vo, v }) => {
const strip = v.geom === 'stripline', ty = strip ? 94 : 78;
if (strip) k.rect(40, 40, 360, 8, 'cu').rect(40, 48, 360, 104, 'fr4').rect(40, 152, 360, 8, 'cu');
else k.rect(40, 90, 360, 70, 'fr4').rect(40, 160, 360, 8, 'cu');
k.rect(110, ty, 70, 12, 'cu').rect(210, ty, 70, 12, 'cu').text(145, ty - 4, 'P', 'pin').text(245, ty - 4, 'N', 'pin')
.dim(110, ty - 16, 180, ty - 16, `w ${vin('w')}`).dim(180, ty + 26, 210, ty + 26, `s ${vin('s')}`, 0, 14)
.dim(372, strip ? 48 : 90, 372, strip ? 152 : 160, `${strip ? 'b' : 'h'} ${vin('h')}`, -8, 4, 'end').text(60, strip ? 70 : 130, `εr ${vin('er')}`, 'n', 'start');
return k.svg(440, 190, `Zdiff ${vo('Differential')} (not to scale)`);
};
D.trace = (k, { vin, vo, v }) => {
const w = v.mode === 'width' ? vo('Minimum width') : vin('w'), internal = v.std === 'IPC-2221' && v.layer === 'internal';
k.rect(40, 70, 360, 90, 'fr4').rect(160, internal ? 108 : 56, 120, 14, 'cu').dim(160, internal ? 96 : 42, 280, internal ? 96 : 42, `w ${w}`)
.dim(300, internal ? 108 : 56, 300, internal ? 122 : 70, `${vin('oz')} (${vo('Copper thickness')})`, 8, 4, 'start');
return k.svg(440, 176, `${v.mode === 'width' ? 'I ' + vin('I') : 'I max ' + vo('Maximum current')} for ΔT ${vin('dT')} · ${internal ? 'internal' : 'external'} layer`);
};
D.via = (k, { vin, vo }) => {
k.rect(60, 50, 320, 120, 'fr4').rect(60, 106, 320, 8, 'cu').rect(185, 100, 70, 20, 'fr4')
.rect(190, 50, 60, 120, 'air').rect(190, 50, 8, 120, 'cu').rect(242, 50, 8, 120, 'cu').rect(175, 44, 90, 6, 'cu').rect(175, 170, 90, 6, 'cu')
.dim(198, 30, 242, 30, `drill ${vin('d')}`).dim(175, 194, 265, 194, `pad ${vin('pad')}`, 0, 16).dim(300, 50, 300, 170, `${vin('h')}`, 8, -26, 'start')
.text(80, 102, 'plane', 'pin', 'start').text(80, 140, `antipad ${vin('anti')}`, 'pin', 'start').text(220, 140, `${vin('tp')}`, 'pin');
return k.svg(440, 214, `R ${vo('DC resistance')} · L ${vo('Inductance')} · C ${vo('Pad capacitance')} (not to scale)`);
};
D.spacing = (k, { vin, vo }) => {
k.rect(40, 90, 360, 60, 'fr4').rect(90, 76, 110, 14, 'cu').rect(250, 76, 110, 14, 'cu').text(145, 70, 'conductor A', 'pin').text(305, 70, 'conductor B', 'pin')
.dim(200, 110, 250, 110, `≥ ${vo('Minimum spacing')}`, 0, 22).text(225, 50, `ΔV ${vin('V')}`, 'n');
return k.svg(440, 160, 'IPC-2221 Table 6-1 minimum electrical clearance');
};
D.spiral = (k, { vin, vo, v }) => {
const n = Math.max(1, Math.min(10, v.n || 1)), cx = 200, cy = 110, R = 80, r0 = Math.max(8, R * (v.din / v.dout || 0.3));
const p = (R - r0) / n, pts = []; let r = R;
const sides = v.shape === 'hexagonal' ? 6 : v.shape === 'octagonal' ? 8 : 4, off = sides === 4 ? Math.PI / 4 : Math.PI / sides;
for (let i = 0; i <= n * sides; i++) { const a = off + i * 2 * Math.PI / sides; const rv = r / Math.cos(Math.PI / sides); pts.push([cx + rv * Math.cos(a), cy + rv * Math.sin(a)]); r -= p / sides; }
k.raw(`<polyline class="w" style="stroke:#e0a458;stroke-width:3" points="${pts.map(q => q.map(z => z.toFixed(1)).join(',')).join(' ')}"/>`)
.dim(cx - R, cy + R + 18, cx + R, cy + R + 18, `dout ${vin('dout')}`, 0, 16).dim(cx - r0, cy, cx + r0, cy, `din ${vin('din')}`, 0, -6)
.text(330, 90, `${Math.round(v.n)} turns`, 'n', 'start').text(330, 108, vo('Inductance'), 'v', 'start');
return k.svg(440, 236, `${v.shape} spiral (not to scale)`);
};
// ---------------------------------------------------------------- entry points
function draw(id, raw = {}, settings = {}) {
const fn = D[id]; if (!fn) return null;
const r = H.run(id, raw, settings); if (!r.inputs) return null;
try { return fn(Kit(), ctx(id, r)); } catch (e) { return null; }
}
return { draw, has: id => !!D[id], ids: Object.keys(D) };
});