use eframe::NativeOptions; use eframe::egui::Vec2; use simple_logger::SimpleLogger; use log::info; use tokio::runtime::Runtime; use bioz_host_rs::{app::App, signals::LoggingSignal}; use bioz_host_rs::communication::communicate_with_hardware; use tokio::sync::mpsc::{self}; use bioz_host_rs::signals::StartStopSignal; use bioz_host_rs::logging::log_data; #[tokio::main] async fn main() { SimpleLogger::new().init().expect("Failed to initialize logger"); log::set_max_level(log::LevelFilter::Info); info!("Starting Bioz Impedance Visualizer..."); let rt = Runtime::new().expect("Unable to create Runtime"); // Enter the runtime so that `tokio::spawn` is available immediately. // let _enter = rt.enter(); // Channel to communicate with the communication task. let (run_impedancemeter_tx, run_impedancemeter_rx) = mpsc::channel::(2); let run_impedancemeter_tx_clone = run_impedancemeter_tx.clone(); // Logging let (log_tx, log_rx) = mpsc::channel::(10); let log_tx_clone = log_tx.clone(); let app = App::new(run_impedancemeter_tx, log_tx); let magnitude_clone = app.magnitude.clone(); let phase_clone = app.phase.clone(); let magnitude_series_clone = app.magnitude_series.clone(); let phase_series_clone = app.phase_series.clone(); let bode_clone = app.bode_plot.clone(); let connected_clone = app.connected.clone(); let data_frequency_clone = app.data_frequency.clone(); let periods_per_dft = app.periods_per_dft.clone(); let periods_per_dft_sweep = app.periods_per_dft_sweep.clone(); let gui_logging_state_1 = app.gui_logging_state.clone(); let gui_logging_state_2 = app.gui_logging_state.clone(); // Log thread tokio::spawn(async move { log_data(log_rx, gui_logging_state_1).await }); // Execute the runtime in its own thread. std::thread::spawn(move || { rt.block_on(communicate_with_hardware( run_impedancemeter_rx, run_impedancemeter_tx_clone, magnitude_clone, phase_clone, magnitude_series_clone, phase_series_clone, bode_clone, connected_clone, data_frequency_clone, periods_per_dft, periods_per_dft_sweep, gui_logging_state_2, log_tx_clone, )); }); // Run the GUI in the main thread. let mut native_options = NativeOptions::default(); native_options.viewport.inner_size = Some(Vec2::new(850.0, 600.0)); let _ = eframe::run_native( "Impedance Visualizer [egui + tokio] - Hubald Verzijl - 2025", native_options, Box::new(|_cc| Ok(Box::new(app))), ); }