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:
50
.vscode/tasks.json
vendored
50
.vscode/tasks.json
vendored
@@ -4,11 +4,13 @@
|
|||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"tasks": [
|
"tasks": [
|
||||||
{
|
{
|
||||||
"label": "cargo run",
|
"label": "cargo run cli",
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"command": "~/.cargo/bin/cargo", // note: full path to the cargo
|
"command": "~/.cargo/bin/cargo", // note: full path to the cargo
|
||||||
"args": [
|
"args": [
|
||||||
"run",
|
"run",
|
||||||
|
"--bin",
|
||||||
|
"main_cli",
|
||||||
// "--release",
|
// "--release",
|
||||||
],
|
],
|
||||||
"group": {
|
"group": {
|
||||||
@@ -17,11 +19,43 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "cargo run release",
|
"label": "cargo run cli release",
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"command": "~/.cargo/bin/cargo", // note: full path to the cargo
|
"command": "~/.cargo/bin/cargo", // note: full path to the cargo
|
||||||
"args": [
|
"args": [
|
||||||
"run",
|
"run",
|
||||||
|
"--bin",
|
||||||
|
"main_cli",
|
||||||
|
"--release",
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
// "isDefault": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "cargo run gui",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "~/.cargo/bin/cargo", // note: full path to the cargo
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"--bin",
|
||||||
|
"main_gui",
|
||||||
|
// "--release",
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
// "isDefault": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "cargo run gui release",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "~/.cargo/bin/cargo", // note: full path to the cargo
|
||||||
|
"args": [
|
||||||
|
"run",
|
||||||
|
"--bin",
|
||||||
|
"main_gui",
|
||||||
"--release",
|
"--release",
|
||||||
],
|
],
|
||||||
"group": {
|
"group": {
|
||||||
@@ -41,12 +75,12 @@
|
|||||||
"type": "shell",
|
"type": "shell",
|
||||||
"problemMatcher": []
|
"problemMatcher": []
|
||||||
},
|
},
|
||||||
{
|
// {
|
||||||
"label": "cargo rerun",
|
// "label": "cargo rerun",
|
||||||
"dependsOn": ["Terminate All Tasks", "delay", "cargo run"],
|
// "dependsOn": ["Terminate All Tasks", "delay", "cargo run"],
|
||||||
"dependsOrder": "sequence",
|
// "dependsOrder": "sequence",
|
||||||
"group": "build",
|
// "group": "build",
|
||||||
}
|
// }
|
||||||
],
|
],
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
|
|||||||
3053
Cargo.lock
generated
3053
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,8 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
defmt = { version = "1.0.1" }
|
defmt = { version = "1.0.1" }
|
||||||
|
eframe = { version = "0.32.0"}
|
||||||
|
egui_plot = "0.33.0"
|
||||||
|
|
||||||
[dependencies.bioz-icd-rs]
|
[dependencies.bioz-icd-rs]
|
||||||
path = "../bioz-icd-rs"
|
path = "../bioz-icd-rs"
|
||||||
|
|||||||
132
src/app.rs
Normal file
132
src/app.rs
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
use std::sync::{Arc, Mutex, RwLock};
|
||||||
|
|
||||||
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
|
use tokio::{sync::mpsc::{self, Receiver, Sender}, time::Timeout};
|
||||||
|
|
||||||
|
use eframe::egui::{self, Button, Checkbox, Color32, DragValue, Key, Layout, Modifiers, RichText, Rounding, };
|
||||||
|
use egui_plot::{Corner, Legend, Line, Plot, PlotPoints, Points, PlotBounds};
|
||||||
|
|
||||||
|
use crate::plot::TimeSeriesPlot;
|
||||||
|
|
||||||
|
pub struct App {
|
||||||
|
interval_ms: u32,
|
||||||
|
run_impedancemeter_tx: Sender<u32>,
|
||||||
|
pub magnitude: Arc<Mutex<f32>>,
|
||||||
|
pub phase: Arc<Mutex<f32>>,
|
||||||
|
pub magnitude_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||||
|
pub phase_series: Arc<Mutex<TimeSeriesPlot>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
pub fn new(run_impedancemeter_tx: Sender<u32>) -> Self {
|
||||||
|
App {
|
||||||
|
interval_ms: 10, // Default interval
|
||||||
|
run_impedancemeter_tx,
|
||||||
|
magnitude: Arc::new(Mutex::new(0.0)),
|
||||||
|
phase: Arc::new(Mutex::new(0.0)),
|
||||||
|
magnitude_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
||||||
|
phase_series: Arc::new(Mutex::new(TimeSeriesPlot::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eframe::App for App {
|
||||||
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||||
|
// Egui add a top bar
|
||||||
|
egui::TopBottomPanel::top("top_bar").show(ctx, |ui| {
|
||||||
|
egui::MenuBar::new().ui(ui, |ui| {
|
||||||
|
egui::widgets::global_theme_preference_switch(ui);
|
||||||
|
ui.separator();
|
||||||
|
|
||||||
|
if ui.add(DragValue::new(&mut self.interval_ms).speed(0.1).range(RangeInclusive::new(0, 50)).update_while_editing(false)).changed() {
|
||||||
|
if let Err(e) = self.run_impedancemeter_tx.try_send(0) {
|
||||||
|
eprintln!("Failed to send stop command: {:?}", e);
|
||||||
|
}
|
||||||
|
// Delay
|
||||||
|
if let Err(e) = self.run_impedancemeter_tx.try_send(self.interval_ms) {
|
||||||
|
eprintln!("Failed to send interval update: {:?}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if ui.button("Start").clicked() {
|
||||||
|
if let Err(e) = self.run_impedancemeter_tx.try_send(self.interval_ms) {
|
||||||
|
eprintln!("Failed to send start command: {:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.separator();
|
||||||
|
|
||||||
|
if ui.button("Stop").clicked() {
|
||||||
|
if let Err(e) = self.run_impedancemeter_tx.try_send(0) {
|
||||||
|
eprintln!("Failed to send stop command: {:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
let available_height = ui.available_height();
|
||||||
|
let mut half_height = available_height / 4.0;
|
||||||
|
|
||||||
|
let point_pos = vec![[*self.magnitude.lock().unwrap() as f64, *self.phase.lock().unwrap() as f64]];
|
||||||
|
|
||||||
|
let point_pos = PlotPoints::new(point_pos);
|
||||||
|
let point_pos = Points::new("pos", point_pos)
|
||||||
|
.radius(20.0)
|
||||||
|
.color(Color32::LIGHT_RED);
|
||||||
|
|
||||||
|
let bounds = PlotBounds::from_min_max([-1.5, -1.5], [1.5, 1.5]);
|
||||||
|
|
||||||
|
// Use a vertical layout to stack the plots
|
||||||
|
ui.with_layout(Layout::top_down(egui::Align::Min), |ui| {
|
||||||
|
// Plot pressure
|
||||||
|
ui.allocate_ui_with_layout(
|
||||||
|
egui::vec2(ui.available_width(), half_height),
|
||||||
|
Layout::top_down(egui::Align::Min),
|
||||||
|
|ui| {
|
||||||
|
// Magnitude and phase
|
||||||
|
let magnitude = self.magnitude_series.lock().unwrap();
|
||||||
|
let phase = self.phase_series.lock().unwrap();
|
||||||
|
Plot::new("magnitude_phase")
|
||||||
|
.allow_scroll(false)
|
||||||
|
.allow_drag(false)
|
||||||
|
// .center_y_axis(true)
|
||||||
|
.legend(Legend::default().position(Corner::LeftTop))
|
||||||
|
.y_axis_label("Value [...]")
|
||||||
|
.y_axis_min_width(2.0)
|
||||||
|
.show(ui, |plot_ui| {
|
||||||
|
|
||||||
|
plot_ui.line(
|
||||||
|
Line::new("Magnitude", magnitude.plot_values())
|
||||||
|
.color(Color32::LIGHT_GREEN)
|
||||||
|
);
|
||||||
|
plot_ui.line(
|
||||||
|
Line::new("Phase", phase.plot_values())
|
||||||
|
.color(Color32::LIGHT_RED)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Plot::new("State")
|
||||||
|
.allow_scroll(false)
|
||||||
|
.allow_drag(false)
|
||||||
|
.data_aspect(1.0)
|
||||||
|
.center_y_axis(true)
|
||||||
|
.show(ui, |plot_ui| {
|
||||||
|
plot_ui.points(point_pos);
|
||||||
|
plot_ui.set_plot_bounds(bounds);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// CMD- or control-W to close window
|
||||||
|
if ctx.input(|i| i.modifiers.cmd_ctrl_matches(Modifiers::COMMAND))
|
||||||
|
&& ctx.input(|i| i.key_pressed(Key::W))
|
||||||
|
{
|
||||||
|
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.request_repaint();
|
||||||
|
}}
|
||||||
@@ -57,9 +57,9 @@ async fn main() {
|
|||||||
client.set_green_led(freq).await.unwrap();
|
client.set_green_led(freq).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
["imp", "listen", ms, dur] => {
|
["imp", "listen", freq, dur] => {
|
||||||
let Ok(ms) = ms.parse::<u32>() else {
|
let Ok(freq) = freq.parse::<f32>() else {
|
||||||
println!("Bad ms: {ms}");
|
println!("Bad freq: {freq}");
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let Ok(dur) = dur.parse::<u32>() else {
|
let Ok(dur) = dur.parse::<u32>() else {
|
||||||
@@ -72,7 +72,7 @@ async fn main() {
|
|||||||
.subscribe_multi::<icd::ImpedanceTopic>(8)
|
.subscribe_multi::<icd::ImpedanceTopic>(8)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
client.start_impedancemeter(ms).await.unwrap();
|
client.start_impedancemeter(freq).await.unwrap();
|
||||||
println!("Started!");
|
println!("Started!");
|
||||||
let dur = Duration::from_millis(dur.into());
|
let dur = Duration::from_millis(dur.into());
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
@@ -83,13 +83,13 @@ async fn main() {
|
|||||||
client.stop_impedancemeter().await.unwrap();
|
client.stop_impedancemeter().await.unwrap();
|
||||||
println!("Stopped!");
|
println!("Stopped!");
|
||||||
}
|
}
|
||||||
["imp", "start", ms] => {
|
["imp", "start", freq] => {
|
||||||
let Ok(ms) = ms.parse::<u32>() else {
|
let Ok(freq) = freq.parse::<f32>() else {
|
||||||
println!("Bad ms: {ms}");
|
println!("Bad freq: {freq}");
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
match client.start_impedancemeter(ms).await {
|
match client.start_impedancemeter(freq).await {
|
||||||
Ok(_) => println!("Started!"),
|
Ok(_) => println!("Started!"),
|
||||||
Err(e) => println!("Error starting impedancemeter: {:?}", e),
|
Err(e) => println!("Error starting impedancemeter: {:?}", e),
|
||||||
};
|
};
|
||||||
41
src/bin/main_gui.rs
Normal file
41
src/bin/main_gui.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
use tokio::runtime::Runtime;
|
||||||
|
|
||||||
|
use bioz_host_rs::app::App;
|
||||||
|
|
||||||
|
use bioz_host_rs::communication::communicate_with_hardware;
|
||||||
|
|
||||||
|
use tokio::sync::mpsc::{self};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
let rt = Runtime::new().expect("Unable to create Runtime");
|
||||||
|
|
||||||
|
// Enter the runtime so that `tokio::spawn` is available immediately.
|
||||||
|
// let _enter = rt.enter();
|
||||||
|
|
||||||
|
let (run_impedancemeter_tx, run_impedancemeter_rx) = mpsc::channel::<u32>(2);
|
||||||
|
|
||||||
|
let app = App::new(run_impedancemeter_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();
|
||||||
|
|
||||||
|
// Execute the runtime in its own thread.
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
rt.block_on(communicate_with_hardware(
|
||||||
|
run_impedancemeter_rx,
|
||||||
|
magnitude_clone,
|
||||||
|
phase_clone,
|
||||||
|
magnitude_series_clone,
|
||||||
|
phase_series_clone
|
||||||
|
));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Run the GUI in the main thread.
|
||||||
|
let _ = eframe::run_native(
|
||||||
|
"Impedance Visualizer [egui + tokio] - Hubald Verzijl - 2025",
|
||||||
|
eframe::NativeOptions::default(),
|
||||||
|
Box::new(|_cc| Ok(Box::new(app))),
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -82,10 +82,10 @@ impl WorkbookClient {
|
|||||||
|
|
||||||
pub async fn start_impedancemeter(
|
pub async fn start_impedancemeter(
|
||||||
&self,
|
&self,
|
||||||
interval_ms: u32,
|
frequency: f32,
|
||||||
) -> Result<(), WorkbookError<Infallible>> {
|
) -> Result<(), WorkbookError<Infallible>> {
|
||||||
self.client
|
self.client
|
||||||
.send_resp::<StartImpedanceEndpoint>(&StartImpedance { interval_ms })
|
.send_resp::<StartImpedanceEndpoint>(&StartImpedance { update_frequency: 60, sinus_frequency: frequency })
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
#![deny(missing_debug_implementations)]
|
// #![deny(missing_debug_implementations)]
|
||||||
|
|
||||||
pub mod client;
|
pub mod client;
|
||||||
|
pub mod app;
|
||||||
|
pub mod communication;
|
||||||
|
pub mod plot;
|
||||||
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 {
|
||||||
|
|||||||
42
src/plot.rs
Normal file
42
src/plot.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
|
use egui_plot::{PlotPoint, PlotPoints};
|
||||||
|
|
||||||
|
pub struct TimeSeriesPlot {
|
||||||
|
pub values: VecDeque<PlotPoint>,
|
||||||
|
max_points: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TimeSeriesPlot {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let max_points = 2000;
|
||||||
|
Self {
|
||||||
|
values: (0..max_points as i32)
|
||||||
|
.map(|i| PlotPoint::new(i, 0.0))
|
||||||
|
.collect(), // Create x amount of (0,0) points
|
||||||
|
max_points,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add(&mut self, val: f64) {
|
||||||
|
let last_x = self.values.back().unwrap().x;
|
||||||
|
|
||||||
|
if last_x >= self.max_points as f64 {
|
||||||
|
self.values.pop_front();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.values.push_back(PlotPoint::new(last_x + 1.0, val));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn plot_values(&self) -> PlotPoints {
|
||||||
|
PlotPoints::Owned(Vec::from_iter(self.values.iter().copied()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn plot_values_negative(&self) -> PlotPoints {
|
||||||
|
let mut values = Vec::from_iter(self.values.iter().copied());
|
||||||
|
for point in &mut values {
|
||||||
|
point.y = -point.y;
|
||||||
|
}
|
||||||
|
PlotPoints::Owned(Vec::from_iter(values))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user