Scope like ui start

This commit is contained in:
Martino Ferrari
2026-02-23 18:01:24 +01:00
parent 56bb3536fc
commit ec29bd5148
2 changed files with 237 additions and 35 deletions

View File

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

View File

@@ -1,5 +1,5 @@
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::net::{TcpStream, UdpSocket};
use std::io::{Write, BufReader, BufRead};
@@ -78,6 +78,25 @@ enum PlotType {
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)]
enum MarkerType {
None,
@@ -145,31 +164,61 @@ struct LogFilters {
content_regex: String,
}
struct ScopeSettings {
enabled: bool,
window_ms: f64,
mode: AcquisitionMode,
paused: bool,
// Trigger Settings
trigger_type: TriggerType,
trigger_source: String,
trigger_edge: TriggerEdge,
trigger_threshold: f64,
pre_trigger_percent: f64,
// Internal State
trigger_active: bool,
last_trigger_time: f64,
is_armed: bool,
}
struct MarteDebugApp {
connected: bool,
is_breaking: bool,
config: ConnectionConfig,
shared_config: Arc<Mutex<ConnectionConfig>>,
app_tree: Option<TreeItem>,
id_to_meta: Arc<Mutex<HashMap<u32, SignalMetadata>>>,
traced_signals: Arc<Mutex<HashMap<String, TraceData>>>,
id_to_meta: Arc<Mutex<HashMap<u32, SignalMetadata>>>,
plots: Vec<PlotInstance>,
forced_signals: HashMap<String, String>,
logs: VecDeque<LogEntry>,
log_filters: LogFilters,
show_left_panel: bool,
show_right_panel: bool,
show_bottom_panel: bool,
selected_node: String,
node_info: String,
udp_packets: u64,
udp_dropped: u64,
forcing_dialog: Option<ForcingDialog>,
style_editor: Option<(usize, usize)>,
style_editor: Option<(usize, usize)>, // plot_idx, signal_idx
tx_cmd: Sender<String>,
rx_events: Receiver<InternalEvent>,
internal_tx: Sender<InternalEvent>,
shared_x_range: Option<[f64; 2]>,
scope: ScopeSettings,
}
impl MarteDebugApp {
@@ -177,15 +226,25 @@ impl MarteDebugApp {
let (tx_cmd, rx_cmd_internal) = unbounded::<String>();
let (tx_events, rx_events) = unbounded::<InternalEvent>();
let internal_tx = tx_events.clone();
let config = ConnectionConfig { ip: "127.0.0.1".to_string(), tcp_port: "8080".to_string(), udp_port: "8081".to_string(), log_port: "8082".to_string(), version: 0 };
let config = ConnectionConfig {
ip: "127.0.0.1".to_string(),
tcp_port: "8080".to_string(),
udp_port: "8081".to_string(),
log_port: "8082".to_string(),
version: 0,
};
let shared_config = Arc::new(Mutex::new(config.clone()));
let id_to_meta = Arc::new(Mutex::new(HashMap::new()));
let traced_signals = Arc::new(Mutex::new(HashMap::new()));
let id_to_meta_clone = id_to_meta.clone();
let traced_signals_clone = traced_signals.clone();
let shared_config_cmd = shared_config.clone();
let shared_config_log = shared_config.clone();
let shared_config_udp = shared_config.clone();
let tx_events_c = tx_events.clone();
thread::spawn(move || { tcp_command_worker(shared_config_cmd, rx_cmd_internal, tx_events_c); });
let tx_events_log = tx_events.clone();
@@ -194,16 +253,47 @@ impl MarteDebugApp {
thread::spawn(move || { udp_worker(shared_config_udp, id_to_meta_clone, traced_signals_clone, tx_events_udp); });
Self {
connected: false, is_breaking: false, config, shared_config, app_tree: None, id_to_meta, traced_signals,
plots: vec![PlotInstance { id: "Plot 1".to_string(), plot_type: PlotType::Normal, signals: Vec::new(), auto_bounds: true }],
forced_signals: HashMap::new(), logs: VecDeque::with_capacity(2000),
log_filters: LogFilters { show_debug: true, show_info: true, show_warning: true, show_error: true, paused: false, content_regex: "".to_string() },
show_left_panel: true, show_right_panel: true, show_bottom_panel: true,
connected: false,
is_breaking: false,
config,
shared_config,
app_tree: None,
id_to_meta,
traced_signals,
plots: vec![PlotInstance {
id: "Plot 1".to_string(),
plot_type: PlotType::Normal,
signals: Vec::new(),
auto_bounds: true,
}],
forced_signals: HashMap::new(),
logs: VecDeque::with_capacity(2000),
log_filters: LogFilters {
show_debug: true, show_info: true, show_warning: true, show_error: true, paused: false,
content_regex: "".to_string(),
},
show_left_panel: true,
show_right_panel: true,
show_bottom_panel: true,
selected_node: "".to_string(), node_info: "".to_string(),
udp_packets: 0, udp_dropped: 0,
forcing_dialog: None, style_editor: None,
tx_cmd, rx_events, internal_tx,
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,
},
}
}
@@ -231,12 +321,54 @@ impl MarteDebugApp {
ui.horizontal(|ui| {
if ui.selectable_label(self.selected_node == current_path, format!("{} [{}]", label, item.class)).clicked() { self.selected_node = current_path.clone(); let _ = self.tx_cmd.send(format!("INFO {}", current_path)); }
if item.class.contains("Signal") {
if ui.button("Trace").clicked() { let _ = self.tx_cmd.send(format!("TRACE {} 1", current_path)); let _ = self.internal_tx.send(InternalEvent::TraceRequested(current_path.clone())); }
if ui.button("⚡ Force").clicked() { self.forcing_dialog = Some(ForcingDialog { signal_path: current_path.clone(), value: "".to_string() }); }
if ui.button("Trace").clicked() {
let _ = self.tx_cmd.send(format!("TRACE {} 1", current_path));
let _ = self.internal_tx.send(InternalEvent::TraceRequested(current_path.clone()));
}
if ui.button("⚡ Force").clicked() {
self.forcing_dialog = Some(ForcingDialog { signal_path: current_path.clone(), value: "".to_string() });
}
}
});
}
}
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; }
// Search for edge crossing in the last 100 samples
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];
// Avoid re-triggering on the same exact sample
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 tcp_command_worker(shared_config: Arc<Mutex<ConnectionConfig>>, rx_cmd: Receiver<String>, tx_events: Sender<InternalEvent>) {
@@ -375,7 +507,7 @@ fn udp_worker(shared_config: Arc<Mutex<ConnectionConfig>>, id_to_meta: Arc<Mutex
if let Some(entry) = data_map.get_mut(&name) {
for point in new_points { entry.values.push_back(point); }
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(); }
}
}
}
@@ -403,6 +535,8 @@ impl eframe::App for MarteDebugApp {
}
}
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"))) {
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)); }); });
}
@@ -437,23 +571,48 @@ impl eframe::App for MarteDebugApp {
ui.toggle_value(&mut self.show_bottom_panel, "📜 Logs");
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 }); }
ui.separator();
let (btn_text, btn_color) = if self.is_breaking { ("▶ Resume", egui::Color32::GREEN) } else { ("⏸ Pause", egui::Color32::YELLOW) };
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("⚙ Trigger", |ui| {
egui::Grid::new("trig_grid").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("Threshold:"); ui.add(egui::DragValue::new(&mut self.scope.trigger_threshold).speed(0.1)); ui.end_row();
ui.label("Pre-trig %:"); ui.add(egui::Slider::new(&mut self.scope.pre_trigger_percent, 0.0..=100.0)); ui.end_row();
ui.label("Mode:"); 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();
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() }); }
ui.separator();
ui.menu_button("🔌 Connection", |ui| {
ui.menu_button("🔌 Conn", |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("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("Logs:"); ui.text_edit_singleline(&mut self.config.log_port); ui.end_row();
});
ui.separator();
if ui.button("🔄 Apply & Reconnect").clicked() { self.config.version += 1; *self.shared_config.lock().unwrap() = self.config.clone(); 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(); }
if ui.button("🔄 Apply").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(); }
});
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)); });
});
});
@@ -484,12 +643,10 @@ impl eframe::App for MarteDebugApp {
if self.show_bottom_panel {
egui::TopBottomPanel::bottom("log_panel").resizable(true).default_height(150.0).show(ctx, |ui| {
ui.horizontal(|ui| {
ui.heading("System Logs"); ui.separator();
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);
ui.separator();
ui.toggle_value(&mut self.log_filters.paused, "⏸ Pause");
if ui.button("🗑 Clear").clicked() { self.logs.clear(); }
});
ui.separator();
@@ -512,37 +669,82 @@ impl eframe::App for MarteDebugApp {
let plot_height = ui.available_height() / n_plots as f32;
let mut to_remove = None;
let mut current_range = None;
for (p_idx, plot_inst) in self.plots.iter_mut().enumerate() {
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]);
// SCOPE SYNC LOGIC
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 pre_trigger_s = (self.scope.pre_trigger_percent / 100.0) * window_s;
let x_min = center_t - pre_trigger_s;
let x_max = x_min + window_s;
plot = plot.include_x(x_min).include_x(x_max);
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 plot_inst.auto_bounds { plot = plot.auto_bounds(egui::Vec2b::new(true, true)); }
}
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]]));
}
}
// Trigger Line
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();
for (s_idx, sig_cfg) in plot_inst.signals.iter().enumerate() {
if let Some(data) = data_map.get(&sig_cfg.source_name) {
let mut points = Vec::new();
let mut points_vec = Vec::new();
for [t, v] in &data.values {
let mut final_v = *v * sig_cfg.gain + sig_cfg.offset;
if plot_inst.plot_type == PlotType::LogicAnalyzer { final_v = (s_idx as f64 * 1.5) + (if final_v > 0.5 { 1.0 } else { 0.0 }); }
points.push([*t, final_v]);
points_vec.push([*t, final_v]);
}
plot_ui.line(Line::new(PlotPoints::from(points)).name(&sig_cfg.label).color(sig_cfg.color));
plot_ui.line(Line::new(PlotPoints::from(points_vec)).name(&sig_cfg.label).color(sig_cfg.color));
}
}
if p_idx == 0 || current_range.is_none() { let b = plot_ui.plot_bounds(); current_range = Some([b.min()[0], b.max()[0]]); }
});
if plot_resp.response.hovered() && ctx.input(|i| i.pointer.any_released()) {
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());
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")));
}
}
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.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]]);
}
}
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(); }
ui.separator();
@@ -555,7 +757,7 @@ impl eframe::App for MarteDebugApp {
});
}
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"); }); }
});