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

TopicWhat you’ll learn
Software-Defined HardwareSimBoard, virtual pins and buses
Peripheral MockingMock 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 pin

See 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());  // 512

What Can Be Simulated

ComponentSimulator ClassCapabilities
Digital pinSimDigitalPinHIGH/LOW, state change history
Analog pinSimAnalogPinContinuous value (0-1023), voltage injection
PWM pinSimPWMPinDuty cycle (0-100%)
Interrupt pinSimInterruptPinRising/falling/change events
Serial portSimSerialPortBuffered TX/RX
I2C busSimI2CBusMulti-device with mock peripherals
SPI busSimSPIBusFull-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.