From beefe733c058adf197831f6eeba3afac3ea53d9a Mon Sep 17 00:00:00 2001 From: Hubald Verzijl Date: Fri, 12 Sep 2025 22:40:55 +0200 Subject: [PATCH] Included setting dft number from gui. --- src/app.rs | 35 ++++++++++++++++++++++++++++++++--- src/bin/main_cli.rs | 8 ++++---- src/client.rs | 5 ++++- src/communication.rs | 6 +++--- src/signals.rs | 4 +++- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index 154269b..39cc604 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,7 +6,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use atomic_float::AtomicF32; use tokio::{sync::mpsc::{Sender}}; -use eframe::egui::{self, Button, CollapsingHeader, Color32, DragValue, Key, Label, Layout, Modifiers}; +use eframe::egui::{self, Button, CollapsingHeader, Color32, ComboBox, DragValue, Key, Label, Layout, Modifiers}; use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds}; use egui_dock::{DockArea, DockState, Style}; @@ -14,6 +14,15 @@ use crate::plot::TimeSeriesPlot; use crate::signals::SingleFrequencySignal; +use crate::icd::IcdDftNum; + +const DFTNUM_VARIANTS: [IcdDftNum; 13] = [ + IcdDftNum::Num4, IcdDftNum::Num8, IcdDftNum::Num16, IcdDftNum::Num32, + IcdDftNum::Num64, IcdDftNum::Num128, IcdDftNum::Num256, IcdDftNum::Num512, + IcdDftNum::Num1024, IcdDftNum::Num2048, IcdDftNum::Num4096, + IcdDftNum::Num8192, IcdDftNum::Num16384, +]; + #[derive(Clone, Copy,Debug, PartialEq, Eq)] enum TabActive { Single, @@ -33,7 +42,8 @@ pub struct App { pub on: Arc>, tab_active: TabActive, pub data_frequency: Arc, - pub single_frequency: Arc> + pub single_frequency: Arc>, + pub dft_num: Arc>, } struct TabViewer { @@ -43,6 +53,7 @@ struct TabViewer { phase_series: Arc>, on: Arc>, single_frequency: Arc>, + dft_num: Arc>, show_settings: bool, show_settings_toggle: Option, } @@ -62,6 +73,21 @@ impl TabViewer { } ui.label("Hz"); }); + ui.horizontal(|ui| { + ui.label("ADC samples per DFT:"); + let mut dft_num = self.dft_num.lock().unwrap(); + let mut index = DFTNUM_VARIANTS.iter().position(|&x| x == *dft_num).unwrap_or(0); + ComboBox::from_id_salt("Dftnum") + .width(75.0) + .show_index(ui, &mut index, DFTNUM_VARIANTS.len(), |i| { + format!("{}", 1 << (2 + i)) // 2^2 = 4, 2^3 = 8, ..., 2^14 = 16384 + }); + let new_value = DFTNUM_VARIANTS[index]; + if *dft_num != new_value { + *dft_num = new_value; + info!("DFTNUM setting changed!"); + }; + }); }); } }); @@ -155,6 +181,7 @@ impl App { 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 dft_num = Arc::new(Mutex::new(IcdDftNum::Num2048)); let on = Arc::new(Mutex::new(true)); let tab_active = TabActive::Single; @@ -165,6 +192,7 @@ impl App { magnitude_series: magnitude_series.clone(), phase_series: phase_series.clone(), single_frequency: single_frequency.clone(), + dft_num: dft_num.clone(), on: on.clone(), show_settings: false, show_settings_toggle: None, @@ -184,6 +212,7 @@ impl App { tab_active, data_frequency: Arc::new(AtomicF32::new(0.0)), single_frequency, + dft_num, }; app.update_start_stop(); @@ -193,7 +222,7 @@ impl App { pub fn update_start_stop(&self) { match (self.tab_active, *self.on.lock().unwrap()) { (TabActive::Single, true) => { - if let Err(e) = self.run_impedancemeter_tx.try_send(SingleFrequencySignal::Start(*self.single_frequency.lock().unwrap())) { + if let Err(e) = self.run_impedancemeter_tx.try_send(SingleFrequencySignal::Start(*self.single_frequency.lock().unwrap(), *self.dft_num.lock().unwrap())) { eprintln!("Failed to send start command: {:?}", e); } }, diff --git a/src/bin/main_cli.rs b/src/bin/main_cli.rs index de86652..e648f49 100644 --- a/src/bin/main_cli.rs +++ b/src/bin/main_cli.rs @@ -78,8 +78,8 @@ async fn main() { .subscribe_multi::(8) .await .unwrap(); - client.start_impedancemeter(freq).await.unwrap(); - println!("Started!"); + client.start_impedancemeter(freq, bioz_icd_rs::IcdDftNum::Num2048).await.unwrap(); + println!("Started with dft_num 2048!"); let dur = Duration::from_millis(dur.into()); let start = Instant::now(); @@ -97,8 +97,8 @@ async fn main() { }; - match client.start_impedancemeter(freq).await { - Ok(_) => println!("Started!"), + match client.start_impedancemeter(freq, bioz_icd_rs::IcdDftNum::Num2048).await { + Ok(_) => println!("Started with dft_num 2048!"), Err(e) => println!("Error starting impedancemeter: {:?}", e), }; diff --git a/src/client.rs b/src/client.rs index 003e200..e33f8ee 100644 --- a/src/client.rs +++ b/src/client.rs @@ -9,6 +9,8 @@ use bioz_icd_rs::{ StopImpedanceEndpoint, }; +use crate::icd::IcdDftNum; + #[derive(Debug)] pub struct WorkbookClient { pub client: HostClient, @@ -64,9 +66,10 @@ impl WorkbookClient { pub async fn start_impedancemeter( &self, frequency: u32, + dft_number: IcdDftNum, ) -> Result<(), WorkbookError> { self.client - .send_resp::(&StartImpedance { update_frequency: 60, sinus_frequency: frequency }) + .send_resp::(&StartImpedance { update_frequency: 60, sinus_frequency: frequency, dft_number}) .await?; Ok(()) } diff --git a/src/communication.rs b/src/communication.rs index 0ddab3b..658e6bc 100644 --- a/src/communication.rs +++ b/src/communication.rs @@ -90,11 +90,11 @@ pub async fn communicate_with_hardware( select! { Some(frequency) = run_impedancemeter_rx.recv() => { match frequency { - SingleFrequencySignal::Start(freq) => { - if let Err(e) = workbook_client.start_impedancemeter(freq).await { + SingleFrequencySignal::Start(freq, dft_num) => { + if let Err(e) = workbook_client.start_impedancemeter(freq, dft_num).await { error!("Failed to start impedancemeter: {:?}", e); } else { - settings.frequency = Some(SingleFrequencySignal::Start(freq)); + settings.frequency = Some(SingleFrequencySignal::Start(freq, dft_num)); info!("Impedancemeter started at frequency: {}", freq); } }, diff --git a/src/signals.rs b/src/signals.rs index 1346436..5df4e25 100644 --- a/src/signals.rs +++ b/src/signals.rs @@ -1,5 +1,7 @@ +use crate::icd::IcdDftNum; + #[derive(Copy, Clone)] pub enum SingleFrequencySignal { - Start(u32), + Start(u32, IcdDftNum), // frequency in Hz, DFT number Stop, } \ No newline at end of file