Skip to content

Commit

Permalink
src: examples: Share resources between examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulTrombin committed Oct 10, 2024
1 parent 1882623 commit 5ea50cd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 74 deletions.
70 changes: 70 additions & 0 deletions examples/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use clap::Parser;
use std::{
net::{IpAddr, SocketAddr},
path::PathBuf,
str::FromStr,
};
use tokio_serial::{SerialPort, SerialPortBuilderExt};
use udp_stream::UdpStream;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[arg(long, group = "source", conflicts_with_all = ["udp_address"])]
pub serial_port: Option<PathBuf>,
#[arg(long, default_value_t = 115200)]
pub serial_baud_rate: u32,
#[arg(long, group = "source", conflicts_with_all = ["serial_port"])]
pub udp_address: Option<IpAddr>,
#[arg(long, default_value_t = 8080)]
pub udp_port: u32,
}

pub enum Port {
Serial(tokio_serial::SerialStream),
Udp(udp_stream::UdpStream),
}

pub async fn create_port() -> Port {
let args = Args::parse();

match (args.serial_port, args.udp_address) {
(Some(serial_port), None) => {
println!("Using serial port: {:?}", serial_port);
let port = tokio_serial::new(serial_port.to_string_lossy(), args.serial_baud_rate)
.open_native_async()
.map_err(|e| {
eprintln!("Error opening serial port: {}", e);
e
})
.unwrap();
port.clear(tokio_serial::ClearBuffer::All).unwrap();
Port::Serial(port)
}
(None, Some(udp_address)) => {
println!("Using UDP address: {}", udp_address);
let socket_addr = SocketAddr::from_str(&format!("{}:{}", udp_address, args.udp_port))
.map_err(|e| {
eprintln!("Error parsing UDP address: {}", e);
e
})
.unwrap();
let port = UdpStream::connect(socket_addr)
.await
.map_err(|e| {
eprintln!("Error connecting to UDP socket: {}", e);
e
})
.unwrap();
Port::Udp(port)
}
(None, None) => {
eprintln!("Error: either serial_port_name or udp_address must be provided");
std::process::exit(1);
}
(Some(_), Some(_)) => {
eprintln!("Error: serial_port_name and udp_address are mutually exclusive");
std::process::exit(1);
}
}
}
77 changes: 3 additions & 74 deletions examples/ping_1d.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use clap::Parser;
use std::{
convert::TryFrom,
net::{IpAddr, SocketAddr},
path::PathBuf,
str::FromStr,
};
use udp_stream::UdpStream;
mod common;
use common::{create_port, Port};
use std::convert::TryFrom;

use bluerobotics_ping::{
device::{Ping1D, PingDevice},
Expand All @@ -14,7 +9,6 @@ use bluerobotics_ping::{
ping1d::{self, ProfileStruct},
Messages,
};
use tokio_serial::{SerialPort, SerialPortBuilderExt};

#[tokio::main]
async fn main() -> Result<(), PingError> {
Expand Down Expand Up @@ -137,68 +131,3 @@ async fn main() -> Result<(), PingError> {

Ok(())
}

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(long, group = "source",
conflicts_with_all = ["udp_address"])]
serial_port: Option<PathBuf>,
#[arg(long, default_value_t = 115200)]
serial_baud_rate: u32,
#[arg(long, group = "source",
conflicts_with_all = ["serial_port"])]
udp_address: Option<IpAddr>,
#[arg(long, default_value_t = 8080)]
udp_port: u32,
}

enum Port {
Serial(tokio_serial::SerialStream),
Udp(udp_stream::UdpStream),
}

async fn create_port() -> Port {
let args = Args::parse();

let port = match (args.serial_port, args.udp_address) {
(Some(serial_port), None) => {
println!("Using serial port: {:?}", serial_port);
let port = tokio_serial::new(serial_port.to_string_lossy(), args.serial_baud_rate)
.open_native_async()
.map_err(|e| {
eprintln!("Error opening serial port: {}", e);
e
})
.unwrap();
port.clear(tokio_serial::ClearBuffer::All).unwrap();
Port::Serial(port)
}
(None, Some(udp_address)) => {
println!("Using UDP address: {}", udp_address);
let socket_addr = SocketAddr::from_str(&format!("{}:{}", udp_address, args.udp_port))
.map_err(|e| {
eprintln!("Error parsing UDP address: {}", e);
e
})
.unwrap();
let port = UdpStream::connect(socket_addr)
.await
.map_err(|e| {
eprintln!("Error connecting to UDP socket: {}", e);
e
})
.unwrap();
Port::Udp(port)
}
(None, None) => {
eprintln!("Error: either serial_port_name or udp_address must be provided");
std::process::exit(1);
}
(Some(_), Some(_)) => {
eprintln!("Error: serial_port_name and udp_address are mutually exclusive");
std::process::exit(1);
}
};
port
}

0 comments on commit 5ea50cd

Please sign in to comment.