mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2025-12-06 05:11:17 +00:00
Created basic sinus output via postcard rpc.
This commit is contained in:
132
src/app.rs
Normal file
132
src/app.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
use tokio::{sync::mpsc::{self, Receiver, Sender}, time::Timeout};
|
||||
|
||||
use eframe::egui::{self, Button, Checkbox, Color32, DragValue, Key, Layout, Modifiers, RichText, Rounding, };
|
||||
use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds};
|
||||
|
||||
use crate::plot::TimeSeriesPlot;
|
||||
|
||||
pub struct App {
|
||||
interval_ms: u32,
|
||||
run_impedancemeter_tx: Sender<u32>,
|
||||
pub magnitude: Arc<Mutex<f32>>,
|
||||
pub phase: Arc<Mutex<f32>>,
|
||||
pub magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||
pub phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(run_impedancemeter_tx: Sender<u32>) -> Self {
|
||||
App {
|
||||
interval_ms: 10, // Default interval
|
||||
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())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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| {
|
||||
egui::widgets::global_theme_preference_switch(ui);
|
||||
ui.separator();
|
||||
|
||||
if ui.add(DragValue::new(&mut self.interval_ms).speed(0.1).range(RangeInclusive::new(0, 50)).update_while_editing(false)).changed() {
|
||||
if let Err(e) = self.run_impedancemeter_tx.try_send(0) {
|
||||
eprintln!("Failed to send stop command: {:?}", e);
|
||||
}
|
||||
// Delay
|
||||
if let Err(e) = self.run_impedancemeter_tx.try_send(self.interval_ms) {
|
||||
eprintln!("Failed to send interval update: {:?}", e);
|
||||
}
|
||||
};
|
||||
|
||||
if ui.button("Start").clicked() {
|
||||
if let Err(e) = self.run_impedancemeter_tx.try_send(self.interval_ms) {
|
||||
eprintln!("Failed to send start command: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
ui.separator();
|
||||
|
||||
if ui.button("Stop").clicked() {
|
||||
if let Err(e) = self.run_impedancemeter_tx.try_send(0) {
|
||||
eprintln!("Failed to send stop command: {:?}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
let available_height = ui.available_height();
|
||||
let mut half_height = available_height / 4.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 and phase
|
||||
let magnitude = self.magnitude_series.lock().unwrap();
|
||||
let phase = self.phase_series.lock().unwrap();
|
||||
Plot::new("magnitude_phase")
|
||||
.allow_scroll(false)
|
||||
.allow_drag(false)
|
||||
// .center_y_axis(true)
|
||||
.legend(Legend::default().position(Corner::LeftTop))
|
||||
.y_axis_label("Value [...]")
|
||||
.y_axis_min_width(2.0)
|
||||
.show(ui, |plot_ui| {
|
||||
|
||||
plot_ui.line(
|
||||
Line::new("Magnitude", magnitude.plot_values())
|
||||
.color(Color32::LIGHT_GREEN)
|
||||
);
|
||||
plot_ui.line(
|
||||
Line::new("Phase", phase.plot_values())
|
||||
.color(Color32::LIGHT_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);
|
||||
}
|
||||
|
||||
ctx.request_repaint();
|
||||
}}
|
||||
Reference in New Issue
Block a user