diff --git a/src/app.rs b/src/app.rs index 2e91767..ead832c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use atomic_float::AtomicF32; use tokio::{sync::mpsc::{Sender}}; -use eframe::egui::{self, Color32, DragValue, Key, Label, Layout, Modifiers}; +use eframe::egui::{self, Button, Color32, DragValue, Key, Label, Layout, Modifiers}; use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds}; use crate::plot::TimeSeriesPlot; @@ -54,6 +54,11 @@ impl App { }, } } + + pub fn reset_view(&self) { + self.magnitude_series.lock().unwrap().clear(); + self.phase_series.lock().unwrap().clear(); + } } impl eframe::App for App { @@ -81,6 +86,12 @@ impl eframe::App for App { self.update_start_stop(); }; + ui.separator(); + + if ui.add_enabled(connected, Button::new("Reset view")).clicked() { + self.reset_view(); + } + // Spacer to push the LED to the right ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { ui.scope(|ui| { @@ -198,6 +209,11 @@ impl eframe::App for App { self.update_start_stop(); } + // Reset view + if ctx.input(|i| i.key_pressed(Key::C)) { + self.reset_view(); + } + ctx.request_repaint(); }} diff --git a/src/bin/main_gui.rs b/src/bin/main_gui.rs index 2dcbc43..8e6e3cf 100644 --- a/src/bin/main_gui.rs +++ b/src/bin/main_gui.rs @@ -36,7 +36,7 @@ fn main() { std::thread::spawn(move || { rt.block_on(communicate_with_hardware( run_impedancemeter_rx, - run_impedancemeter_tx_clone, + run_impedancemeter_tx_clone, magnitude_clone, phase_clone, magnitude_series_clone, diff --git a/src/plot.rs b/src/plot.rs index f3f3629..042cd6a 100644 --- a/src/plot.rs +++ b/src/plot.rs @@ -1,3 +1,4 @@ +use core::f64; use std::collections::VecDeque; use egui_plot::{PlotPoint, PlotPoints}; @@ -9,10 +10,10 @@ pub struct TimeSeriesPlot { impl TimeSeriesPlot { pub fn new() -> Self { - let max_points = 100; + let max_points = 500; Self { - values: (0..max_points as i32) - .map(|i| PlotPoint::new(i, 0.0)) + values: (-(max_points as i32)..0) + .map(|i| PlotPoint::new(i, f64::NAN)) .collect(), // Create x amount of (0,0) points max_points, } @@ -21,7 +22,7 @@ impl TimeSeriesPlot { pub fn add(&mut self, val: f64) { let last_x = self.values.back().unwrap().x; - if last_x >= self.max_points as f64 { + if last_x >= 0.0 { self.values.pop_front(); } @@ -39,4 +40,10 @@ impl TimeSeriesPlot { } PlotPoints::Owned(Vec::from_iter(values)) } + + pub fn clear(&mut self) { + self.values = (-(self.max_points as i32)..0) + .map(|i| PlotPoint::new(i, f64::NAN)) + .collect(); + } } \ No newline at end of file