2026-03-03 10:20:49 +07:00
# Wokwi Libraries Integration
2026-03-12 01:46:42 +07:00
This project uses the official Wokwi repositories cloned locally, which allows keeping them up-to-date and compatible with the latest versions. The local repositories power both AVR emulation and the dynamic component system with 48+ electronic elements.
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
## Cloned Repositories
2026-03-03 10:20:49 +07:00
2026-03-05 05:16:00 +07:00
### wokwi-elements
2026-03-12 01:46:42 +07:00
- **Location**: `wokwi-libs/wokwi-elements/`
- **Description**: Web Components (Lit) for 48+ electronic elements (LEDs, resistors, buttons, LCDs, sensors, etc.)
- **Repository**: https://github.com/wokwi/wokwi-elements
- **License**: MIT
- **Current usage**: Visual rendering of all components on the simulation canvas. A metadata generation script (`scripts/generate-component-metadata.ts`) parses the TypeScript source code to automatically discover all components, their properties, and pins.
2026-03-03 10:20:49 +07:00
2026-03-05 05:16:00 +07:00
### avr8js
2026-03-12 01:46:42 +07:00
- **Location**: `wokwi-libs/avr8js/`
- **Description**: Complete AVR8 microcontroller emulator (ATmega328p) in JavaScript
- **Repository**: https://github.com/wokwi/avr8js
- **License**: MIT
- **Current usage**: Real CPU emulation at 16MHz, with Timer0/1/2, USART, ADC, and GPIO ports (PORTB/C/D). Runs ~267,000 cycles per frame at ~60fps.
2026-03-03 10:20:49 +07:00
2026-03-05 05:16:00 +07:00
### rp2040js
2026-03-12 01:46:42 +07:00
- **Location**: `wokwi-libs/rp2040js/`
- **Description**: Raspberry Pi Pico (RP2040) emulator in JavaScript
- **Repository**: https://github.com/wokwi/rp2040js
- **License**: MIT
- **Usage**: Cloned for future Raspberry Pi Pico support
2026-03-03 10:20:49 +07:00
2026-03-05 05:16:00 +07:00
### wokwi-features
2026-03-12 01:46:42 +07:00
- **Location**: `wokwi-libs/wokwi-features/`
- **Description**: Wokwi documentation and feature tracking
- **Repository**: https://github.com/wokwi/wokwi-features
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
## Project Configuration
2026-03-03 10:20:49 +07:00
### Frontend (Vite)
2026-03-12 01:46:42 +07:00
The `frontend/vite.config.ts` file is configured to use the local repositories via aliases:
2026-03-03 10:20:49 +07:00
```typescript
resolve: {
alias: {
'avr8js': path.resolve(__dirname, '../wokwi-libs/avr8js/dist/esm'),
'@wokwi/elements': path.resolve(__dirname, '../wokwi-libs/wokwi-elements/dist/esm'),
},
2026-03-05 04:41:49 +07:00
},
optimizeDeps: {
include: ['avr8js', '@wokwi/elements'],
2026-03-03 10:20:49 +07:00
}
```
2026-03-12 01:46:42 +07:00
The `frontend/package.json` file references the local packages:
2026-03-03 10:20:49 +07:00
```json
{
"dependencies": {
"@wokwi/elements": "file:../wokwi-libs/wokwi-elements",
"avr8js": "file:../wokwi-libs/avr8js"
}
}
```
2026-03-12 01:46:42 +07:00
### Automatic Metadata Generation
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
The `scripts/generate-component-metadata.ts` script parses the wokwi-elements source code using the TypeScript AST to extract:
- Tag name (`@customElement('wokwi-led')` → `wokwi-led` )
- Properties (`@property()` decorators → type, default value)
- Number of pins
- Category, description, and tags
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
The result is stored in `frontend/public/components-metadata.json` and consumed by the `ComponentRegistry` at runtime.
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
## Updating the Wokwi Libraries
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
To keep your project up-to-date with the latest versions of Wokwi:
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
### Option 1: Update all libraries (Recommended)
2026-03-03 10:20:49 +07:00
```bash
2026-03-12 01:46:42 +07:00
# Script to update all repositories
2026-03-05 04:41:49 +07:00
update-wokwi-libs.bat
2026-03-03 10:20:49 +07:00
```
2026-03-12 01:46:42 +07:00
### Option 2: Update each repository manually
2026-03-03 10:20:49 +07:00
```bash
2026-03-05 04:41:49 +07:00
cd wokwi-libs
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
# Update wokwi-elements
2026-03-03 10:20:49 +07:00
cd wokwi-elements
git pull origin main
npm install
npm run build
2026-03-12 01:46:42 +07:00
# Update avr8js
2026-03-03 10:20:49 +07:00
cd ../avr8js
git pull origin main
npm install
npm run build
2026-03-12 01:46:42 +07:00
# Update rp2040js
2026-03-03 10:20:49 +07:00
cd ../rp2040js
git pull origin main
npm install
npm run build
```
2026-03-12 01:46:42 +07:00
### Option 3: Update to a specific version
2026-03-03 10:20:49 +07:00
```bash
2026-03-05 04:41:49 +07:00
cd wokwi-libs/wokwi-elements
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
# View available versions
2026-03-03 10:20:49 +07:00
git tag -l
2026-03-12 01:46:42 +07:00
# Switch to a specific version
2026-03-03 10:20:49 +07:00
git checkout v1.9.2
2026-03-12 01:46:42 +07:00
# Rebuild
2026-03-03 10:20:49 +07:00
npm install
npm run build
```
2026-03-12 01:46:42 +07:00
### After Updating wokwi-elements
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
If you updated wokwi-elements, regenerate the component metadata so that new components appear in the UI:
2026-03-05 04:41:49 +07:00
```bash
cd frontend
npx tsx ../scripts/generate-component-metadata.ts
```
2026-03-12 01:46:42 +07:00
## Automatic Update Script
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
The `update-wokwi-libs.bat` script simplifies updates:
2026-03-03 10:20:49 +07:00
```batch
@echo off
echo ========================================
2026-03-12 01:46:42 +07:00
echo Updating Wokwi Libraries
2026-03-03 10:20:49 +07:00
echo ========================================
cd wokwi-libs
2026-03-12 01:46:42 +07:00
echo [1/3] Updating wokwi-elements...
2026-03-03 10:20:49 +07:00
cd wokwi-elements
git pull origin main
npm install
npm run build
cd ..
2026-03-12 01:46:42 +07:00
echo [2/3] Updating avr8js...
2026-03-03 10:20:49 +07:00
cd avr8js
git pull origin main
npm install
npm run build
cd ..
2026-03-12 01:46:42 +07:00
echo [3/3] Updating rp2040js...
2026-03-03 10:20:49 +07:00
cd rp2040js
git pull origin main
npm install
npm run build
cd ..
echo ========================================
2026-03-12 01:46:42 +07:00
echo Update complete!
2026-03-03 10:20:49 +07:00
echo ========================================
pause
```
2026-03-12 01:46:42 +07:00
## How the Libraries Are Used
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
### avr8js — AVR Emulation
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
The `AVRSimulator` (`frontend/src/simulation/AVRSimulator.ts`) uses avr8js to create:
2026-03-03 10:20:49 +07:00
2026-03-05 04:41:49 +07:00
```typescript
import { CPU, avrInstruction, AVRTimer, AVRUSART, AVRADC, AVRIOPort } from 'avr8js';
2026-03-12 01:46:42 +07:00
// ATmega328p CPU at 16MHz
2026-03-05 04:41:49 +07:00
const cpu = new CPU(programMemory);
2026-03-12 01:46:42 +07:00
// Peripherals
2026-03-05 04:41:49 +07:00
const timer0 = new AVRTimer(cpu, timer0Config);
const timer1 = new AVRTimer(cpu, timer1Config);
const timer2 = new AVRTimer(cpu, timer2Config);
const usart = new AVRUSART(cpu, usart0Config, CLOCK);
const adc = new AVRADC(cpu, adcConfig);
const portB = new AVRIOPort(cpu, portBConfig); // pins 8-13
const portC = new AVRIOPort(cpu, portCConfig); // A0-A5
const portD = new AVRIOPort(cpu, portDConfig); // pins 0-7
2026-03-12 01:46:42 +07:00
// Simulation loop (~60fps)
2026-03-05 04:41:49 +07:00
function runFrame() {
const cyclesToRun = Math.floor(267000 * speed);
for (let i = 0; i < cyclesToRun ; i + + ) {
2026-03-12 01:46:42 +07:00
avrInstruction(cpu); // Execute AVR instruction
cpu.tick(); // Update peripherals
2026-03-05 04:41:49 +07:00
}
requestAnimationFrame(runFrame);
}
```
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
### wokwi-elements — Visual Components
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
Components are rendered in two ways:
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
**1. DynamicComponent (current system — 48 components)**
2026-03-03 10:20:49 +07:00
2026-03-05 04:41:49 +07:00
```typescript
import { ComponentRegistry } from './services/ComponentRegistry';
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
// Load metadata from /components-metadata.json
2026-03-05 04:41:49 +07:00
const registry = ComponentRegistry.getInstance();
const metadata = registry.getById('led');
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
// DynamicComponent creates the web component dynamically
2026-03-05 04:41:49 +07:00
// document.createElement(metadata.tagName) → < wokwi-led >
2026-03-12 01:46:42 +07:00
// Syncs React props → web component
// Extracts pinInfo from the DOM for wire connections
2026-03-05 04:41:49 +07:00
```
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
**2. Legacy React wrappers (5 components)**
2026-03-03 10:20:49 +07:00
```tsx
2026-03-12 01:46:42 +07:00
// ArduinoUno.tsx — still actively used for the main board
2026-03-05 04:41:49 +07:00
< wokwi-arduino-uno ref = {ref} led13 = {led13} / >
2026-03-03 10:20:49 +07:00
```
2026-03-12 01:46:42 +07:00
### PartSimulationRegistry — Simulation Behaviors
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
16 parts have registered simulation logic that connects the web components to the AVR emulator:
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
| Part | Type | Behavior |
|------|------|----------|
2026-03-05 04:41:49 +07:00
| `led` | Output | Pin state → `element.value` |
2026-03-12 01:46:42 +07:00
| `rgb-led` | Output | Digital + PWM on R/G/B |
| `led-bar-graph` | Output | 10 independent LEDs |
| `7segment` | Output | 8 segments (A-G + DP) |
2026-03-05 04:41:49 +07:00
| `pushbutton` | Input | Press/release → `setPinState()` |
2026-03-12 01:46:42 +07:00
| `pushbutton-6mm` | Input | Same as pushbutton |
2026-03-05 04:41:49 +07:00
| `slide-switch` | Input | Change event → pin state |
2026-03-12 01:46:42 +07:00
| `dip-switch-8` | Input | 8 independent switches |
| `potentiometer` | Input | Value → ADC voltage |
| `slide-potentiometer` | Input | Same logic via SIG/OUT |
| `photoresistor-sensor` | Input/Output | Analog voltage + digital LED |
2026-03-05 04:41:49 +07:00
| `analog-joystick` | Input | VRX/VRY (ADC) + SW (digital) |
2026-03-12 01:46:42 +07:00
| `servo` | Output | OCR1A/ICR1 registers → angle 0-180° |
2026-03-05 04:41:49 +07:00
| `buzzer` | Output | Web Audio API + Timer2 |
2026-03-12 01:46:42 +07:00
| `lcd1602` | Output | Full HD44780 4-bit protocol (16× 2) |
| `lcd2004` | Output | Full HD44780 4-bit protocol (20× 4) |
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
## Available Wokwi Components (48)
2026-03-05 04:41:49 +07:00
### Boards (4)
- `wokwi-arduino-uno` — Arduino Uno R3
- `wokwi-arduino-mega` — Arduino Mega 2560
- `wokwi-arduino-nano` — Arduino Nano
- `wokwi-esp32-devkit-v1` — ESP32 DevKit v1
### Sensors (6)
2026-03-12 01:46:42 +07:00
- `wokwi-dht22` — Temperature and humidity sensor
- `wokwi-hc-sr04` — Ultrasonic distance sensor
- `wokwi-pir-motion-sensor` — PIR motion sensor
- `wokwi-photoresistor-sensor` — Photoresistor (LDR)
- `wokwi-ntc-temperature-sensor` — NTC temperature sensor
- `wokwi-analog-joystick` — Analog joystick
2026-03-05 04:41:49 +07:00
### Displays (3)
2026-03-12 01:46:42 +07:00
- `wokwi-lcd1602` — LCD 16x2 with HD44780 protocol
- `wokwi-lcd2004` — LCD 20x4 with HD44780 protocol
- `wokwi-7segment` — 7-segment display
2026-03-05 04:41:49 +07:00
### Input (5)
2026-03-12 01:46:42 +07:00
- `wokwi-pushbutton` — Push button
- `wokwi-pushbutton-6mm` — 6mm push button
- `wokwi-slide-switch` — Slide switch
- `wokwi-dip-switch-8` — 8-position DIP switch
- `wokwi-potentiometer` — Potentiometer
2026-03-05 04:41:49 +07:00
### Output (5)
2026-03-12 01:46:42 +07:00
- `wokwi-led` — Colored LED
- `wokwi-rgb-led` — RGB LED
- `wokwi-led-bar-graph` — LED bar graph (10 LEDs)
- `wokwi-buzzer` — Piezoelectric buzzer
- `wokwi-neopixel` — Addressable RGB LED (WS2812)
2026-03-05 04:41:49 +07:00
### Motors (2)
- `wokwi-servo` — Servo motor
2026-03-12 01:46:42 +07:00
- `wokwi-stepper-motor` — Stepper motor
2026-03-05 04:41:49 +07:00
### Passive (4)
2026-03-12 01:46:42 +07:00
- `wokwi-resistor` — Resistor with color code
- `wokwi-slide-potentiometer` — Slide potentiometer
- `wokwi-led-ring` — LED ring
- `wokwi-membrane-keypad` — Matrix keypad
2026-03-05 04:41:49 +07:00
### Other (19)
2026-03-12 01:46:42 +07:00
- Various components including `wokwi-ir-receiver` , `wokwi-ds1307` , breadboards, etc.
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
## Advantages of This Approach
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
### Advantages
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
1. **Easy Updates** : A simple `git pull` + rebuild gives you the latest improvements
2. **Wokwi Compatible** : Uses exactly the same code as Wokwi.com
3. **Automatic Discovery** : New components appear automatically after regenerating metadata
4. **Version Control** : You can checkout to specific versions
5. **Flexible Development** : Source code available for debugging and modifications
6. **No npm Dependency** : You don't depend on npm package publications
7. **100% Offline** : Works completely without internet after initial setup
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
### Considerations
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
1. **Disk Space** : Cloned repositories take more disk space (~200MB)
2. **Compilation** : You must rebuild the repositories after updating them
3. **Metadata** : Regenerate `components-metadata.json` after updating wokwi-elements
2026-03-03 10:20:49 +07:00
## Troubleshooting
### Error: "Module not found: @wokwi/elements"
2026-03-12 01:46:42 +07:00
Make sure wokwi-elements is built:
2026-03-03 10:20:49 +07:00
```bash
cd wokwi-libs/wokwi-elements
npm install
npm run build
```
### Error: "Cannot find module 'avr8js'"
2026-03-12 01:46:42 +07:00
Verify that the alias in `vite.config.ts` is correct and that avr8js is built:
2026-03-03 10:20:49 +07:00
2026-03-05 04:41:49 +07:00
```bash
cd wokwi-libs/avr8js
npm install
npm run build
```
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
### Components are not shown in the picker
2026-03-03 10:20:49 +07:00
2026-03-12 01:46:42 +07:00
Regenerate the component metadata:
2026-03-05 04:41:49 +07:00
```bash
cd frontend
npx tsx ../scripts/generate-component-metadata.ts
2026-03-03 10:20:49 +07:00
```
2026-03-12 01:46:42 +07:00
### New wokwi-elements component does not appear
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
1. Update wokwi-elements: `cd wokwi-libs/wokwi-elements && git pull && npm run build`
2. Regenerate metadata: `cd frontend && npx tsx ../scripts/generate-component-metadata.ts`
3. If it needs simulation, register its behavior in `frontend/src/simulation/parts/`
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
### Components are visible but do not respond to simulation
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
Verify that the component has simulation logic registered in `PartSimulationRegistry` (files `BasicParts.ts` or `ComplexParts.ts` ). Only the 16 registered components have interactive behavior.
2026-03-05 04:41:49 +07:00
2026-03-12 01:46:42 +07:00
## References
2026-03-03 10:20:49 +07:00
- [Wokwi Elements Documentation ](https://elements.wokwi.com/ )
2026-03-05 04:41:49 +07:00
- [AVR8js Repository ](https://github.com/wokwi/avr8js )
2026-03-03 10:20:49 +07:00
- [Wokwi Simulator ](https://wokwi.com )
2026-03-12 01:46:42 +07:00
- [Lit Documentation ](https://lit.dev/ ) — Framework used by wokwi-elements
2026-03-03 10:20:49 +07:00
- [Web Components Guide ](https://developer.mozilla.org/en-US/docs/Web/Web_Components )