Hardware Simulation
Run your firmware logic in Node.js — no board, no upload, no waiting. The @typecad/simulator package gives you virtual pins, buses, and mock devices that behave like real parts, so you can test your code on your computer. A mock device is a stand-in you fully control: set it to return any reading you want, then watch how your firmware responds.
Reasons to Simulate
- Test firmware logic without a board connected
- Verify bus protocols (I2C register reads, SPI transfers) with mock devices
- Catch logic errors before uploading to hardware
- Run in CI — no serial port or device needed
Topics
| Topic | What you’ll learn |
|---|---|
| Software-Defined Hardware | SimBoard, virtual pins and buses |
| Peripheral Mocking | Mock I2C and SPI devices with register-level control |
Simulating a specific real board
If you already depend on a @typecad/board-* package, createBoardFromDefinition() builds a SimBoard whose pin layout, PWM/interrupt pins, ADC config, and bus counts match the real board — no hardcoding:
import { ArduinoUno } from '@typecad/board-arduino-uno';
import { createBoardFromDefinition } from '@typecad/simulator';
const board = createBoardFromDefinition(ArduinoUno);
board.digital(13).asOutput().high(); // matches the real Uno's LED pinSee Software-Defined Hardware → Simulating a Real Board Package for the full API and what’s derived from the board definition.
Quick Example
import { createSimBoard } from '@typecad/simulator';
const board = createSimBoard({
boardType: 'arduino-uno',
digitalPinCount: 14,
analogPinCount: 6,
i2cBusCount: 1,
});
// Simulate a button press on pin 2 (active-low with pull-up)
const button = board.digital(2).asInputPullUp();
button.injectValue(0); // Pressed
button.injectValue(1); // Released
// Read a simulated analog value
const sensor = board.analog(0);
sensor.injectValue(512);
console.log(sensor.readAnalog()); // 512What Can Be Simulated
| Component | Simulator Class | Capabilities |
|---|---|---|
| Digital pin | SimDigitalPin | HIGH/LOW, state change history |
| Analog pin | SimAnalogPin | Continuous value (0-1023), voltage injection |
| PWM pin | SimPWMPin | Duty cycle (0-100%) |
| Interrupt pin | SimInterruptPin | Rising/falling/change events |
| Serial port | SimSerialPort | Buffered TX/RX |
| I2C bus | SimI2CBus | Multi-device with mock peripherals |
| SPI bus | SimSPIBus | Full-duplex transfer |
Combining with @typecad/expect
Simulators work with the test assertion library for end-to-end logic testing:
import { createSimBoard } from '@typecad/simulator';
import { describe, done } from '@typecad/expect';
const board = createSimBoard({ boardType: 'arduino-uno' });
const led = board.digital(13).asOutput();
led.high();
describe('LED control')
.it('turns on')
.expect(led.getBitValue()).toBe(1);
done();See Software-Defined Hardware for the full SimBoard API and Peripheral Mocking for I2C/SPI mock devices.
On This Page