velxio/test/test_circuit/autosearch/02_solver_api_cheatsheet.md

63 lines
1.8 KiB
Markdown
Raw Normal View History

feat: electrical simulation via ngspice-WASM (eecircuit-engine) Adds full SPICE-accurate electrical simulation to Velxio, behind a lazy- loaded ⚡ toolbar toggle. Arduino / ESP32 / RP2040 sketches now co-simulate with real analog behaviour: correct voltages on wires, real I–V curves on LEDs, working potentiometers, NTC thermistors read by analogRead(), PWM driving RC filters, transistors, op-amps, diodes, MOSFETs, etc. Engine: eecircuit-engine (ngspice compiled to WebAssembly). Main bundle stays at 2.4 MB; the 20 MB SPICE chunk only loads when the user activates electrical mode. Disabled at build time via VITE_ELECTRICAL_SIM=false. Frontend additions: - simulation/spice/: SpiceEngine wrapper + lazy entry, NetlistBuilder with UnionFind over wires, componentToSpice mapping (24 metadataIds incl. real part numbers: 2N2222, 2N3055, BC547, IRF540, 2N7000, 1N4148, 1N4007, 1N4733, LEDs, NTC, op-amp ideal), CircuitScheduler with debounced coalescing, AVRSpiceBridge for quasi-static co-simulation. - store/useElectricalStore: Zustand slice, feature-flag aware. - components/analog-ui/: ⚡ toolbar toggle + SVG voltage overlay. - components/components-instruments/: Voltmeter, Ammeter probes. - 62 tests (spice-*, netlist-builder, component-to-spice, instruments). Sandbox (test/test_circuit/): 47-test validation sandbox that proved the approach (hand-rolled MNA baseline + ngspice pipeline) before porting to the app. Kept as reference. Docs: docs/wiki/circuit-emulation-*.md (13 engineering pages covering architecture, solvers, components, AVR bridge, gotchas, performance, integration plan, API reference, appendix) + electrical-simulation- user-guide.md (end-user facing). Reference plan: test/test_circuit/plan/phase_8_velxio_implementation.md Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 19:11:54 +07:00
# API cheatsheet
```js
import {
Circuit,
Resistor, VoltageSource, CurrentSource, Capacitor, Potentiometer, NTCThermistor, Switch,
Diode, LED, BJT_NPN,
} from '../src/index.js';
const c = new Circuit();
c.addComponent(new VoltageSource('V1', 'vcc', 'gnd', 5));
c.addComponent(new Resistor('R1', 'vcc', 'out', 1000));
c.addComponent(new LED('LED1', 'out', 'gnd', 'red'));
c.solveDC(); // { nodeVoltages, branchCurrents }
c.nodeVoltage('out'); // 2.0 V
c.branchCurrent('V1'); // 0.0136 A (source convention)
// Transient
c.addComponent(new Capacitor('C1', 'out', 'gnd', 1e-6, 0));
const samples = c.runTransient(/* tEnd */ 0.01, /* dt */ 1e-5, /* sampleEvery */ 10);
// Non-linear helpers
led.currentThrough(c.state); // A
led.brightness(c.state); // 0..1 (I/ratedCurrent)
// Parametric components
pot.setWiper(0.75); // 0..1
ntc.setTemperatureC(25);
switch.set(true);
```
## AVR harness
```js
import { AVRHarness } from '../src/avr/AVRHarness.js';
import { potToPwmProgram, adcReadProgram } from '../src/avr/programs.js';
const avr = new AVRHarness();
avr.load(intelHexText); // or avr.loadProgram(Uint16Array)
avr.runCycles(16_000_000); // 1 s of 16 MHz AVR
avr.getPin(13); // 0 or 1
avr.onPinChange(13, (s) => { ... });
avr.getPWMDuty(9); // 0..1
avr.setAnalogVoltage(0, 2.5); // inject 2.5 V on A0
avr.getSerialOutput(); // String buffer from USART TX
```
## AVR mini-assembler
```js
import { LDI, OUT, IN, STS, LDS, RJMP, SBRC, SBRS, NOP, assemble } from '../src/avr/asm.js';
const prog = assemble([
LDI(16, 0xFF),
OUT(0x04, 16), // DDRB = 0xFF
LDI(16, 0x20),
OUT(0x05, 16), // PORTB |= pin13
RJMP(-1), // loop forever (back to self)
]);
```