Hardware Simulation Mode
Relevant source files
The following files were used as context for generating this wiki page:
Hardware Simulation Mode is a feature in ComChan designed to allow users to test the application's monitoring and plotting capabilities without requiring physical serial hardware. It generates mock sensor data internally, enabling developers to verify UI layouts, data parsing logic, and 3D telemetry visualizations in a controlled environment.
This mode is particularly useful for debugging the Serial Plotter or testing Dual Monitor setups. It bypasses actual serial port initialization, instead routing synthetic data through the same ingestion pipelines used by real hardware interfaces.
Sources: README.md:129-129, src/main.rs:77-80
Activation and Configuration
Hardware Simulation Mode is triggered via the --simulate CLI flag or by setting the simulate field to true in the comchan.toml configuration file. When active, the system bypasses standard serial port discovery and instead identifies the connection as a virtual "SIMULATE_PORT".
Sources: src/config.rs:113, src/config.rs:188, src/main.rs:82-82
Configuration Parameters
The following configuration options directly influence the behavior of the simulation:
| Parameter | Type | Description |
|---|---|---|
simulate | bool | Enables/disables the mock data generation. |
plot | bool | Determines if the simulated data is sent to the TUI plotter. |
plot_points | usize | The number of simulated data points to retain in the buffer. |
port | Vec | Used in dual-mode to name mock ports (e.g., mock1, mock2). |
Sources: src/config.rs:101-125, README.md:94-94
Simulation Data Flow
When enabled, the application bypasses the serialport::open() logic and enters a loop where synthetic strings are generated. This data is then fed into the ingest_line function (in Plotter mode) or printed to the terminal (in Monitor mode).
The diagram above illustrates how the simulation logic branches off early in the execution flow to prevent hardware errors.
Sources: src/main.rs:77-83, src/plotter.rs:379-389, src/monitor.rs:412-427
Serial Plotter Integration
In the Serial Plotter, Hardware Simulation Mode generates specific telemetry data to demonstrate the 3D capabilities of the engine. It simulates three primary axes of movement:
- Pitch: Calculated using
(t * 0.5).sin() * 45.0. - Roll: Calculated using
(t * 0.8).cos() * 30.0. - Yaw: Calculated as a continuous rotation
(t * 2.0) % 360.0.
These values are formatted as standard sensor strings (e.g., "Pitch: 22.50") and ingested every 50ms. This allows the WireframeWidget to render a rotating 3D model (Cube, Tetrahedron, or Octahedron) without external input.
Sources: src/plotter.rs:379-389, src/plotter.rs:556-578
Monitor Mode Simulation
When running in standard monitor mode with simulation enabled, ComChan generates mock environmental data strings. This includes:
- Temperature: Fluctuates based on a sine wave.
- Humidity: Fluctuates based on a cosine wave.
- Pressure: Fluctuates based on a secondary sine wave.
The simulation loop in monitor mode includes a 500ms delay between updates to mimic a typical low-speed embedded sensor output.
Sources: src/monitor.rs:412-430
Dual Monitor Simulation
The simulation mode extends to the Dual Monitor side-by-side view. Users can simulate two independent data streams by passing the --simulate flag along with two mock port names.
comchan --simulate -p mock1 mock2
In this configuration, the dual_ports::run_dual_mode architecture manages two separate virtual streams, allowing for testing of split-pane scrolling and independent logging features.
Sources: README.md:94-94, src/main.rs:71-76
Implementation Details
The simulation logic is implemented as a conditional branch within the main loops of the plotter and monitor modules. By checking config.simulate, the software avoids calls to port.read() or port.bytes_to_read(), which would otherwise fail without physical hardware connected.
// src/plotter.rs:379-389
if config.simulate {
let t = state.x * 0.1;
let pitch = (t * 0.5).sin() * 45.0;
let roll = (t * 0.8).cos() * 30.0;
let yaw = (t * 2.0) % 360.0;
state.ingest_line(&format!("Pitch: {:.2}", pitch), config.plot_points);
state.ingest_line(&format!("Roll: {:.2}", roll), config.plot_points);
state.ingest_line(&format!("Yaw: {:.2}", yaw), config.plot_points);
std::thread::sleep(Duration::from_millis(50));
}
Sources: src/plotter.rs:379-389, src/monitor.rs:412-430
Hardware Simulation Mode ensures that ComChan remains accessible and testable even when a developer does not have access to microcontrollers or serial adapters, providing a consistent "sandbox" for development and demonstration.