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:
66
src/communication.rs
Normal file
66
src/communication.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use tokio::select;
|
||||
use tokio::sync::mpsc::Receiver;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::icd;
|
||||
use crate::client::WorkbookClient;
|
||||
|
||||
use crate::plot::TimeSeriesPlot;
|
||||
|
||||
pub async fn communicate_with_hardware(
|
||||
mut run_impedancemeter_rx: Receiver<u32>,
|
||||
magnitude: Arc<Mutex<f32>>,
|
||||
phase: Arc<Mutex<f32>>,
|
||||
magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||
phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||
) {
|
||||
let workbook_client = WorkbookClient::new();
|
||||
|
||||
let mut sub = workbook_client
|
||||
.client
|
||||
.subscribe_multi::<icd::ImpedanceTopic>(8)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tokio::spawn(async move {
|
||||
while let Ok(val) = sub.recv().await {
|
||||
let mut mag_plot = magnitude_series.lock().unwrap();
|
||||
let mut phase_plot = phase_series.lock().unwrap();
|
||||
|
||||
*magnitude.lock().unwrap() = val.magnitude;
|
||||
*phase.lock().unwrap() = val.phase;
|
||||
|
||||
mag_plot.add(val.magnitude as f64);
|
||||
phase_plot.add(val.phase as f64);
|
||||
}
|
||||
});
|
||||
|
||||
loop {
|
||||
select! {
|
||||
Some(run) = run_impedancemeter_rx.recv() => {
|
||||
if run > 0 {
|
||||
// Start the impedancemeter
|
||||
if let Err(e) = workbook_client.start_impedancemeter(run as f32).await {
|
||||
eprintln!("Failed to start impedancemeter: {:?}", e);
|
||||
} else {
|
||||
println!("Impedancemeter started.");
|
||||
}
|
||||
} else {
|
||||
// Stop the impedancemeter
|
||||
if let Err(e) = workbook_client.stop_impedancemeter().await {
|
||||
eprintln!("Failed to stop impedancemeter: {:?}", e);
|
||||
} else {
|
||||
println!("Impedancemeter stopped.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else => {
|
||||
// All channels closed
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user