2 Commits

Author SHA1 Message Date
Martino Ferrari
aaf69c0949 Added record to file 2026-02-24 22:59:37 +01:00
Martino Ferrari
ec29bd5148 Scope like ui start 2026-02-23 18:01:24 +01:00
5 changed files with 1096 additions and 83 deletions

View File

@@ -28,6 +28,14 @@ Implement a "Zero-Code-Change" observability layer for the MARTe2 real-time fram
- **FR-09 (Navigation):** - **FR-09 (Navigation):**
- Context menus for resetting zoom (X, Y, or both). - Context menus for resetting zoom (X, Y, or both).
- "Fit to View" functionality that automatically scales both axes to encompass all available buffered data points. - "Fit to View" functionality that automatically scales both axes to encompass all available buffered data points.
- **FR-10 (Scope Mode):**
- High-performance oscilloscope mode with configurable time windows (10ms to 10s).
- Global synchronization of time axes across all plot panels.
- Support for Free-run and Triggered acquisition (Single/Continuous, rising/falling edges).
- **FR-11 (Data Recording):**
- Record any traced signal to disk in Parquet format.
- Native file dialog for destination selection.
- Visual recording indicator in the GUI.
### 2.2 Technical Constraints (TC) ### 2.2 Technical Constraints (TC)
- **TC-01:** No modifications allowed to the MARTe2 core library or component source code. - **TC-01:** No modifications allowed to the MARTe2 core library or component source code.

View File

