mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2025-12-06 05:11:17 +00:00
Included settings menu.
This commit is contained in:
140
src/app.rs
140
src/app.rs
@@ -4,9 +4,8 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
|||||||
use atomic_float::AtomicF32;
|
use atomic_float::AtomicF32;
|
||||||
use tokio::{sync::mpsc::{Sender}};
|
use tokio::{sync::mpsc::{Sender}};
|
||||||
|
|
||||||
use eframe::egui::{self, Button, Color32, DragValue, Key, Label, Layout, Modifiers};
|
use eframe::egui::{self, Button, CollapsingHeader, Color32, DragValue, Key, Label, Layout, Modifiers};
|
||||||
use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds};
|
use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds};
|
||||||
use egui_dock::tab_viewer::OnCloseResponse;
|
|
||||||
use egui_dock::{DockArea, DockState, Style};
|
use egui_dock::{DockArea, DockState, Style};
|
||||||
|
|
||||||
use crate::plot::TimeSeriesPlot;
|
use crate::plot::TimeSeriesPlot;
|
||||||
@@ -15,15 +14,16 @@ use crate::signals::SingleFrequencySignal;
|
|||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
tree: DockState<String>,
|
tree: DockState<String>,
|
||||||
|
tab_viewer: TabViewer,
|
||||||
run_impedancemeter_tx: Sender<SingleFrequencySignal>,
|
run_impedancemeter_tx: Sender<SingleFrequencySignal>,
|
||||||
pub magnitude: Arc<Mutex<f32>>,
|
pub magnitude: Arc<Mutex<f32>>,
|
||||||
pub phase: Arc<Mutex<f32>>,
|
pub phase: Arc<Mutex<f32>>,
|
||||||
pub magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
pub magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||||
pub phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
pub phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||||
pub connected: Arc<AtomicBool>,
|
pub connected: Arc<AtomicBool>,
|
||||||
pub on: bool,
|
pub on: Arc<Mutex<bool>>,
|
||||||
pub data_frequency: Arc<AtomicF32>,
|
pub data_frequency: Arc<AtomicF32>,
|
||||||
pub single_frequency: u32
|
pub single_frequency: Arc<Mutex<u32>>
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TabViewer {
|
struct TabViewer {
|
||||||
@@ -31,6 +31,10 @@ struct TabViewer {
|
|||||||
phase: Arc<Mutex<f32>>,
|
phase: Arc<Mutex<f32>>,
|
||||||
magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||||
phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||||
|
on: Arc<Mutex<bool>>,
|
||||||
|
single_frequency: Arc<Mutex<u32>>,
|
||||||
|
show_settings: bool,
|
||||||
|
show_settings_toggle: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl egui_dock::TabViewer for TabViewer {
|
impl egui_dock::TabViewer for TabViewer {
|
||||||
@@ -42,20 +46,30 @@ impl egui_dock::TabViewer for TabViewer {
|
|||||||
|
|
||||||
fn ui(&mut self, ui: &mut egui::Ui, tab: &mut Self::Tab) {
|
fn ui(&mut self, ui: &mut egui::Ui, tab: &mut Self::Tab) {
|
||||||
egui::Frame::default().inner_margin(5).show(ui, |ui| {
|
egui::Frame::default().inner_margin(5).show(ui, |ui| {
|
||||||
|
let settings = CollapsingHeader::new("Settings")
|
||||||
|
.open(self.show_settings_toggle)
|
||||||
|
.show(ui, |ui| {
|
||||||
|
if let Ok(on) = self.on.lock() {
|
||||||
|
ui.add_enabled_ui(!*on, |ui| {
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
ui.label("Single Frequency:");
|
||||||
|
if let Ok(mut freq) = self.single_frequency.lock() {
|
||||||
|
ui.add(DragValue::new(&mut *freq).speed(0.1));
|
||||||
|
}
|
||||||
|
ui.label("Hz");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.show_settings_toggle = None;
|
||||||
|
self.show_settings = !settings.fully_closed();
|
||||||
|
|
||||||
|
ui.separator();
|
||||||
|
|
||||||
let available_height = ui.available_height();
|
let available_height = ui.available_height();
|
||||||
let half_height = available_height / 2.0;
|
let half_height = available_height / 2.0-2.0;
|
||||||
|
|
||||||
let point_pos = vec![[*self.magnitude.lock().unwrap() as f64, *self.phase.lock().unwrap() as f64]];
|
|
||||||
|
|
||||||
let point_pos = PlotPoints::new(point_pos);
|
|
||||||
let point_pos = Points::new("pos", point_pos)
|
|
||||||
.radius(20.0)
|
|
||||||
.color(Color32::LIGHT_RED);
|
|
||||||
|
|
||||||
let bounds = PlotBounds::from_min_max([-1.5, -1.5], [1.5, 1.5]);
|
|
||||||
|
|
||||||
// Use a vertical layout to stack the plots
|
|
||||||
ui.with_layout(Layout::top_down(egui::Align::Min), |ui| {
|
|
||||||
// Plot pressure
|
// Plot pressure
|
||||||
ui.allocate_ui_with_layout(
|
ui.allocate_ui_with_layout(
|
||||||
egui::vec2(ui.available_width(), half_height),
|
egui::vec2(ui.available_width(), half_height),
|
||||||
@@ -64,15 +78,12 @@ impl egui_dock::TabViewer for TabViewer {
|
|||||||
// Magnitude
|
// Magnitude
|
||||||
let magnitude = self.magnitude_series.lock().unwrap();
|
let magnitude = self.magnitude_series.lock().unwrap();
|
||||||
Plot::new("magnitude")
|
Plot::new("magnitude")
|
||||||
.allow_scroll(false)
|
|
||||||
.allow_drag(false)
|
|
||||||
// .center_y_axis(true)
|
|
||||||
.legend(Legend::default().position(Corner::LeftTop))
|
.legend(Legend::default().position(Corner::LeftTop))
|
||||||
.y_axis_label("Magnitude [Ω]")
|
.y_axis_label("Magnitude at {} Hz[Ω]")
|
||||||
.y_axis_min_width(2.0)
|
.y_axis_min_width(80.0)
|
||||||
.show(ui, |plot_ui| {
|
.show(ui, |plot_ui| {
|
||||||
plot_ui.line(
|
plot_ui.line(
|
||||||
Line::new("Magnitude", magnitude.plot_values())
|
Line::new(format!("Magnitude at {} Hz", self.single_frequency.lock().unwrap()), magnitude.plot_values())
|
||||||
.color(Color32::BLUE)
|
.color(Color32::BLUE)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -86,52 +97,66 @@ impl egui_dock::TabViewer for TabViewer {
|
|||||||
// Phase
|
// Phase
|
||||||
let phase = self.phase_series.lock().unwrap();
|
let phase = self.phase_series.lock().unwrap();
|
||||||
Plot::new("phase")
|
Plot::new("phase")
|
||||||
.allow_scroll(false)
|
|
||||||
.allow_drag(false)
|
|
||||||
// .center_y_axis(true)
|
|
||||||
.legend(Legend::default().position(Corner::LeftTop))
|
.legend(Legend::default().position(Corner::LeftTop))
|
||||||
.y_axis_label("Phase [rad]")
|
.y_axis_label("Phase [rad]")
|
||||||
.y_axis_min_width(2.0)
|
.y_axis_min_width(80.0)
|
||||||
.show(ui, |plot_ui| {
|
.show(ui, |plot_ui| {
|
||||||
plot_ui.line(
|
plot_ui.line(
|
||||||
Line::new("Phase", phase.plot_values())
|
Line::new(format!("Phase at {} Hz", self.single_frequency.lock().unwrap()), phase.plot_values())
|
||||||
.color(Color32::RED)
|
.color(Color32::RED)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_close(&mut self, _tab: &mut Self::Tab) -> OnCloseResponse {
|
|
||||||
println!("Closed tab: {_tab}");
|
|
||||||
OnCloseResponse::Close
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new(run_impedancemeter_tx: Sender<SingleFrequencySignal>) -> Self {
|
pub fn new(run_impedancemeter_tx: Sender<SingleFrequencySignal>) -> Self {
|
||||||
|
// Step 1: Initialize shared fields first
|
||||||
|
let magnitude = Arc::new(Mutex::new(0.0));
|
||||||
|
let phase = Arc::new(Mutex::new(0.0));
|
||||||
|
let magnitude_series = Arc::new(Mutex::new(TimeSeriesPlot::new()));
|
||||||
|
let phase_series = Arc::new(Mutex::new(TimeSeriesPlot::new()));
|
||||||
|
let single_frequency = Arc::new(Mutex::new(50000));
|
||||||
|
let on = Arc::new(Mutex::new(true));
|
||||||
|
|
||||||
|
// Step 2: Now we can initialize tab_viewer
|
||||||
|
let tab_viewer = TabViewer {
|
||||||
|
magnitude: magnitude.clone(),
|
||||||
|
phase: phase.clone(),
|
||||||
|
magnitude_series: magnitude_series.clone(),
|
||||||
|
phase_series: phase_series.clone(),
|
||||||
|
single_frequency: single_frequency.clone(),
|
||||||
|
on: on.clone(),
|
||||||
|
show_settings: false,
|
||||||
|
show_settings_toggle: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Step 3: Construct App
|
||||||
let app = App {
|
let app = App {
|
||||||
tree: DockState::new(vec!["Single".to_string()]),
|
tree: DockState::new(vec!["Single".to_string()]),
|
||||||
|
tab_viewer,
|
||||||
run_impedancemeter_tx,
|
run_impedancemeter_tx,
|
||||||
magnitude: Arc::new(Mutex::new(0.0)),
|
magnitude,
|
||||||
phase: Arc::new(Mutex::new(0.0)),
|
phase,
|
||||||
magnitude_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
magnitude_series,
|
||||||
phase_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
phase_series,
|
||||||
connected: Arc::new(AtomicBool::new(false)),
|
connected: Arc::new(AtomicBool::new(false)),
|
||||||
on: true,
|
on,
|
||||||
data_frequency: Arc::new(AtomicF32::new(0.0)),
|
data_frequency: Arc::new(AtomicF32::new(0.0)),
|
||||||
single_frequency: 50000,
|
single_frequency,
|
||||||
};
|
};
|
||||||
|
|
||||||
app.update_start_stop();
|
app.update_start_stop();
|
||||||
app
|
app
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_start_stop(&self) {
|
pub fn update_start_stop(&self) {
|
||||||
match self.on {
|
match *self.on.lock().unwrap() {
|
||||||
true => {
|
true => {
|
||||||
if let Err(e) = self.run_impedancemeter_tx.try_send(SingleFrequencySignal::Start(self.single_frequency)) {
|
if let Err(e) = self.run_impedancemeter_tx.try_send(SingleFrequencySignal::Start(*self.single_frequency.lock().unwrap())) {
|
||||||
eprintln!("Failed to send start command: {:?}", e);
|
eprintln!("Failed to send start command: {:?}", e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -159,18 +184,11 @@ impl eframe::App for App {
|
|||||||
egui::widgets::global_theme_preference_switch(ui);
|
egui::widgets::global_theme_preference_switch(ui);
|
||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
ui.label(format!("{} Hz", self.data_frequency.load(Ordering::Relaxed)));
|
ui.label(format!("Data rate: {} Hz", self.data_frequency.load(Ordering::Relaxed)));
|
||||||
|
|
||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
ui.add_enabled_ui(!self.on, |ui| {
|
if ui.add_enabled(connected, toggle_start_stop(&mut *self.on.lock().unwrap())).changed() {
|
||||||
ui.add(DragValue::new(&mut self.single_frequency));
|
|
||||||
ui.add(Label::new("Hz"));
|
|
||||||
});
|
|
||||||
|
|
||||||
ui.separator();
|
|
||||||
|
|
||||||
if ui.add_enabled(connected, toggle_start_stop(&mut self.on)).changed() {
|
|
||||||
self.update_start_stop();
|
self.update_start_stop();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -211,12 +229,7 @@ impl eframe::App for App {
|
|||||||
.show_leaf_close_all_buttons(false)
|
.show_leaf_close_all_buttons(false)
|
||||||
.show_leaf_collapse_buttons(false)
|
.show_leaf_collapse_buttons(false)
|
||||||
.show_close_buttons(false)
|
.show_close_buttons(false)
|
||||||
.show_inside(ui, &mut TabViewer {
|
.show_inside(ui, &mut self.tab_viewer);
|
||||||
magnitude: self.magnitude.clone(),
|
|
||||||
phase: self.phase.clone(),
|
|
||||||
magnitude_series: self.magnitude_series.clone(),
|
|
||||||
phase_series: self.phase_series.clone(),
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// CMD- or control-W to close window
|
// CMD- or control-W to close window
|
||||||
@@ -229,13 +242,14 @@ impl eframe::App for App {
|
|||||||
// CMD- or control-W to close window
|
// CMD- or control-W to close window
|
||||||
if ctx.input(|i| i.key_pressed(Key::Space))
|
if ctx.input(|i| i.key_pressed(Key::Space))
|
||||||
{
|
{
|
||||||
self.on = !self.on;
|
let value = *self.on.lock().unwrap();
|
||||||
|
*self.on.lock().unwrap() = !value;
|
||||||
self.update_start_stop();
|
self.update_start_stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send stop command when the window is closed
|
// Send stop command when the window is closed
|
||||||
if ctx.input(|i| i.viewport().close_requested()) {
|
if ctx.input(|i| i.viewport().close_requested()) {
|
||||||
self.on = false;
|
*self.on.lock().unwrap() = false;
|
||||||
self.update_start_stop();
|
self.update_start_stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,6 +258,16 @@ impl eframe::App for App {
|
|||||||
self.reset_view();
|
self.reset_view();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Toggle setttings view
|
||||||
|
if ctx.input(|i| i.key_pressed(egui::Key::S)) {
|
||||||
|
self.tab_viewer.show_settings = !self.tab_viewer.show_settings;
|
||||||
|
if self.tab_viewer.show_settings {
|
||||||
|
self.tab_viewer.show_settings_toggle = Some(true);
|
||||||
|
} else {
|
||||||
|
self.tab_viewer.show_settings_toggle = Some(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx.request_repaint();
|
ctx.request_repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user