Skip to content

Commit 8bbbc2c

Browse files
authored
chore: Add blocking io example. (#90)
1 parent 2739a1b commit 8bbbc2c

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ required-features = ["embedded_io_async"]
3636
test = false
3737

3838

39+
[[example]]
40+
name = "blocking"
41+
path = "examples/local_blocking.rs"
42+
required-features = ["embedded_io"]
43+
test = false
44+
45+
3946
[[example]]
4047
name = "raw"
4148
path = "examples/local_raw.rs"

examples/local_blocking.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use embedded_io::ErrorKind;
2+
use embedded_io_adapters::std::FromStd;
3+
use std::env;
4+
use std::process::exit;
5+
use std::time::Duration;
6+
use uf_crsf::blocking_io::BlockingCrsfReader;
7+
use uf_crsf::error::CrsfStreamError;
8+
9+
fn main() {
10+
let ports = match serialport::available_ports() {
11+
Ok(ports) => ports,
12+
Err(e) => {
13+
eprintln!("Failed to enumerate serial ports: {}", e);
14+
exit(1);
15+
}
16+
};
17+
18+
if ports.is_empty() {
19+
eprintln!("No serial ports found.");
20+
eprintln!("Please specify a serial port path as an argument.");
21+
exit(1);
22+
}
23+
24+
let path = env::args().nth(1).unwrap_or_else(|| {
25+
const DEFAULT_PORT: &str = "/dev/tty.usbmodem00000000001B1";
26+
if ports.iter().any(|p| p.port_name == DEFAULT_PORT) {
27+
println!(
28+
"No serial port specified. Using default port: {}",
29+
DEFAULT_PORT
30+
);
31+
DEFAULT_PORT.to_string()
32+
} else {
33+
println!("No serial port specified. Available ports:");
34+
for p in &ports {
35+
println!(" {}", p.port_name);
36+
}
37+
println!("\nUsing first available port: {}", ports[0].port_name);
38+
ports[0].port_name.clone()
39+
}
40+
});
41+
42+
let mut port = serialport::new(&path, 420_000)
43+
.timeout(Duration::from_millis(10))
44+
.open()
45+
.unwrap_or_else(|e| {
46+
eprintln!("Failed to open serial port '{}': {}", &path, e);
47+
exit(1);
48+
});
49+
50+
let adapted_port = FromStd::new(&mut port);
51+
let mut reader = BlockingCrsfReader::new(adapted_port);
52+
println!("Reading from serial port '{}'...", path);
53+
54+
loop {
55+
match reader.read_packet() {
56+
Ok(packet) => {
57+
println!("{:?}", packet);
58+
}
59+
Err(CrsfStreamError::Io(ErrorKind::TimedOut)) => {}
60+
Err(e) => {
61+
eprintln!("Error reading from serial port: {:?}", e);
62+
}
63+
}
64+
}
65+
}

justfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ example_simple:
5959
example_async:
6060
cargo run --example=async --all-features
6161

62+
# Run example local_simple that parses hard coded buffer.
63+
[group('examples')]
64+
example_blocking:
65+
cargo run --example=blocking --all-features
66+
67+
6268
# Lint source code with strict linter
6369
[group('lint')]
6470
pedantic:

0 commit comments

Comments
 (0)