mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2025-12-06 05:11:17 +00:00
230 lines
8.6 KiB
Rust
230 lines
8.6 KiB
Rust
use std::sync::{Arc, Mutex};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use atomic_float::AtomicF32;
|
|
use tokio::{sync::mpsc::{Sender}};
|
|
|
|
use eframe::egui::{self, Color32, DragValue, Key, Layout, Modifiers, };
|
|
use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds};
|
|
|
|
use crate::plot::TimeSeriesPlot;
|
|
|
|
use crate::signals::FrequencySignal;
|
|
|
|
pub struct App {
|
|
run_impedancemeter_tx: Sender<FrequencySignal>,
|
|
pub magnitude: Arc<Mutex<f32>>,
|
|
pub phase: Arc<Mutex<f32>>,
|
|
pub magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
|
pub phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
|
pub connected: Arc<AtomicBool>,
|
|
pub on: bool,
|
|
pub data_frequency: Arc<AtomicF32>,
|
|
}
|
|
|
|
impl App {
|
|
pub fn new(run_impedancemeter_tx: Sender<FrequencySignal>) -> Self {
|
|
let app = App {
|
|
run_impedancemeter_tx,
|
|
magnitude: Arc::new(Mutex::new(0.0)),
|
|
phase: Arc::new(Mutex::new(0.0)),
|
|
magnitude_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
|
phase_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
|
connected: Arc::new(AtomicBool::new(false)),
|
|
on: true,
|
|
data_frequency: Arc::new(AtomicF32::new(0.0)),
|
|
};
|
|
app.update_start_stop();
|
|
app
|
|
}
|
|
|
|
pub fn update_start_stop(&self) {
|
|
match self.on {
|
|
true => {
|
|
if let Err(e) = self.run_impedancemeter_tx.try_send(FrequencySignal::Start(0.0)) {
|
|
eprintln!("Failed to send start command: {:?}", e);
|
|
}
|
|
},
|
|
false => {
|
|
if let Err(e) = self.run_impedancemeter_tx.try_send(FrequencySignal::Stop) {
|
|
eprintln!("Failed to send stop command: {:?}", e);
|
|
}
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
impl eframe::App for App {
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
// Egui add a top bar
|
|
egui::TopBottomPanel::top("top_bar").show(ctx, |ui| {
|
|
egui::MenuBar::new().ui(ui, |ui| {
|
|
let connected = self.connected.load(Ordering::Relaxed);
|
|
|
|
egui::widgets::global_theme_preference_switch(ui);
|
|
ui.separator();
|
|
|
|
ui.label(format!("{} Hz", self.data_frequency.load(Ordering::Relaxed)));
|
|
|
|
ui.separator();
|
|
|
|
if ui.add_enabled(connected, toggle_start_stop(&mut self.on)).changed() {
|
|
self.update_start_stop();
|
|
};
|
|
|
|
// Spacer to push the LED to the right
|
|
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
|
ui.scope(|ui| {
|
|
let is_connected = self.connected.load(Ordering::Relaxed);
|
|
let color = if is_connected { Color32::GREEN } else { Color32::RED };
|
|
let tooltip = if is_connected { "Connected" } else { "Disconnected" };
|
|
|
|
// Allocate a fixed-size rectangle for the LED
|
|
let led_size = egui::Vec2::splat(12.0);
|
|
let (rect, response) = ui.allocate_exact_size(led_size, egui::Sense::hover());
|
|
|
|
// Draw the circle
|
|
let center = rect.center();
|
|
let radius = 5.0;
|
|
ui.painter().circle_filled(center, radius, color);
|
|
|
|
// Tooltip
|
|
if response.hovered() {
|
|
response.on_hover_text(tooltip);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
|
let available_height = ui.available_height();
|
|
let half_height = available_height / 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
|
|
ui.allocate_ui_with_layout(
|
|
egui::vec2(ui.available_width(), half_height),
|
|
Layout::top_down(egui::Align::Min),
|
|
|ui| {
|
|
// Magnitude
|
|
let magnitude = self.magnitude_series.lock().unwrap();
|
|
Plot::new("magnitude")
|
|
.allow_scroll(false)
|
|
.allow_drag(false)
|
|
// .center_y_axis(true)
|
|
.legend(Legend::default().position(Corner::LeftTop))
|
|
.y_axis_label("Magnitude [Ω]")
|
|
.y_axis_min_width(2.0)
|
|
.show(ui, |plot_ui| {
|
|
plot_ui.line(
|
|
Line::new("Magnitude", magnitude.plot_values())
|
|
.color(Color32::BLUE)
|
|
);
|
|
});
|
|
},
|
|
);
|
|
// Plot pressure
|
|
ui.allocate_ui_with_layout(
|
|
egui::vec2(ui.available_width(), half_height),
|
|
Layout::top_down(egui::Align::Min),
|
|
|ui| {
|
|
// Phase
|
|
let phase = self.phase_series.lock().unwrap();
|
|
Plot::new("phase")
|
|
.allow_scroll(false)
|
|
.allow_drag(false)
|
|
// .center_y_axis(true)
|
|
.legend(Legend::default().position(Corner::LeftTop))
|
|
.y_axis_label("Phase [rad]")
|
|
.y_axis_min_width(2.0)
|
|
.show(ui, |plot_ui| {
|
|
plot_ui.line(
|
|
Line::new("Phase", phase.plot_values())
|
|
.color(Color32::RED)
|
|
);
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
// Plot::new("State")
|
|
// .allow_scroll(false)
|
|
// .allow_drag(false)
|
|
// .data_aspect(1.0)
|
|
// .center_y_axis(true)
|
|
// .show(ui, |plot_ui| {
|
|
// plot_ui.points(point_pos);
|
|
// plot_ui.set_plot_bounds(bounds);
|
|
// });
|
|
});
|
|
|
|
// CMD- or control-W to close window
|
|
if ctx.input(|i| i.modifiers.cmd_ctrl_matches(Modifiers::COMMAND))
|
|
&& ctx.input(|i| i.key_pressed(Key::W))
|
|
{
|
|
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
|
}
|
|
|
|
// CMD- or control-W to close window
|
|
if ctx.input(|i| i.key_pressed(Key::Space))
|
|
{
|
|
self.on = !self.on;
|
|
self.update_start_stop();
|
|
}
|
|
|
|
// Send stop command when the window is closed
|
|
if ctx.input(|i| i.viewport().close_requested()) {
|
|
self.on = false;
|
|
self.update_start_stop();
|
|
}
|
|
|
|
ctx.request_repaint();
|
|
}}
|
|
|
|
|
|
fn toggle_ui_start_stop(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
|
|
let desired_size = ui.spacing().interact_size.y * egui::vec2(2.0, 1.0);
|
|
let (rect, mut response) = ui.allocate_exact_size(desired_size, egui::Sense::click());
|
|
if response.clicked() {
|
|
*on = !*on;
|
|
response.mark_changed();
|
|
}
|
|
response.widget_info(|| {
|
|
egui::WidgetInfo::selected(egui::WidgetType::Checkbox, ui.is_enabled(), *on, "")
|
|
});
|
|
|
|
if ui.is_rect_visible(rect) {
|
|
let how_on = ui.ctx().animate_bool_responsive(response.id, *on);
|
|
let visuals = ui.style().interact_selectable(&response, *on);
|
|
let rect = rect.expand(visuals.expansion);
|
|
let radius = 0.5 * rect.height();
|
|
ui.painter().rect(
|
|
rect,
|
|
radius,
|
|
visuals.bg_fill,
|
|
visuals.bg_stroke,
|
|
egui::StrokeKind::Inside,
|
|
);
|
|
let circle_x = egui::lerp((rect.left() + radius)..=(rect.right() - radius), how_on);
|
|
let center = egui::pos2(circle_x, rect.center().y);
|
|
ui.painter()
|
|
.circle(center, 0.75 * radius, visuals.bg_fill, visuals.fg_stroke);
|
|
}
|
|
|
|
response
|
|
}
|
|
|
|
pub fn toggle_start_stop(on: &mut bool) -> impl egui::Widget + '_ {
|
|
move |ui: &mut egui::Ui| toggle_ui_start_stop(ui, on)
|
|
} |