@@ -42,7 +42,7 @@
} }
} }
+Logger = { +Logger = {
Class = LoggerDataSource Class = GAMDataSource
Signals = { Signals = {
CounterCopy = { CounterCopy = {
Type = uint32 Type = uint32

File diff suppressed because it is too large Load Diff

View File

@@ -12,4 +12,7 @@ chrono = "0.4"
crossbeam-channel = "0.5" crossbeam-channel = "0.5"
regex = "1.10" regex = "1.10"
socket2 = { version = "0.5", features = ["all"] } socket2 = { version = "0.5", features = ["all"] }
once_cell = "1.21.3" once_cell = "1.21"
rfd = "0.15"
parquet = { version = "53.0", features = ["arrow"] }
arrow = "53.0"

View File

@@ -1,8 +1,9 @@
use eframe::egui; use eframe::egui;
use egui_plot::{Line, Plot, PlotPoints, MarkerShape, LineStyle, PlotBounds}; use egui_plot::{Line, Plot, PlotPoints, MarkerShape, LineStyle, PlotBounds, VLine};
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use std::net::{TcpStream, UdpSocket}; use std::net::{TcpStream, UdpSocket};
use std::io::{Write, BufReader, BufRead}; use std::io::{Write, BufReader, BufRead};
use std::fs::File;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -11,6 +12,12 @@ use crossbeam_channel::{unbounded, Receiver, Sender};
use socket2::{Socket, Domain, Type, Protocol}; use socket2::{Socket, Domain, Type, Protocol};
use regex::Regex; use regex::Regex;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rfd::FileDialog;
use arrow::array::{Float64Array, Array};
use arrow::record_batch::RecordBatch;
use arrow::datatypes::{DataType, Field, Schema};
use parquet::arrow::arrow_writer::ArrowWriter;
use parquet::file::properties::WriterProperties;
static APP_START_TIME: Lazy<std::time::Instant> = Lazy::new(std::time::Instant::now); static APP_START_TIME: Lazy<std::time::Instant> = Lazy::new(std::time::Instant::now);
@@ -56,6 +63,8 @@ struct LogEntry {
struct TraceData { struct TraceData {
values: VecDeque<[f64; 2]>, values: VecDeque<[f64; 2]>,
last_value: f64, last_value: f64,
recording_tx: Option<Sender<[f64; 2]>>,
recording_path: Option<String>,
} }
struct SignalMetadata { struct SignalMetadata {
@@ -78,6 +87,25 @@ enum PlotType {
LogicAnalyzer, LogicAnalyzer,
} }
#[derive(Clone, Copy, PartialEq, Debug)]
enum AcquisitionMode {
FreeRun,
Triggered,
}
#[derive(Clone, Copy, PartialEq, Debug)]
enum TriggerEdge {
Rising,
Falling,
Both,
}
#[derive(Clone, Copy, PartialEq, Debug)]
enum TriggerType {
Single,
Continuous,
}
#[derive(Clone, Copy, PartialEq, Debug)] #[derive(Clone, Copy, PartialEq, Debug)]
enum MarkerType { enum MarkerType {
None, None,
@@ -127,6 +155,8 @@ enum InternalEvent {
ClearTrace(String), ClearTrace(String),
UdpStats(u64), UdpStats(u64),
UdpDropped(u32), UdpDropped(u32),
RecordPathChosen(String, String), // SignalName, FilePath
RecordingError(String, String), // SignalName, ErrorMessage
} }
// --- App State --- // --- App State ---
@@ -145,6 +175,21 @@ struct LogFilters {
content_regex: String, content_regex: String,
} }
struct ScopeSettings {
enabled: bool,
window_ms: f64,
mode: AcquisitionMode,
paused: bool,
trigger_type: TriggerType,
trigger_source: String,
trigger_edge: TriggerEdge,
trigger_threshold: f64,
pre_trigger_percent: f64,
trigger_active: bool,
last_trigger_time: f64,
is_armed: bool,
}
struct MarteDebugApp { struct MarteDebugApp {
connected: bool, connected: bool,
is_breaking: bool, is_breaking: bool,
@@ -170,6 +215,7 @@ struct MarteDebugApp {
rx_events: Receiver<InternalEvent>, rx_events: Receiver<InternalEvent>,
internal_tx: Sender<InternalEvent>, internal_tx: Sender<InternalEvent>,
shared_x_range: Option<[f64; 2]>, shared_x_range: Option<[f64; 2]>,
scope: ScopeSettings,
} }
impl MarteDebugApp { impl MarteDebugApp {
@@ -204,6 +250,11 @@ impl MarteDebugApp {
forcing_dialog: None, style_editor: None, forcing_dialog: None, style_editor: None,
tx_cmd, rx_events, internal_tx, tx_cmd, rx_events, internal_tx,
shared_x_range: None, shared_x_range: None,
scope: ScopeSettings {
enabled: false, window_ms: 1000.0, mode: AcquisitionMode::FreeRun, paused: false,
trigger_type: TriggerType::Continuous, trigger_source: "".to_string(), trigger_edge: TriggerEdge::Rising, trigger_threshold: 0.0, pre_trigger_percent: 25.0,
trigger_active: false, last_trigger_time: 0.0, is_armed: true,
},
} }
} }
@@ -217,6 +268,34 @@ impl MarteDebugApp {
colors[idx % colors.len()] colors[idx % colors.len()]
} }
fn apply_trigger_logic(&mut self) {
if self.scope.mode != AcquisitionMode::Triggered || !self.scope.is_armed { return; }
if self.scope.trigger_source.is_empty() { return; }
let data_map = self.traced_signals.lock().unwrap();
if let Some(data) = data_map.get(&self.scope.trigger_source) {
if data.values.len() < 2 { return; }
let start_idx = if data.values.len() > 100 { data.values.len() - 100 } else { 0 };
for i in (start_idx + 1..data.values.len()).rev() {
let v_prev = data.values[i-1][1];
let v_curr = data.values[i][1];
let t_curr = data.values[i][0];
if t_curr <= self.scope.last_trigger_time { continue; }
let triggered = match self.scope.trigger_edge {
TriggerEdge::Rising => v_prev < self.scope.trigger_threshold && v_curr >= self.scope.trigger_threshold,
TriggerEdge::Falling => v_prev > self.scope.trigger_threshold && v_curr <= self.scope.trigger_threshold,
TriggerEdge::Both => (v_prev < self.scope.trigger_threshold && v_curr >= self.scope.trigger_threshold) ||
(v_prev > self.scope.trigger_threshold && v_curr <= self.scope.trigger_threshold),
};
if triggered {
self.scope.last_trigger_time = t_curr;
self.scope.trigger_active = true;
if self.scope.trigger_type == TriggerType::Single { self.scope.is_armed = false; }
break;
}
}
}
}
fn render_tree(&mut self, ui: &mut egui::Ui, item: &TreeItem, path: String) { fn render_tree(&mut self, ui: &mut egui::Ui, item: &TreeItem, path: String) {
let current_path = if path.is_empty() { if item.name == "Root" { "".to_string() } else { item.name.clone() } } let current_path = if path.is_empty() { if item.name == "Root" { "".to_string() } else { item.name.clone() } }
else { if path.is_empty() { item.name.clone() } else { format!("{}.{}", path, item.name) } }; else { if path.is_empty() { item.name.clone() } else { format!("{}.{}", path, item.name) } };
@@ -303,6 +382,31 @@ fn tcp_log_worker(shared_config: Arc<Mutex<ConnectionConfig>>, tx_events: Sender
} }
} }
fn recording_worker(rx: Receiver<[f64; 2]>, path: String, signal_name: String, tx_events: Sender<InternalEvent>) {
let file = match File::create(&path) {
Ok(f) => f,
Err(e) => { let _ = tx_events.send(InternalEvent::RecordingError(signal_name, format!("File Error: {}", e))); return; }
};
let schema = Arc::new(Schema::new(vec![Field::new("timestamp", DataType::Float64, false), Field::new("value", DataType::Float64, false)]));
let mut writer = match ArrowWriter::try_new(file, schema.clone(), Some(WriterProperties::builder().build())) {
Ok(w) => w,
Err(e) => { let _ = tx_events.send(InternalEvent::RecordingError(signal_name, format!("Parquet Error: {}", e))); return; }
};
let (mut t_acc, mut v_acc) = (Vec::with_capacity(1000), Vec::with_capacity(1000));
while let Ok([t, v]) = rx.recv() {
t_acc.push(t); v_acc.push(v);
if t_acc.len() >= 1000 {
let batch = RecordBatch::try_new(schema.clone(), vec![Arc::new(Float64Array::from(t_acc.clone())), Arc::new(Float64Array::from(v_acc.clone()))]).unwrap();
let _ = writer.write(&batch); t_acc.clear(); v_acc.clear();
}
}
if !t_acc.is_empty() {
let batch = RecordBatch::try_new(schema.clone(), vec![Arc::new(Float64Array::from(t_acc)), Arc::new(Float64Array::from(v_acc))]).unwrap();
let _ = writer.write(&batch);
}
let _ = writer.close();
}
fn udp_worker(shared_config: Arc<Mutex<ConnectionConfig>>, id_to_meta: Arc<Mutex<HashMap<u32, SignalMetadata>>>, traced_data: Arc<Mutex<HashMap<String, TraceData>>>, tx_events: Sender<InternalEvent>) { fn udp_worker(shared_config: Arc<Mutex<ConnectionConfig>>, id_to_meta: Arc<Mutex<HashMap<u32, SignalMetadata>>>, traced_data: Arc<Mutex<HashMap<String, TraceData>>>, tx_events: Sender<InternalEvent>) {
let mut current_version = 0; let mut current_version = 0;
let mut socket: Option<UdpSocket> = None; let mut socket: Option<UdpSocket> = None;
@@ -336,17 +440,14 @@ fn udp_worker(shared_config: Arc<Mutex<ConnectionConfig>>, id_to_meta: Arc<Mutex
total_packets += 1; total_packets += 1;
if (total_packets % 500) == 0 { let _ = tx_events.send(InternalEvent::UdpStats(total_packets)); } if (total_packets % 500) == 0 { let _ = tx_events.send(InternalEvent::UdpStats(total_packets)); }
if n < 20 { continue; } if n < 20 { continue; }
let mut magic_buf = [0u8; 4]; magic_buf.copy_from_slice(&buf[0..4]); if u32::from_le_bytes(buf[0..4].try_into().unwrap()) != 0xDA7A57AD { continue; }
if u32::from_le_bytes(magic_buf) != 0xDA7A57AD { continue; } let seq = u32::from_le_bytes(buf[4..8].try_into().unwrap());
let mut seq_buf = [0u8; 4]; seq_buf.copy_from_slice(&buf[4..8]);
let seq = u32::from_le_bytes(seq_buf);
if let Some(last) = last_seq { if seq != last + 1 && seq > last { let _ = tx_events.send(InternalEvent::UdpDropped(seq - last - 1)); } } if let Some(last) = last_seq { if seq != last + 1 && seq > last { let _ = tx_events.send(InternalEvent::UdpDropped(seq - last - 1)); } }
last_seq = Some(seq); last_seq = Some(seq);
let count = u32::from_le_bytes(buf[16..20].try_into().unwrap()); let count = u32::from_le_bytes(buf[16..20].try_into().unwrap());
let now = APP_START_TIME.elapsed().as_secs_f64(); let now = APP_START_TIME.elapsed().as_secs_f64();
let mut offset = 20; let mut offset = 20;
let mut local_updates: HashMap<String, Vec<[f64; 2]>> = HashMap::new(); let (mut local_updates, mut last_values): (HashMap<String, Vec<[f64; 2]>>, HashMap<String, f64>) = (HashMap::new(), HashMap::new());
let mut last_values: HashMap<String, f64> = HashMap::new();
let metas = id_to_meta.lock().unwrap(); let metas = id_to_meta.lock().unwrap();
for _ in 0..count { for _ in 0..count {
if offset + 8 > n { break; } if offset + 8 > n { break; }
@@ -373,9 +474,9 @@ fn udp_worker(shared_config: Arc<Mutex<ConnectionConfig>>, id_to_meta: Arc<Mutex
let mut data_map = traced_data.lock().unwrap(); let mut data_map = traced_data.lock().unwrap();
for (name, new_points) in local_updates { for (name, new_points) in local_updates {
if let Some(entry) = data_map.get_mut(&name) { if let Some(entry) = data_map.get_mut(&name) {
for point in new_points { entry.values.push_back(point); } for point in new_points { entry.values.push_back(point); if let Some(tx) = &entry.recording_tx { let _ = tx.send(point); } }
if let Some(lv) = last_values.get(&name) { entry.last_value = *lv; } if let Some(lv) = last_values.get(&name) { entry.last_value = *lv; }
while entry.values.len() > 10000 { entry.values.pop_front(); } while entry.values.len() > 100000 { entry.values.pop_front(); }
} }
} }
} }
@@ -392,7 +493,7 @@ impl eframe::App for MarteDebugApp {
InternalEvent::Discovery(signals) => { let mut metas = self.id_to_meta.lock().unwrap(); metas.clear(); for s in &signals { let meta = metas.entry(s.id).or_insert_with(|| SignalMetadata { names: Vec::new(), sig_type: s.sig_type.clone() }); if !meta.names.contains(&s.name) { meta.names.push(s.name.clone()); } } } InternalEvent::Discovery(signals) => { let mut metas = self.id_to_meta.lock().unwrap(); metas.clear(); for s in &signals { let meta = metas.entry(s.id).or_insert_with(|| SignalMetadata { names: Vec::new(), sig_type: s.sig_type.clone() }); if !meta.names.contains(&s.name) { meta.names.push(s.name.clone()); } } }
InternalEvent::Tree(tree) => { self.app_tree = Some(tree); } InternalEvent::Tree(tree) => { self.app_tree = Some(tree); }
InternalEvent::NodeInfo(info) => { self.node_info = info; } InternalEvent::NodeInfo(info) => { self.node_info = info; }
InternalEvent::TraceRequested(name) => { let mut data_map = self.traced_signals.lock().unwrap(); data_map.entry(name).or_insert_with(|| TraceData { values: VecDeque::with_capacity(10000), last_value: 0.0 }); } InternalEvent::TraceRequested(name) => { let mut data_map = self.traced_signals.lock().unwrap(); data_map.entry(name).or_insert_with(|| TraceData { values: VecDeque::with_capacity(10000), last_value: 0.0, recording_tx: None, recording_path: None }); }
InternalEvent::ClearTrace(name) => { let mut data_map = self.traced_signals.lock().unwrap(); data_map.remove(&name); for plot in &mut self.plots { plot.signals.retain(|s| s.source_name != name); } } InternalEvent::ClearTrace(name) => { let mut data_map = self.traced_signals.lock().unwrap(); data_map.remove(&name); for plot in &mut self.plots { plot.signals.retain(|s| s.source_name != name); } }
InternalEvent::UdpStats(count) => { self.udp_packets = count; } InternalEvent::UdpStats(count) => { self.udp_packets = count; }
InternalEvent::UdpDropped(dropped) => { self.udp_dropped += dropped as u64; } InternalEvent::UdpDropped(dropped) => { self.udp_dropped += dropped as u64; }
@@ -400,8 +501,23 @@ impl eframe::App for MarteDebugApp {
InternalEvent::Disconnected => { self.connected = false; } InternalEvent::Disconnected => { self.connected = false; }
InternalEvent::InternalLog(msg) => { self.logs.push_back(LogEntry { time: Local::now().format("%H:%M:%S").to_string(), level: "GUI_ERROR".to_string(), message: msg }); } InternalEvent::InternalLog(msg) => { self.logs.push_back(LogEntry { time: Local::now().format("%H:%M:%S").to_string(), level: "GUI_ERROR".to_string(), message: msg }); }
InternalEvent::CommandResponse(resp) => { self.logs.push_back(LogEntry { time: Local::now().format("%H:%M:%S").to_string(), level: "CMD_RESP".to_string(), message: resp }); } InternalEvent::CommandResponse(resp) => { self.logs.push_back(LogEntry { time: Local::now().format("%H:%M:%S").to_string(), level: "CMD_RESP".to_string(), message: resp }); }
InternalEvent::RecordPathChosen(name, path) => {
let mut data_map = self.traced_signals.lock().unwrap();
if let Some(entry) = data_map.get_mut(&name) {
let (tx, rx) = unbounded();
entry.recording_tx = Some(tx);
entry.recording_path = Some(path.clone());
let tx_err = self.internal_tx.clone();
thread::spawn(move || { recording_worker(rx, path, name, tx_err); });
} }
} }
InternalEvent::RecordingError(name, err) => {
self.logs.push_back(LogEntry { time: Local::now().format("%H:%M:%S").to_string(), level: "REC_ERROR".to_string(), message: format!("{}: {}", name, err) });
}
}
}
if self.scope.enabled { self.apply_trigger_logic(); }
if let Some(dragged_name) = ctx.data_mut(|d| d.get_temp::<String>(egui::Id::new("drag_signal"))) { if let Some(dragged_name) = ctx.data_mut(|d| d.get_temp::<String>(egui::Id::new("drag_signal"))) {
egui::Area::new(egui::Id::new("drag_ghost")).fixed_pos(ctx.input(|i| i.pointer.hover_pos().unwrap_or(egui::Pos2::ZERO))).order(egui::Order::Tooltip).show(ctx, |ui| { ui.group(|ui| { ui.label(format!("📈 {}", dragged_name)); }); }); egui::Area::new(egui::Id::new("drag_ghost")).fixed_pos(ctx.input(|i| i.pointer.hover_pos().unwrap_or(egui::Pos2::ZERO))).order(egui::Order::Tooltip).show(ctx, |ui| { ui.group(|ui| { ui.label(format!("📈 {}", dragged_name)); }); });
@@ -438,22 +554,39 @@ impl eframe::App for MarteDebugApp {
ui.separator(); ui.separator();
if ui.button(" Plot").clicked() { self.plots.push(PlotInstance { id: format!("Plot {}", self.plots.len()+1), plot_type: PlotType::Normal, signals: Vec::new(), auto_bounds: true }); } if ui.button(" Plot").clicked() { self.plots.push(PlotInstance { id: format!("Plot {}", self.plots.len()+1), plot_type: PlotType::Normal, signals: Vec::new(), auto_bounds: true }); }
ui.separator(); ui.separator();
let (btn_text, btn_color) = if self.is_breaking { ("▶ Resume", egui::Color32::GREEN) } else { ("⏸ Pause", egui::Color32::YELLOW) }; let (btn_text, btn_color) = if self.is_breaking { ("▶ Resume App", egui::Color32::GREEN) } else { ("⏸ Pause App", egui::Color32::YELLOW) };
if ui.button(egui::RichText::new(btn_text).color(btn_color)).clicked() { self.is_breaking = !self.is_breaking; let _ = self.tx_cmd.send(if self.is_breaking { "PAUSE".to_string() } else { "RESUME".to_string() }); } if ui.button(egui::RichText::new(btn_text).color(btn_color)).clicked() { self.is_breaking = !self.is_breaking; let _ = self.tx_cmd.send(if self.is_breaking { "PAUSE".to_string() } else { "RESUME".to_string() }); }
ui.separator(); ui.separator();
ui.menu_button("🔌 Connection", |ui| { ui.checkbox(&mut self.scope.enabled, "🔭 Scope");
if self.scope.enabled {
egui::ComboBox::from_id_salt("window_size").selected_text(format!("{}ms", self.scope.window_ms)).show_ui(ui, |ui| { for ms in [10.0, 20.0, 50.0, 100.0, 200.0, 500.0, 1000.0, 2000.0, 5000.0, 10000.0] { ui.selectable_value(&mut self.scope.window_ms, ms, format!("{}ms", ms)); } });
ui.selectable_value(&mut self.scope.mode, AcquisitionMode::FreeRun, "Free");
ui.selectable_value(&mut self.scope.mode, AcquisitionMode::Triggered, "Trig");
if self.scope.mode == AcquisitionMode::FreeRun { if ui.button(if self.scope.paused { "▶ Resume" } else { "⏸ Pause" }).clicked() { self.scope.paused = !self.scope.paused; } }
else {
if ui.button(if self.scope.is_armed { "🔴 Armed" } else { "⚪ Single" }).clicked() { self.scope.is_armed = true; self.scope.trigger_active = false; }
ui.menu_button("⚙ Trig", |ui| {
egui::Grid::new("trig").num_columns(2).show(ui, |ui| {
ui.label("Source:"); ui.text_edit_singleline(&mut self.scope.trigger_source); ui.end_row();
ui.label("Edge:"); egui::ComboBox::from_id_salt("edge").selected_text(format!("{:?}", self.scope.trigger_edge)).show_ui(ui, |ui| { ui.selectable_value(&mut self.scope.trigger_edge, TriggerEdge::Rising, "Rising"); ui.selectable_value(&mut self.scope.trigger_edge, TriggerEdge::Falling, "Falling"); ui.selectable_value(&mut self.scope.trigger_edge, TriggerEdge::Both, "Both"); }); ui.end_row();
ui.label("Thresh:"); ui.add(egui::DragValue::new(&mut self.scope.trigger_threshold).speed(0.1)); ui.end_row();
ui.label("Pre %:"); ui.add(egui::Slider::new(&mut self.scope.pre_trigger_percent, 0.0..=100.0)); ui.end_row();
ui.label("Type:"); ui.selectable_value(&mut self.scope.trigger_type, TriggerType::Single, "Single"); ui.selectable_value(&mut self.scope.trigger_type, TriggerType::Continuous, "Cont"); ui.end_row();
});
});
}
}
ui.separator();
ui.menu_button("🔌 Conn", |ui| {
egui::Grid::new("conn_grid").num_columns(2).show(ui, |ui| { egui::Grid::new("conn_grid").num_columns(2).show(ui, |ui| {
ui.label("IP:"); ui.text_edit_singleline(&mut self.config.ip); ui.end_row(); ui.label("IP:"); ui.text_edit_singleline(&mut self.config.ip); ui.end_row();
ui.label("Control:"); ui.text_edit_singleline(&mut self.config.tcp_port); ui.end_row(); ui.label("Control:"); ui.text_edit_singleline(&mut self.config.tcp_port); ui.end_row();
ui.label("Telemetry:"); ui.text_edit_singleline(&mut self.config.udp_port); ui.end_row(); ui.label("Telemetry:"); ui.text_edit_singleline(&mut self.config.udp_port); ui.end_row();
ui.label("Logs:"); ui.text_edit_singleline(&mut self.config.log_port); ui.end_row(); ui.label("Logs:"); ui.text_edit_singleline(&mut self.config.log_port); ui.end_row();
}); });
ui.separator(); if ui.button("🔄 Apply").clicked() { self.config.version += 1; *self.shared_config.lock().unwrap() = self.config.clone(); ui.close_menu(); }
if ui.button("🔄 Apply & Reconnect").clicked() { self.config.version += 1; *self.shared_config.lock().unwrap() = self.config.clone(); ui.close_menu(); } if ui.button("❌ Off").clicked() { self.config.version += 1; let mut cfg = self.config.clone(); cfg.ip = "".to_string(); *self.shared_config.lock().unwrap() = cfg; ui.close_menu(); }
if ui.button("❌ Disconnect").clicked() { self.config.version += 1; let mut cfg = self.config.clone(); cfg.ip = "".to_string(); *self.shared_config.lock().unwrap() = cfg; ui.close_menu(); }
}); });
let status_color = if self.connected { egui::Color32::GREEN } else { egui::Color32::RED };
ui.label(egui::RichText::new(if self.connected { "● Online" } else { "○ Offline" }).color(status_color));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { ui.label(format!("UDP: OK[{}] DROP[{}]", self.udp_packets, self.udp_dropped)); }); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { ui.label(format!("UDP: OK[{}] DROP[{}]", self.udp_packets, self.udp_dropped)); });
}); });
}); });
@@ -467,12 +600,33 @@ impl eframe::App for MarteDebugApp {
names.sort(); names.sort();
egui::ScrollArea::vertical().id_salt("traced_scroll").show(ui, |ui| { egui::ScrollArea::vertical().id_salt("traced_scroll").show(ui, |ui| {
for key in names { for key in names {
let last_val = { self.traced_signals.lock().unwrap().get(&key).map(|d| d.last_value).unwrap_or(0.0) }; let mut data_map = self.traced_signals.lock().unwrap();
if let Some(entry) = data_map.get_mut(&key) {
let last_val = entry.last_value;
let is_recording = entry.recording_tx.is_some();
ui.horizontal(|ui| { ui.horizontal(|ui| {
let response = ui.add(egui::Label::new(format!("{}: {:.2}", key, last_val)).sense(egui::Sense::drag())); if is_recording { ui.label(egui::RichText::new("").color(egui::Color32::RED)); }
let response = ui.add(egui::Label::new(format!("{}: {:.2}", key, last_val)).sense(egui::Sense::drag().union(egui::Sense::click())));
if response.drag_started() { ctx.data_mut(|d| d.insert_temp(egui::Id::new("drag_signal"), key.clone())); } if response.drag_started() { ctx.data_mut(|d| d.insert_temp(egui::Id::new("drag_signal"), key.clone())); }
if ui.button("").clicked() { let _ = self.tx_cmd.send(format!("TRACE {} 0", key)); let _ = self.internal_tx.send(InternalEvent::ClearTrace(key)); } response.context_menu(|ui| {
if !is_recording {
if ui.button("⏺ Record to Parquet").clicked() {
let tx = self.internal_tx.clone();
let name_clone = key.clone();
thread::spawn(move || {
if let Some(path) = FileDialog::new().add_filter("Parquet", &["parquet"]).save_file() {
let _ = tx.send(InternalEvent::RecordPathChosen(name_clone, path.to_string_lossy().to_string()));
}
}); });
ui.close_menu();
}
} else {
if ui.button("⏹ Stop").clicked() { entry.recording_tx = None; ui.close_menu(); }
}
});
if ui.button("").clicked() { let _ = self.tx_cmd.send(format!("TRACE {} 0", key)); let _ = self.internal_tx.send(InternalEvent::ClearTrace(key.clone())); }
});
}
} }
}); });
ui.separator(); ui.separator();
@@ -483,15 +637,7 @@ impl eframe::App for MarteDebugApp {
if self.show_bottom_panel { if self.show_bottom_panel {
egui::TopBottomPanel::bottom("log_panel").resizable(true).default_height(150.0).show(ctx, |ui| { egui::TopBottomPanel::bottom("log_panel").resizable(true).default_height(150.0).show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| { ui.heading("Logs"); ui.separator(); ui.checkbox(&mut self.log_filters.show_debug, "Debug"); ui.checkbox(&mut self.log_filters.show_info, "Info"); ui.checkbox(&mut self.log_filters.show_warning, "Warn"); ui.checkbox(&mut self.log_filters.show_error, "Error"); ui.separator(); ui.label("Filter:"); ui.text_edit_singleline(&mut self.log_filters.content_regex); if ui.button("🗑 Clear").clicked() { self.logs.clear(); } });
ui.heading("System Logs"); ui.separator();
ui.checkbox(&mut self.log_filters.show_debug, "Debug"); ui.checkbox(&mut self.log_filters.show_info, "Info"); ui.checkbox(&mut self.log_filters.show_warning, "Warn"); ui.checkbox(&mut self.log_filters.show_error, "Error");
ui.separator();
ui.label("Filter:"); ui.text_edit_singleline(&mut self.log_filters.content_regex);
ui.separator();
ui.toggle_value(&mut self.log_filters.paused, "⏸ Pause");
if ui.button("🗑 Clear").clicked() { self.logs.clear(); }
});
ui.separator(); ui.separator();
let regex = if !self.log_filters.content_regex.is_empty() { Regex::new(&self.log_filters.content_regex).ok() } else { None }; let regex = if !self.log_filters.content_regex.is_empty() { Regex::new(&self.log_filters.content_regex).ok() } else { None };
egui::ScrollArea::vertical().stick_to_bottom(true).auto_shrink([false, false]).show(ui, |ui| { egui::ScrollArea::vertical().stick_to_bottom(true).auto_shrink([false, false]).show(ui, |ui| {
@@ -516,10 +662,19 @@ impl eframe::App for MarteDebugApp {
ui.group(|ui| { ui.group(|ui| {
ui.horizontal(|ui| { ui.label(egui::RichText::new(&plot_inst.id).strong()); ui.selectable_value(&mut plot_inst.plot_type, PlotType::Normal, "Series"); ui.selectable_value(&mut plot_inst.plot_type, PlotType::LogicAnalyzer, "Logic"); if ui.button("🗑").clicked() { to_remove = Some(p_idx); } }); ui.horizontal(|ui| { ui.label(egui::RichText::new(&plot_inst.id).strong()); ui.selectable_value(&mut plot_inst.plot_type, PlotType::Normal, "Series"); ui.selectable_value(&mut plot_inst.plot_type, PlotType::LogicAnalyzer, "Logic"); if ui.button("🗑").clicked() { to_remove = Some(p_idx); } });
let mut plot = Plot::new(&plot_inst.id).height(plot_height - 40.0).show_axes([true, true]); let mut plot = Plot::new(&plot_inst.id).height(plot_height - 40.0).show_axes([true, true]);
if self.scope.enabled {
let window_s = self.scope.window_ms / 1000.0;
let center_t = if self.scope.mode == AcquisitionMode::Triggered { if self.scope.trigger_active { self.scope.last_trigger_time } else { APP_START_TIME.elapsed().as_secs_f64() } } else { APP_START_TIME.elapsed().as_secs_f64() };
let x_min = center_t - (self.scope.pre_trigger_percent / 100.0) * window_s;
plot = plot.include_x(x_min).include_x(x_min + window_s);
if !self.scope.paused { plot = plot.auto_bounds(egui::Vec2b::new(true, true)); }
} else {
if let Some(range) = self.shared_x_range { if !plot_inst.auto_bounds { plot = plot.include_x(range[0]).include_x(range[1]); } } if let Some(range) = self.shared_x_range { if !plot_inst.auto_bounds { plot = plot.include_x(range[0]).include_x(range[1]); } }
if plot_inst.auto_bounds { plot = plot.auto_bounds(egui::Vec2b::new(true, true)); } if plot_inst.auto_bounds { plot = plot.auto_bounds(egui::Vec2b::new(true, true)); }
}
let plot_resp = plot.show(ui, |plot_ui| { let plot_resp = plot.show(ui, |plot_ui| {
if !plot_inst.auto_bounds { if let Some(range) = self.shared_x_range { let bounds = plot_ui.plot_bounds(); plot_ui.set_plot_bounds(PlotBounds::from_min_max([range[0], bounds.min()[1]], [range[1], bounds.max()[1]])); } } if !self.scope.enabled && !plot_inst.auto_bounds { if let Some(range) = self.shared_x_range { let bounds = plot_ui.plot_bounds(); plot_ui.set_plot_bounds(PlotBounds::from_min_max([range[0], bounds.min()[1]], [range[1], bounds.max()[1]])); } }
if self.scope.enabled && self.scope.mode == AcquisitionMode::Triggered && self.scope.trigger_active { plot_ui.vline(VLine::new(self.scope.last_trigger_time).color(egui::Color32::YELLOW).style(LineStyle::Dashed { length: 5.0 })); }
let data_map = self.traced_signals.lock().unwrap(); let data_map = self.traced_signals.lock().unwrap();
for (s_idx, sig_cfg) in plot_inst.signals.iter().enumerate() { for (s_idx, sig_cfg) in plot_inst.signals.iter().enumerate() {
if let Some(data) = data_map.get(&sig_cfg.source_name) { if let Some(data) = data_map.get(&sig_cfg.source_name) {
@@ -538,27 +693,25 @@ impl eframe::App for MarteDebugApp {
if let Some(dropped) = ctx.data_mut(|d| d.get_temp::<String>(egui::Id::new("drag_signal"))) { if let Some(dropped) = ctx.data_mut(|d| d.get_temp::<String>(egui::Id::new("drag_signal"))) {
let color = Self::next_color(plot_inst.signals.len()); let color = Self::next_color(plot_inst.signals.len());
plot_inst.signals.push(SignalPlotConfig { source_name: dropped.clone(), label: dropped.clone(), unit: "".to_string(), color, line_style: LineStyle::Solid, marker_type: MarkerType::None, gain: 1.0, offset: 0.0 }); plot_inst.signals.push(SignalPlotConfig { source_name: dropped.clone(), label: dropped.clone(), unit: "".to_string(), color, line_style: LineStyle::Solid, marker_type: MarkerType::None, gain: 1.0, offset: 0.0 });
self.logs.push_back(LogEntry { time: Local::now().format("%H:%M:%S").to_string(), level: "GUI".to_string(), message: format!("Dropped {} into plot", dropped) });
ctx.data_mut(|d| d.remove_temp::<String>(egui::Id::new("drag_signal"))); ctx.data_mut(|d| d.remove_temp::<String>(egui::Id::new("drag_signal")));
} }
} }
if plot_resp.response.dragged() || ctx.input(|i| i.pointer.any_click() || i.smooth_scroll_delta.y != 0.0) { if plot_resp.response.hovered() { plot_inst.auto_bounds = false; let b = plot_resp.transform.bounds(); self.shared_x_range = Some([b.min()[0], b.max()[0]]); } } if plot_resp.response.dragged() || ctx.input(|i| i.smooth_scroll_delta.y != 0.0) { if plot_resp.response.hovered() { plot_inst.auto_bounds = false; let b = plot_resp.transform.bounds(); self.shared_x_range = Some([b.min()[0], b.max()[0]]); } }
plot_resp.response.context_menu(|ui| { plot_resp.response.context_menu(|ui| {
if ui.button("🔍 Fit View").clicked() { plot_inst.auto_bounds = true; self.shared_x_range = None; ui.close_menu(); } if ui.button("🔍 Fit View").clicked() { plot_inst.auto_bounds = true; self.shared_x_range = None; ui.close_menu(); }
ui.separator(); ui.separator();
let mut sig_to_remove = None; let mut sig_to_remove = None;
for (s_idx, sig) in plot_inst.signals.iter().enumerate() { for (s_idx, sig) in plot_inst.signals.iter().enumerate() {
ui.horizontal(|ui| { ui.label(&sig.label); if ui.button("🎨").clicked() { self.style_editor = Some((p_idx, s_idx)); ui.close_menu(); } if ui.button("").clicked() { sig_to_remove = Some(s_idx); ui.close_menu(); } }); ui.horizontal(|ui| { ui.label(&sig.label); if ui.button("🎨 Style").clicked() { self.style_editor = Some((p_idx, s_idx)); ui.close_menu(); } if ui.button(" Remove").clicked() { sig_to_remove = Some(s_idx); ui.close_menu(); } });
} }
if let Some(idx) = sig_to_remove { plot_inst.signals.remove(idx); } if let Some(idx) = sig_to_remove { plot_inst.signals.remove(idx); }
}); });
}); });
} }
if let Some(idx) = to_remove { self.plots.remove(idx); } if let Some(idx) = to_remove { self.plots.remove(idx); }
if let Some(range) = current_range { if self.shared_x_range.is_none() { self.shared_x_range = Some(range); } } if !self.scope.enabled { if let Some(range) = current_range { if self.shared_x_range.is_none() { self.shared_x_range = Some(range); } } }
} else { ui.centered_and_justified(|ui| { ui.label("Add a plot panel to begin analysis"); }); } } else { ui.centered_and_justified(|ui| { ui.label("Add a plot panel to begin analysis"); }); }
}); });
ctx.request_repaint_after(std::time::Duration::from_millis(16)); ctx.request_repaint_after(std::time::Duration::from_millis(16));
} }
} }