State Management & Data Flow
Relevant source files
The following files were used as context for generating this wiki page:
ComChan manages state through a centralized configuration model and specialized mode-based state machines. Data flows from various input sources—including physical Serial ports, BLE streams, RTT probes, and simulated sources—into a processing pipeline that handles TUI rendering, file logging, and CSV exporting. The system employs a "Hot-Swapping" architecture, allowing users to transition between the raw Serial Monitor and the 2D/3D Plotter while maintaining the active connection handle.
Sources: src/main.rs:18-35, src/plotter.rs:604-625, src/monitor.rs:777-787
Centralized Configuration State
The application state is initialized via MergedConfig, which resolves conflicts between file-based settings (comchan.toml) and Command Line Interface (CLI) arguments. This immutable configuration object dictates the behavior of all downstream modules.
Configuration Fields
| Field | Type | Description |
|---|---|---|
port | Option<Vec<String>> | List of target serial ports or stream identifiers. |
baud | u32 | Communication speed for serial interfaces. |
plot | bool | Toggle for initial launch into Plotter mode. |
simulate | bool | Enables mock data generation for testing. |
rtt | bool | Enables Segger RTT/defmt debug probe mode. |
ble | bool | Enables Bluetooth Low Energy (NUS) streaming. |
Sources: src/config.rs:159-188, src/config.rs:324-353
Application Lifecycle & Mode Switching
ComChan utilizes an AppExitState enum to manage transitions between the main execution loops. The main function operates a loop that re-invokes the relevant mode handler based on the returned state, passing ownership of active communication handles (Serial port or RTT reader) to prevent connection drops during UI transitions.
Sources: src/main.rs:18-35, src/main.rs:136-189
Data Ingestion & Transformation
Data ingestion is handled via a multi-source polling strategy. In PlotterMode, a PlotterState struct maintains the internal telemetry history, including sensor maps and global bounds for auto-scaling.
Processing Pipeline
- Ingestion: Data is read from
serialport,btleplug(BLE), orprobe-rs(RTT) into a local buffer. - Accumulation: The system drains the OS buffer until a newline (
\n) is detected. - Parsing: Lines are processed to extract numeric sensor values (e.g.,
Pitch: 10.5). - Distribution: Parsed data is simultaneously sent to the TUI state, the
CsvStreamer, and theBufWriterfor logging.
Internal Module States
Plotter State Management
The PlotterState struct tracks ordered sensor data to ensure consistent legend rendering and color mapping. It also manages interpolation for 3D spatial telemetry to provide smooth rotations even with low-frequency sensor updates.
struct PlotterState {
sensors: HashMap<String, SensorData>,
sensor_order: Vec<String>,
x: f64,
global_y_min: f64,
global_y_max: f64,
paused: bool,
display_pitch: f64,
display_yaw: f64,
display_roll: f64,
// ...
}
Sources: src/plotter.rs:64-96
Dual Monitor State
In dual-port mode, the application spawns two independent serial threads that communicate with the main TUI thread via a Multi-Producer Single-Consumer (mpsc) channel. The DualMonitorState manages independent scroll positions and log buffers for two distinct split panes.
Sources: src/dual_ports.rs:46-65, src/dual_ports.rs:82-166
Export & Logging Flow
ComChan supports continuous data streaming to disk through the CsvStreamer and BufWriter components. The export_to_svg function generates a static snapshot of the current PlotterState history, applying styling based on the active dark_mode configuration.
Sources: src/export.rs:13-113, src/export.rs:133-167
Summary
The state management of ComChan is designed for low-latency feedback. By decoupling the communication threads from the rendering engine and using mpsc channels for control signals (e.g., MonitorCommand::SwitchMode), the project achieves high throughput for embedded telemetry while maintaining a responsive user interface across multiple modes.