Created basic sinus output via postcard rpc.

This commit is contained in:
2025-08-06 18:26:17 +02:00
parent 2d8c6d23fd
commit 0b55cdf8b8
10 changed files with 3384 additions and 29 deletions

136
src/bin/main_cli.rs Normal file
View File

@@ -0,0 +1,136 @@
use std::{
io::{stdout, Write},
time::{Duration, Instant},
};
use bioz_host_rs::{client::WorkbookClient, icd, read_line};
#[tokio::main]
async fn main() {
println!("Connecting to USB device...");
let client = WorkbookClient::new();
for i in 0..10 {
print!("Pinging with {i}... ");
let res = client.ping(i).await.unwrap();
println!("got {res}!");
assert_eq!(res, i);
}
println!("Connected! Pinging 42");
let ping = client.ping(42).await.unwrap();
println!("Got: {ping}.");
let uid = client.get_id().await.unwrap();
let hex_uid = uid.iter().map(|b| format!("{:02X}", b)).collect::<Vec<_>>().join(" ");
println!("ID (HEX): {}", hex_uid);
println!("Connected! Pinging 42");
let ping = client.ping(42).await.unwrap();
println!("Got: {ping}.");
println!();
// Begin repl...
loop {
print!("> ");
stdout().flush().unwrap();
let line = read_line().await;
let parts: Vec<&str> = line.split_whitespace().collect();
match parts.as_slice() {
["ping"] => {
let ping = client.ping(42).await.unwrap();
println!("Got: {ping}.");
}
["ping", n] => {
let Ok(idx) = n.parse::<u32>() else {
println!("Bad u32: '{n}'");
continue;
};
let ping = client.ping(idx).await.unwrap();
println!("Got: {ping}.");
}
["led", freq] => {
let Ok(freq) = freq.parse() else {
println!("Bad freq: {freq} Hz");
continue;
};
client.set_green_led(freq).await.unwrap();
}
["imp", "listen", freq, dur] => {
let Ok(freq) = freq.parse::<f32>() else {
println!("Bad freq: {freq}");
continue;
};
let Ok(dur) = dur.parse::<u32>() else {
println!("Bad dur: {dur}");
continue;
};
let mut sub = client
.client
.subscribe_multi::<icd::ImpedanceTopic>(8)
.await
.unwrap();
client.start_impedancemeter(freq).await.unwrap();
println!("Started!");
let dur = Duration::from_millis(dur.into());
let start = Instant::now();
while start.elapsed() < dur {
let val = sub.recv().await.unwrap();
println!("acc: {val:?}");
}
client.stop_impedancemeter().await.unwrap();
println!("Stopped!");
}
["imp", "start", freq] => {
let Ok(freq) = freq.parse::<f32>() else {
println!("Bad freq: {freq}");
continue;
};
match client.start_impedancemeter(freq).await {
Ok(_) => println!("Started!"),
Err(e) => println!("Error starting impedancemeter: {:?}", e),
};
}
["imp", "stop"] => {
let res = client.stop_impedancemeter().await.unwrap();
println!("Stopped: {res}");
}
["schema"] => {
let schema = client.client.get_schema_report().await.unwrap();
println!();
println!("# Endpoints");
println!();
for ep in &schema.endpoints {
println!("* '{}'", ep.path);
println!(" * Request: {}", ep.req_ty);
println!(" * Response: {}", ep.resp_ty);
}
println!();
println!("# Topics Client -> Server");
println!();
for tp in &schema.topics_in {
println!("* '{}'", tp.path);
println!(" * Message: {}", tp.ty);
}
println!();
println!("# Topics Client <- Server");
println!();
for tp in &schema.topics_out {
println!("* '{}'", tp.path);
println!(" * Message: {}", tp.ty);
}
println!();
}
other => {
println!("Error, didn't understand '{other:?};");
}
}
}
}

41
src/bin/main_gui.rs Normal file
View 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))),
);
}