Skip to content

Commit

Permalink
examples: Add ping_1d example using library
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulTrombin committed Apr 22, 2024
1 parent e80a0a4 commit 67d02f2
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/ping_1d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use ping_rs::{
device::{Ping1D, PingDevice},
error::PingError,
};
use tokio_serial::SerialPortBuilderExt;

#[tokio::main]
async fn main() -> Result<(), PingError> {
let mut port = tokio_serial::new("/dev/ttyUSB1", 115200).open_native_async()?;
#[cfg(unix)]
port.set_exclusive(false)?;

let ping1d = Ping1D::new(port);

let mut subscribed = ping1d.subscribe();
tokio::spawn(async move {
loop {
let received = subscribed.recv().await;
match received {
Ok(msg) => println!("Subscribed channel received: \n Start: \n {msg:?} \n Ending"),
Err(_e) => break,
}
}
});

match ping1d.get_firmware().await {
Ok(version) => {
println!("Firmware version: {:?}", version);
}
Err(err) => {
eprintln!("Error getting firmware version: {:?}", err);
}
}

match ping1d.get_device_id().await {
Ok(version) => {
println!("Device id: {:?}", version);
}
Err(err) => {
eprintln!("Error getting device id: {:?}", err);
}
}

Ok(())
}

0 comments on commit 67d02f2

Please sign in to comment.