Added logger to GUI.

This commit is contained in:
2025-10-15 18:11:10 +02:00
parent cb7bc2f025
commit 7229d4cd33
10 changed files with 269 additions and 94 deletions

View File

@@ -1,7 +1,9 @@
use std::time::SystemTime;
use log::{error, info};
use tokio::select;
use tokio::sync::mpsc::{Sender, Receiver};
use tokio::sync::mpsc::{Receiver, Sender};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use atomic_float::AtomicF32;
@@ -28,7 +30,7 @@ pub async fn communicate_with_hardware(
connected: Arc<AtomicBool>,
data_frequency: Arc<AtomicF32>,
periods_per_dft: Arc<Mutex<Option<f32>>>,
periods_per_dft_multi: Arc<Mutex<(Vec<f32>, Option<Vec<f32>>)>>,
periods_per_dft_multi: Arc<Mutex<(Vec<u32>, Option<Vec<f32>>)>>,
gui_logging_enabled: Arc<AtomicBool>,
log_tx: Sender<LoggingSignal>,
) {
@@ -42,17 +44,17 @@ pub async fn communicate_with_hardware(
}
});
#[derive(Default)]
#[derive(Default, Clone, Copy)]
struct Settings {
frequency: Option<StartStopSignal>,
}
let mut settings = Settings::default();
let settings = Arc::new(Mutex::new(Settings::default()));
loop {
let workbook_client = match WorkbookClient::new() {
Ok(client) => {
info!("Connected to hardware successfully.");
if let Some(frequency) = settings.frequency {
if let Some(frequency) = settings.lock().unwrap().frequency {
run_impedancemeter_tx.send(frequency).await.unwrap();
}
connected.store(true, Ordering::Relaxed);
@@ -79,6 +81,8 @@ pub async fn communicate_with_hardware(
let gui_logging_enabled_clone = gui_logging_enabled.clone();
let log_tx_clone = log_tx.clone();
let settings_clone = settings.clone();
tokio::spawn(async move {
while let Ok(val) = single_impedance_sub.recv().await {
{
@@ -97,9 +101,20 @@ pub async fn communicate_with_hardware(
}
// Send logging signal
if gui_logging_enabled_clone.load(Ordering::Relaxed) {
if let Err(e) = log_tx_clone.try_send(LoggingSignal::SingleImpedance(val.magnitude, val.phase)) {
error!("Failed to send logging signal: {:?}", e);
let settings = *settings_clone.lock().unwrap();
match settings.frequency {
Some(StartStopSignal::StartSingle(freq, _)) => {
if let Err(e) = log_tx_clone.send(LoggingSignal::SingleImpedance(SystemTime::now(), freq, val.magnitude, val.phase)).await {
error!("Failed to send logging signal: {:?}", e);
}
},
_ => {
error!("Frequency not set for single impedance logging.");
},
}
// if let Err(e) = log_tx_clone.send(LoggingSignal::SingleImpedance(SystemTime::now(), settings.frequency, val.magnitude, val.phase)).await {
// error!("Failed to send logging signal: {:?}", e);
// }
}
}
info!("SingleImpedanceOutputTopic subscription ended.");
@@ -133,7 +148,7 @@ pub async fn communicate_with_hardware(
bode_plot.update_phases(MeasurementPointSet::Eight, phases.clone());
}
if gui_logging_enabled_clone.load(Ordering::Relaxed) {
if let Err(e) = log_tx_clone.try_send(LoggingSignal::MultiImpedance(magnitudes.clone(), phases.clone())) {
if let Err(e) = log_tx_clone.send(LoggingSignal::MultiImpedance(SystemTime::now(), MeasurementPointSet::Eight.values().to_vec(), magnitudes.clone(), phases.clone())).await {
error!("Failed to send logging signal: {:?}", e);
}
}
@@ -147,7 +162,7 @@ pub async fn communicate_with_hardware(
bode_plot.update_phases(MeasurementPointSet::Eighteen, phases.clone());
}
if gui_logging_enabled_clone.load(Ordering::Relaxed) {
if let Err(e) = log_tx_clone.try_send(LoggingSignal::MultiImpedance(magnitudes.clone(), phases.clone())) {
if let Err(e) = log_tx_clone.send(LoggingSignal::MultiImpedance(SystemTime::now(), MeasurementPointSet::Eighteen.values().to_vec(), magnitudes.clone(), phases.clone())).await {
error!("Failed to send logging signal: {:?}", e);
}
}
@@ -166,7 +181,7 @@ pub async fn communicate_with_hardware(
match workbook_client.start_impedancemeter_single(freq, dft_num).await {
Ok(Ok(periods)) => {
info!("Impedance meter started at frequency: {} with periods per DFT: {}", freq, periods);
settings.frequency = Some(StartStopSignal::StartSingle(freq, dft_num));
settings.lock().unwrap().frequency = Some(StartStopSignal::StartSingle(freq, dft_num));
*periods_per_dft.lock().unwrap() = Some(periods);
},
Ok(Err(e)) => {
@@ -182,7 +197,7 @@ pub async fn communicate_with_hardware(
StartStopSignal::StartMulti(num_points) => {
match workbook_client.start_impedancemeter_multi(num_points).await {
Ok(Ok(periods)) => {
settings.frequency = Some(StartStopSignal::StartMulti(num_points));
settings.lock().unwrap().frequency = Some(StartStopSignal::StartMulti(num_points));
info!("Multi-point Impedancemeter started.");
match num_points {
MeasurementPointSet::Eight => {
@@ -207,7 +222,7 @@ pub async fn communicate_with_hardware(
if let Err(e) = workbook_client.stop_impedancemeter().await {
error!("Failed to stop impedancemeter: {:?}", e);
} else {
settings.frequency = Some(StartStopSignal::Stop);
settings.lock().unwrap().frequency = Some(StartStopSignal::Stop);
*periods_per_dft.lock().unwrap() = None;
let (freq, _) = periods_per_dft_multi.lock().unwrap().clone();
*periods_per_dft_multi.lock().unwrap() = (freq, None);