mirror of
https://github.com/hubaldv/bioz-host-rs.git
synced 2025-12-06 05:11:17 +00:00
Added start stop toggle, included log.
This commit is contained in:
127
src/app.rs
127
src/app.rs
@@ -1,32 +1,56 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
use tokio::{sync::mpsc::{Sender}};
|
use tokio::{sync::mpsc::{Sender}};
|
||||||
|
|
||||||
use eframe::egui::{self, Color32, DragValue, Key, Layout, Modifiers};
|
use eframe::egui::{self, Color32, DragValue, Key, Layout, Modifiers, };
|
||||||
use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds};
|
use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds};
|
||||||
|
|
||||||
use crate::plot::TimeSeriesPlot;
|
use crate::plot::TimeSeriesPlot;
|
||||||
|
|
||||||
|
use crate::signals::FrequencySignal;
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
frequency: u32,
|
frequency: f32,
|
||||||
run_impedancemeter_tx: Sender<u32>,
|
run_impedancemeter_tx: Sender<FrequencySignal>,
|
||||||
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 on: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new(run_impedancemeter_tx: Sender<u32>) -> Self {
|
pub fn new(run_impedancemeter_tx: Sender<FrequencySignal>) -> Self {
|
||||||
App {
|
let app = App {
|
||||||
frequency: 2, // Default frequency
|
frequency: 2.0, // Default frequency
|
||||||
run_impedancemeter_tx,
|
run_impedancemeter_tx,
|
||||||
magnitude: Arc::new(Mutex::new(0.0)),
|
magnitude: Arc::new(Mutex::new(0.0)),
|
||||||
phase: Arc::new(Mutex::new(0.0)),
|
phase: Arc::new(Mutex::new(0.0)),
|
||||||
magnitude_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
magnitude_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
||||||
phase_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
phase_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
||||||
|
connected: Arc::new(AtomicBool::new(false)),
|
||||||
|
on: true,
|
||||||
|
};
|
||||||
|
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(self.frequency)) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,36 +60,47 @@ impl eframe::App for App {
|
|||||||
// Egui add a top bar
|
// Egui add a top bar
|
||||||
egui::TopBottomPanel::top("top_bar").show(ctx, |ui| {
|
egui::TopBottomPanel::top("top_bar").show(ctx, |ui| {
|
||||||
egui::MenuBar::new().ui(ui, |ui| {
|
egui::MenuBar::new().ui(ui, |ui| {
|
||||||
|
let connected = self.connected.load(Ordering::Relaxed);
|
||||||
|
|
||||||
egui::widgets::global_theme_preference_switch(ui);
|
egui::widgets::global_theme_preference_switch(ui);
|
||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
if ui.add(DragValue::new(&mut self.frequency).speed(0.1).range(RangeInclusive::new(0, 50)).update_while_editing(false)).changed() {
|
let response = ui.add_enabled(connected, DragValue::new(&mut self.frequency).speed(0.1).update_while_editing(false).range(RangeInclusive::new(0, 50)));
|
||||||
if let Err(e) = self.run_impedancemeter_tx.try_send(0) {
|
|
||||||
eprintln!("Failed to send stop command: {:?}", e);
|
if response.changed() && response.lost_focus() {
|
||||||
}
|
self.update_start_stop();
|
||||||
// Delay
|
|
||||||
if let Err(e) = self.run_impedancemeter_tx.try_send(self.frequency) {
|
|
||||||
eprintln!("Failed to send frequency update: {:?}", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ui.separator();
|
||||||
|
|
||||||
|
if ui.add_enabled(connected, toggle_start_stop(&mut self.on)).changed() {
|
||||||
|
self.update_start_stop();
|
||||||
};
|
};
|
||||||
|
|
||||||
ui.separator();
|
// 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" };
|
||||||
|
|
||||||
if ui.button("Start").clicked() {
|
// Allocate a fixed-size rectangle for the LED
|
||||||
if let Err(e) = self.run_impedancemeter_tx.try_send(self.frequency) {
|
let led_size = egui::Vec2::splat(12.0);
|
||||||
eprintln!("Failed to send start command: {:?}", e);
|
let (rect, response) = ui.allocate_exact_size(led_size, egui::Sense::hover());
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ui.separator();
|
// Draw the circle
|
||||||
|
let center = rect.center();
|
||||||
|
let radius = 5.0;
|
||||||
|
ui.painter().circle_filled(center, radius, color);
|
||||||
|
|
||||||
if ui.button("Stop").clicked() {
|
// Tooltip
|
||||||
if let Err(e) = self.run_impedancemeter_tx.try_send(0) {
|
if response.hovered() {
|
||||||
eprintln!("Failed to send stop command: {:?}", e);
|
response.on_hover_text(tooltip);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
let available_height = ui.available_height();
|
let available_height = ui.available_height();
|
||||||
@@ -130,5 +165,49 @@ impl eframe::App for App {
|
|||||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
ctx.request_repaint();
|
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)
|
||||||
|
}
|
||||||
@@ -8,6 +8,8 @@ use bioz_host_rs::communication::communicate_with_hardware;
|
|||||||
|
|
||||||
use tokio::sync::mpsc::{self};
|
use tokio::sync::mpsc::{self};
|
||||||
|
|
||||||
|
use bioz_host_rs::signals::FrequencySignal;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
SimpleLogger::new().init().expect("Failed to initialize logger");
|
SimpleLogger::new().init().expect("Failed to initialize logger");
|
||||||
log::set_max_level(log::LevelFilter::Info);
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
@@ -18,13 +20,14 @@ fn main() {
|
|||||||
// Enter the runtime so that `tokio::spawn` is available immediately.
|
// Enter the runtime so that `tokio::spawn` is available immediately.
|
||||||
// let _enter = rt.enter();
|
// let _enter = rt.enter();
|
||||||
|
|
||||||
let (run_impedancemeter_tx, run_impedancemeter_rx) = mpsc::channel::<u32>(2);
|
let (run_impedancemeter_tx, run_impedancemeter_rx) = mpsc::channel::<FrequencySignal>(2);
|
||||||
|
|
||||||
let app = App::new(run_impedancemeter_tx);
|
let app = App::new(run_impedancemeter_tx);
|
||||||
let magnitude_clone = app.magnitude.clone();
|
let magnitude_clone = app.magnitude.clone();
|
||||||
let phase_clone = app.phase.clone();
|
let phase_clone = app.phase.clone();
|
||||||
let magnitude_series_clone = app.magnitude_series.clone();
|
let magnitude_series_clone = app.magnitude_series.clone();
|
||||||
let phase_series_clone = app.phase_series.clone();
|
let phase_series_clone = app.phase_series.clone();
|
||||||
|
let connected_clone = app.connected.clone();
|
||||||
|
|
||||||
// Execute the runtime in its own thread.
|
// Execute the runtime in its own thread.
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
@@ -33,7 +36,8 @@ fn main() {
|
|||||||
magnitude_clone,
|
magnitude_clone,
|
||||||
phase_clone,
|
phase_clone,
|
||||||
magnitude_series_clone,
|
magnitude_series_clone,
|
||||||
phase_series_clone
|
phase_series_clone,
|
||||||
|
connected_clone,
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use log::{error, info};
|
|||||||
use tokio::select;
|
use tokio::select;
|
||||||
use tokio::sync::mpsc::Receiver;
|
use tokio::sync::mpsc::Receiver;
|
||||||
|
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use crate::icd;
|
use crate::icd;
|
||||||
@@ -10,17 +11,21 @@ use crate::client::WorkbookClient;
|
|||||||
|
|
||||||
use crate::plot::TimeSeriesPlot;
|
use crate::plot::TimeSeriesPlot;
|
||||||
|
|
||||||
|
use crate::signals::FrequencySignal;
|
||||||
|
|
||||||
pub async fn communicate_with_hardware(
|
pub async fn communicate_with_hardware(
|
||||||
mut run_impedancemeter_rx: Receiver<u32>,
|
mut run_impedancemeter_rx: Receiver<FrequencySignal>,
|
||||||
magnitude: Arc<Mutex<f32>>,
|
magnitude: Arc<Mutex<f32>>,
|
||||||
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>>,
|
||||||
|
connected: Arc<AtomicBool>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
let workbook_client = match WorkbookClient::new() {
|
let workbook_client = match WorkbookClient::new() {
|
||||||
Ok(client) => {
|
Ok(client) => {
|
||||||
info!("Connected to hardware successfully.");
|
info!("Connected to hardware successfully.");
|
||||||
|
connected.store(true, Ordering::Relaxed);
|
||||||
client
|
client
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -57,27 +62,27 @@ pub async fn communicate_with_hardware(
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
select! {
|
select! {
|
||||||
Some(run) = run_impedancemeter_rx.recv() => {
|
Some(frequency) = run_impedancemeter_rx.recv() => {
|
||||||
if run > 0 {
|
match frequency {
|
||||||
// Start the impedancemeter
|
FrequencySignal::Start(freq) => {
|
||||||
if let Err(e) = workbook_client.start_impedancemeter(run as f32).await {
|
if let Err(e) = workbook_client.start_impedancemeter(freq).await {
|
||||||
error!("Failed to start impedancemeter: {:?}", e);
|
error!("Failed to start impedancemeter: {:?}", e);
|
||||||
} else {
|
} else {
|
||||||
info!("Impedancemeter started.");
|
info!("Impedancemeter started at frequency: {}", freq);
|
||||||
}
|
}
|
||||||
} else {
|
},
|
||||||
// Stop the impedancemeter
|
FrequencySignal::Stop => {
|
||||||
if let Err(e) = workbook_client.stop_impedancemeter().await {
|
if let Err(e) = workbook_client.stop_impedancemeter().await {
|
||||||
error!("Failed to stop impedancemeter: {:?}", e);
|
error!("Failed to stop impedancemeter: {:?}", e);
|
||||||
} else {
|
} else {
|
||||||
info!("Impedancemeter stopped.");
|
info!("Impedancemeter stopped.");
|
||||||
}
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ = workbook_client.wait_closed() => {
|
_ = workbook_client.wait_closed() => {
|
||||||
// Handle client closure
|
// Handle client closure
|
||||||
info!("Client connection closed.");
|
info!("Client connection closed.");
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,6 +94,7 @@ pub async fn communicate_with_hardware(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
info!("Communication with hardware ended.");
|
info!("Communication with hardware ended.");
|
||||||
// Wait for a short period before trying to reconnect
|
connected.store(false, Ordering::Relaxed);
|
||||||
|
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ pub mod client;
|
|||||||
pub mod app;
|
pub mod app;
|
||||||
pub mod communication;
|
pub mod communication;
|
||||||
pub mod plot;
|
pub mod plot;
|
||||||
|
pub mod signals;
|
||||||
pub use bioz_icd_rs as icd;
|
pub use bioz_icd_rs as icd;
|
||||||
|
|
||||||
pub async fn read_line() -> String {
|
pub async fn read_line() -> String {
|
||||||
|
|||||||
4
src/signals.rs
Normal file
4
src/signals.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub enum FrequencySignal {
|
||||||
|
Start(f32),
|
||||||
|
Stop,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user