Lin_Simulator/docs/step2_ldf_loading.md
Mohamed Salem cb60c2ad5d Steps 2-7: LDF loading, signal editing, Rx display, connection, BabyLIN backend, scheduler
Step 2 - LDF Loading:
- ldfparser integration (Python) / custom regex parser (C++)
- QTreeWidget with expandable signal rows, merged Value column
- Hex/Dec toggle, FreeFormat schedule entries, auto-reload
- Baud rate auto-detection from LDF

Step 3 - Signal Editing:
- Bit packing/unpacking (signal value ↔ frame bytes)
- ReadOnlyColumnDelegate for per-column editability
- Value clamping to signal width, recursion guard

Step 4 - Rx Panel:
- receive_rx_frame() API with timestamp, signal unpacking
- Change highlighting (yellow), auto-scroll toggle, clear button
- Dashboard view (in-place update per frame_id)

Step 5 - Connection Panel:
- ConnectionManager with state machine (Disconnected/Connecting/Connected/Error)
- Port scanning (pyserial / QSerialPort), connect/disconnect with UI mapping

Step 6 - BabyLIN Backend:
- BabyLinBackend wrapping Lipowsky BabyLIN_library.py DLL
- Mock mode for macOS/CI, device scan, SDF loading, signal access
- Frame callbacks, raw command access

Step 7 - Master Scheduler:
- QTimer-based schedule execution with start/stop/pause
- Frame sent callback with visual highlighting
- Mock Rx simulation, manual send, global rate override

Tests: Python 171 | C++ 124 (Steps 1-5 parity, Steps 6-7 Python-first)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 14:21:24 +02:00

71 lines
3.2 KiB
Markdown

# Step 2 — LDF Loading & Display
## What Was Built
LDF file parsing, GUI table population, baud rate detection, schedule table loading, and auto-reload on file change.
## Architecture
```
User clicks Browse → QFileDialog → file_path
_load_ldf_file(path)
┌────────────┼────────────┐
▼ ▼ ▼
parse_ldf() On error: Setup file
(ldf_handler) show dialog watcher
LdfData
┌────┼────────────┬──────────────┐
▼ ▼ ▼ ▼
Baud Tx table Rx table Schedule
rate (master (slave dropdown
label frames) frames)
```
## Key Module: ldf_handler.py
Adapter between `ldfparser` library and our GUI. Converts complex library objects into simple dataclasses:
| Dataclass | Purpose |
|-----------|---------|
| `LdfData` | Complete parsed result — baudrate, frames, schedules |
| `FrameInfo` | One frame: name, ID, publisher, length, is_master_tx, signals |
| `SignalInfo` | One signal: name, bit_offset, width, init_value |
| `ScheduleTableInfo` | One schedule: name, entries [(frame_name, delay_ms)] |
### ldfparser API notes
- `ldf.baudrate` returns bps * 1000 (19200 kbps → 19200000) — divide by 1000
- `frame.signal_map` is list of (bit_offset, LinSignal) tuples
- `frame.publisher` is LinMaster or LinSlave — use isinstance() to classify
- Schedule delay is in seconds (0.01 = 10ms) — multiply by 1000
## Features
- **LDF parsing**: Extracts frames, signals, schedules, baud rate
- **Tx table**: Populated with master frames (name, ID, length, interval, data, signals)
- **Rx table**: Prepared with slave frame definitions (data filled at runtime)
- **Baud rate**: Auto-detected from LDF's LIN_speed field
- **Schedule dropdown**: Populated with schedule table names
- **Per-frame intervals**: Auto-filled from first schedule table
- **Auto-reload**: QFileSystemWatcher detects file changes, re-parses automatically
- **Error handling**: Invalid files show error dialog, don't corrupt GUI state
## Files
| File | Purpose |
|------|---------|
| `python/src/ldf_handler.py` | LDF parsing adapter — parse_ldf() → LdfData |
| `python/src/main_window.py` | Updated with _load_ldf_file(), table population, file watcher |
| `python/tests/test_ldf_handler.py` | 27 tests for the parsing layer |
| `python/tests/test_ldf_loading.py` | 20 tests for GUI integration |
| `resources/sample.ldf` | Sample LIN 2.1 LDF with 4 frames, 2 schedule tables |
## Test Results
- 79 total tests passing (Step 1: 32 + Step 2: 47)
- Parser tests: valid/invalid files, frame classification, signal extraction, schedules
- GUI tests: table population, baud rate, schedule combo, error handling, auto-reload