-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Add ping_1d example using library
- Loading branch information
1 parent
e80a0a4
commit 67d02f2
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |