2026-03-04 06:42:17 +07:00
|
|
|
|
# Project Architecture - Arduino Emulator
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
## Overview
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
This project is a fully local Arduino emulator using official Wokwi repositories for maximum compatibility.
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
┌─────────────────────────────────────────────────────────────┐
|
2026-03-04 06:42:17 +07:00
|
|
|
|
│ USER (Browser) │
|
2026-03-03 10:20:49 +07:00
|
|
|
|
│ http://localhost:5173 │
|
|
|
|
|
|
└──────────────────────────┬──────────────────────────────────┘
|
|
|
|
|
|
│
|
|
|
|
|
|
▼
|
|
|
|
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
|
|
|
|
│ FRONTEND (React + Vite) │
|
|
|
|
|
|
│ │
|
|
|
|
|
|
│ ┌────────────────┐ ┌──────────────────┐ ┌─────────────┐ │
|
|
|
|
|
|
│ │ Monaco Editor │ │ Zustand Store │ │ Simulator │ │
|
|
|
|
|
|
│ │ (Code Edit) │ │ (State Mgmt) │ │ Canvas │ │
|
|
|
|
|
|
│ └────────────────┘ └──────────────────┘ └─────────────┘ │
|
|
|
|
|
|
│ │ │ │ │
|
|
|
|
|
|
│ └───────────────────┴──────────────────────┘ │
|
|
|
|
|
|
│ │ │
|
|
|
|
|
|
│ ▼ │
|
|
|
|
|
|
│ ┌──────────────────────────────────────────────────────┐ │
|
|
|
|
|
|
│ │ Wokwi Components Integration │ │
|
2026-03-04 06:42:17 +07:00
|
|
|
|
│ │ (wokwi-elements + avr8js from local repos) │ │
|
2026-03-03 10:20:49 +07:00
|
|
|
|
│ └──────────────────────────────────────────────────────┘ │
|
|
|
|
|
|
└──────────────────────────┬──────────────────────────────────┘
|
|
|
|
|
|
│ HTTP (axios)
|
|
|
|
|
|
▼
|
|
|
|
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
|
|
|
|
│ BACKEND (FastAPI + Python) │
|
|
|
|
|
|
│ http://localhost:8000 │
|
|
|
|
|
|
│ │
|
|
|
|
|
|
│ ┌─────────────────────────────────────────────────────┐ │
|
|
|
|
|
|
│ │ POST /api/compile │ │
|
2026-03-04 06:42:17 +07:00
|
|
|
|
│ │ - Receives Arduino code (.ino) │ │
|
|
|
|
|
|
│ │ - Compiles with arduino-cli │ │
|
|
|
|
|
|
│ │ - Returns .hex file │ │
|
2026-03-03 10:20:49 +07:00
|
|
|
|
│ └─────────────────────────────────────────────────────┘ │
|
|
|
|
|
|
│ │ │
|
|
|
|
|
|
│ ▼ │
|
|
|
|
|
|
│ ┌─────────────────────────────────────────────────────┐ │
|
|
|
|
|
|
│ │ Arduino CLI Service │ │
|
2026-03-04 06:42:17 +07:00
|
|
|
|
│ │ (Invokes arduino-cli as subprocess) │ │
|
2026-03-03 10:20:49 +07:00
|
|
|
|
│ └─────────────────────────────────────────────────────┘ │
|
|
|
|
|
|
└──────────────────────────┬──────────────────────────────────┘
|
|
|
|
|
|
│
|
|
|
|
|
|
▼
|
|
|
|
|
|
┌──────────────────────┐
|
|
|
|
|
|
│ arduino-cli │
|
2026-03-04 06:42:17 +07:00
|
|
|
|
│ (Local system) │
|
2026-03-03 10:20:49 +07:00
|
|
|
|
└──────────────────────┘
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
## Data Flow: Compilation and Simulation
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### 1. Code Editing
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
2026-03-04 06:42:17 +07:00
|
|
|
|
User writes code
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
|
|
|
|
|
Monaco Editor
|
|
|
|
|
|
↓
|
|
|
|
|
|
Zustand (useEditorStore)
|
|
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
State: code
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### 2. Compilation
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
2026-03-04 06:42:17 +07:00
|
|
|
|
Click "Compile"
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
|
|
|
|
|
EditorToolbar.tsx → compileCode()
|
|
|
|
|
|
↓
|
|
|
|
|
|
Axios POST → http://localhost:8000/api/compile
|
|
|
|
|
|
↓
|
|
|
|
|
|
Backend: ArduinoCLIService.compile()
|
|
|
|
|
|
↓
|
|
|
|
|
|
arduino-cli compile --fqbn arduino:avr:uno
|
|
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
Generates .hex file in temp directory
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
Backend reads .hex and returns content
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
|
|
|
|
|
Frontend: useSimulatorStore.setCompiledHex()
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### 3. Simulation (Currently simplified)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
2026-03-04 06:42:17 +07:00
|
|
|
|
Click "Run"
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
|
|
|
|
|
useSimulatorStore.setRunning(true)
|
|
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
SimulatorCanvas: useEffect detects running=true
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
setInterval every 1000ms
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
|
|
|
|
|
updateComponentState('led-builtin', !state)
|
|
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
wokwi-led component updates visually
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### 4. Real Simulation (Coming with avr8js)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
2026-03-04 06:42:17 +07:00
|
|
|
|
Compiled .hex file
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
|
|
|
|
|
AVRSimulator.loadHex(hex)
|
|
|
|
|
|
↓
|
|
|
|
|
|
hexParser → Uint16Array (program memory)
|
|
|
|
|
|
↓
|
|
|
|
|
|
CPU = new CPU(program)
|
|
|
|
|
|
↓
|
|
|
|
|
|
Click "Run" → AVRSimulator.start()
|
|
|
|
|
|
↓
|
|
|
|
|
|
requestAnimationFrame loop
|
|
|
|
|
|
↓
|
|
|
|
|
|
CPU.tick() × 267,000 cycles/frame
|
|
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
Writes to PORTB/PORTC/PORTD
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
|
|
|
|
|
Write hooks → PinManager.updatePort()
|
|
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
PinManager notifies callbacks
|
2026-03-03 10:20:49 +07:00
|
|
|
|
↓
|
2026-03-04 06:42:17 +07:00
|
|
|
|
Components update visual state
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
## Key Components
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
### Frontend
|
|
|
|
|
|
|
|
|
|
|
|
#### 1. Stores (Zustand)
|
|
|
|
|
|
- **[useEditorStore.ts](frontend/src/store/useEditorStore.ts)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- `code`: Current source code
|
|
|
|
|
|
- `theme`: Editor theme (dark/light)
|
|
|
|
|
|
- `setCode()`: Update code
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
- **[useSimulatorStore.ts](frontend/src/store/useSimulatorStore.ts)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- `running`: Simulation state
|
|
|
|
|
|
- `compiledHex`: Compiled hex file
|
|
|
|
|
|
- `components`: List of electronic components
|
|
|
|
|
|
- `setCompiledHex()`: Save hex
|
|
|
|
|
|
- `updateComponentState()`: Update LED/component
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
#### 2. UI Components
|
2026-03-03 10:20:49 +07:00
|
|
|
|
- **[CodeEditor.tsx](frontend/src/components/editor/CodeEditor.tsx)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- Monaco Editor wrapper
|
|
|
|
|
|
- C++ syntax highlighting
|
|
|
|
|
|
- Auto-completion
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
- **[EditorToolbar.tsx](frontend/src/components/editor/EditorToolbar.tsx)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- Buttons: Compile, Run, Stop
|
|
|
|
|
|
- Compilation state handling
|
|
|
|
|
|
- Error/success messages
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
- **[SimulatorCanvas.tsx](frontend/src/components/simulator/SimulatorCanvas.tsx)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- Renders Arduino Uno
|
|
|
|
|
|
- Renders components (LEDs)
|
|
|
|
|
|
- Simulation loop
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
#### 3. Wokwi Component Wrappers
|
2026-03-03 10:20:49 +07:00
|
|
|
|
- **[LED.tsx](frontend/src/components/components-wokwi/LED.tsx)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- React wrapper for `<wokwi-led>`
|
2026-03-03 10:20:49 +07:00
|
|
|
|
- Props: color, value, x, y
|
|
|
|
|
|
|
|
|
|
|
|
- **[ArduinoUno.tsx](frontend/src/components/components-wokwi/ArduinoUno.tsx)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- React wrapper for `<wokwi-arduino-uno>`
|
|
|
|
|
|
- Internal LED control (pin 13)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
- **[Resistor.tsx](frontend/src/components/components-wokwi/Resistor.tsx)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- React wrapper for `<wokwi-resistor>`
|
2026-03-03 10:20:49 +07:00
|
|
|
|
- Props: value (ohms)
|
|
|
|
|
|
|
|
|
|
|
|
- **[Pushbutton.tsx](frontend/src/components/components-wokwi/Pushbutton.tsx)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- React wrapper for `<wokwi-pushbutton>`
|
2026-03-03 10:20:49 +07:00
|
|
|
|
- Events: onPress, onRelease
|
|
|
|
|
|
|
|
|
|
|
|
### Backend
|
|
|
|
|
|
|
|
|
|
|
|
#### 1. API Routes
|
|
|
|
|
|
- **[compile.py](backend/app/api/routes/compile.py)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- `POST /api/compile`: Compile code
|
|
|
|
|
|
- `GET /api/compile/boards`: List boards
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
#### 2. Services
|
|
|
|
|
|
- **[arduino_cli.py](backend/app/services/arduino_cli.py)**
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- `compile()`: Compile sketch with arduino-cli
|
|
|
|
|
|
- `list_boards()`: Get available boards
|
|
|
|
|
|
- Temporary directory management
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### Wokwi Libraries (Cloned Locally)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
#### 1. wokwi-elements
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- **Location**: `wokwi-libs/wokwi-elements/`
|
|
|
|
|
|
- **Build**: `dist/esm/` and `dist/cjs/`
|
|
|
|
|
|
- **Components**: 50+ electronic elements
|
|
|
|
|
|
- **Technology**: Lit (Web Components)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
#### 2. avr8js
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- **Location**: `wokwi-libs/avr8js/`
|
|
|
|
|
|
- **Build**: `dist/esm/` and `dist/cjs/`
|
|
|
|
|
|
- **Functionality**: Complete ATmega328p emulator
|
|
|
|
|
|
- **Supports**: CPU, Timers, USART, GPIO, ADC, etc.
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
#### 3. rp2040js
|
2026-03-04 06:42:17 +07:00
|
|
|
|
- **Location**: `wokwi-libs/rp2040js/`
|
|
|
|
|
|
- **Future use**: Raspberry Pi Pico support
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
## Vite Integration
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
### Alias Configuration
|
|
|
|
|
|
```typescript
|
|
|
|
|
|
// vite.config.ts
|
|
|
|
|
|
resolve: {
|
|
|
|
|
|
alias: {
|
|
|
|
|
|
'avr8js': path.resolve(__dirname, '../wokwi-libs/avr8js/dist/esm'),
|
|
|
|
|
|
'@wokwi/elements': path.resolve(__dirname, '../wokwi-libs/wokwi-elements/dist/esm'),
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
This allows:
|
|
|
|
|
|
- Use local repos instead of npm
|
|
|
|
|
|
- Easy updates with `git pull`
|
|
|
|
|
|
- Modify source code if needed
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
## Technology Stack
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
### Frontend
|
2026-03-04 06:42:17 +07:00
|
|
|
|
| Technology | Version | Purpose |
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|------------|---------|-----------|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
| React | 19.2 | UI framework |
|
2026-03-03 10:20:49 +07:00
|
|
|
|
| Vite | 7.3 | Build tool & dev server |
|
2026-03-04 06:42:17 +07:00
|
|
|
|
| TypeScript | 5.9 | Static typing |
|
|
|
|
|
|
| Monaco Editor | 4.7 | Code editor (VSCode) |
|
2026-03-03 10:20:49 +07:00
|
|
|
|
| Zustand | 5.0 | State management |
|
|
|
|
|
|
| Axios | 1.13 | HTTP client |
|
2026-03-04 06:42:17 +07:00
|
|
|
|
| wokwi-elements | 1.9.2 | Electronic components |
|
|
|
|
|
|
| avr8js | 0.21.0 | AVR8 emulator |
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
### Backend
|
2026-03-04 06:42:17 +07:00
|
|
|
|
| Technology | Version | Purpose |
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|------------|---------|-----------|
|
|
|
|
|
|
| Python | 3.12+ | Runtime |
|
|
|
|
|
|
| FastAPI | 0.115 | Web framework |
|
|
|
|
|
|
| Uvicorn | 0.32 | ASGI server |
|
2026-03-04 06:42:17 +07:00
|
|
|
|
| SQLAlchemy | 2.0 | ORM (future) |
|
|
|
|
|
|
| aiosqlite | 0.20 | Async DB (future) |
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### External Tools
|
|
|
|
|
|
| Tool | Purpose |
|
|
|
|
|
|
|------|---------|
|
|
|
|
|
|
| arduino-cli | Arduino compiler |
|
|
|
|
|
|
| Git | Version control for Wokwi libs |
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
## Architecture Advantages
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### ✅ Separation of Concerns
|
|
|
|
|
|
- **Frontend**: UI, UX, visualization
|
|
|
|
|
|
- **Backend**: Compilation, business logic
|
|
|
|
|
|
- **Wokwi Libs**: Emulation and components (maintained by Wokwi)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### ✅ Wokwi Compatibility
|
|
|
|
|
|
- Official repositories = same functionality
|
|
|
|
|
|
- Automatic updates with `git pull`
|
|
|
|
|
|
- New components available immediately
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### ✅ Scalability
|
|
|
|
|
|
- Frontend can easily add more components
|
|
|
|
|
|
- Backend can add more endpoints (projects, sensors)
|
|
|
|
|
|
- Wokwi libs update independently
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### ✅ Local Development
|
|
|
|
|
|
- No internet required to work
|
|
|
|
|
|
- Local compilation with arduino-cli
|
|
|
|
|
|
- Local database (SQLite)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
## Upcoming Improvements
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### Phase 2: Real Emulation (avr8js)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
2026-03-04 06:42:17 +07:00
|
|
|
|
[ ] Implement AVRSimulator.ts
|
|
|
|
|
|
[ ] Intel HEX file parser
|
|
|
|
|
|
[ ] PinManager with write hooks
|
|
|
|
|
|
[ ] Integrate CPU execution loop
|
|
|
|
|
|
[ ] Map Arduino pins to components
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### Phase 3: More Components
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
2026-03-04 06:42:17 +07:00
|
|
|
|
[ ] Integrate more wokwi-elements
|
|
|
|
|
|
[ ] Buttons, potentiometers
|
|
|
|
|
|
[ ] Sensors (DHT22, HC-SR04)
|
|
|
|
|
|
[ ] Displays (LCD, 7-segment)
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### Phase 4: Persistence
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
2026-03-04 06:42:17 +07:00
|
|
|
|
[ ] SQLite database
|
|
|
|
|
|
[ ] SQLAlchemy models
|
|
|
|
|
|
[ ] Project CRUD
|
|
|
|
|
|
[ ] Save circuits as JSON
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
### Phase 5: Advanced Features
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
|
|
|
|
|
[ ] Serial Monitor
|
2026-03-04 06:42:17 +07:00
|
|
|
|
[ ] Visual wiring (drag & drop)
|
|
|
|
|
|
[ ] Multiple boards (Mega, Nano, ESP32)
|
|
|
|
|
|
[ ] Export to Wokwi.com
|
2026-03-03 10:20:49 +07:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-03-04 06:42:17 +07:00
|
|
|
|
## References
|
2026-03-03 10:20:49 +07:00
|
|
|
|
|
|
|
|
|
|
- [Wokwi Elements Repo](https://github.com/wokwi/wokwi-elements)
|
|
|
|
|
|
- [AVR8js Repo](https://github.com/wokwi/avr8js)
|
|
|
|
|
|
- [Wokwi Simulator](https://wokwi.com)
|
|
|
|
|
|
- [Arduino CLI](https://arduino.github.io/arduino-cli/)
|
|
|
|
|
|
- [FastAPI Docs](https://fastapi.tiangolo.com/)
|
|
|
|
|
|
- [Vite Docs](https://vitejs.dev/)
|