Attempt to generalise serial init
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Blizzard Finnegan 2023-06-29 09:58:20 -04:00
parent 32789a4546
commit b27da2155e
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
3 changed files with 126 additions and 94 deletions

7
Cargo.lock generated
View file

@ -302,6 +302,12 @@ dependencies = [
"log",
]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "heck"
version = "0.4.1"
@ -618,6 +624,7 @@ dependencies = [
"clap",
"derivative",
"fern",
"glob",
"log",
"once_cell",
"rppal",

View file

@ -20,6 +20,7 @@ once_cell = "1.17.1"
derivative = "2.2.0"
time = "0.2.23"
clap = { version = "4.3.2", features = ["derive"] }
glob = "0.3.1"
[dev-dependencies]
time = "0.2.23"

View file

@ -58,7 +58,7 @@ fn input_filtering(prompt:Option<&str>) -> String{
log::debug!("{}:{}",internal_prompt,user_input);
return user_input;
}
//Path::new(&&str).is_dir() -> bool
fn main(){
let args = Args::parse();
setup_logs(&args.debug);
@ -80,18 +80,54 @@ fn main(){
log::info!("Testing all available USB ports for connected devices. This may take several minutes, and devices may reboot several times.");
let gpio = &mut GpioPins::new();
match std::fs::read_dir("/dev/serial/by-path"){
Ok(available_ttys)=>{
let mut available_ttys:Vec<Box<Path>> = Vec::new();
for entry in glob::glob("/dev/serial/*").expect("Failed to read glob pattern"){
match entry{
Ok(real_path) =>{
match fs::read_dir::<&Path>(real_path.as_ref()){
Ok(possible_ttys) =>{
possible_ttys.into_iter().for_each(|tty| {
if let Ok(single_tty) = tty {
available_ttys.push(single_tty.path().into());
}
});
break;
}
Err(error) =>{
log::error!("Invalid permissions to /dev directory... did you run with sudo?");
log::error!("{}",error);
return;
}
}
}
Err(error) =>{
log::error!("{}",error);
}
}
}
if available_ttys.is_empty(){
for entry in glob::glob("/dev/ttyUSB*").expect("Unable to read glob"){
match entry{
Ok(possible_tty) => available_ttys.push(Path::new(&possible_tty).into()),
Err(error) => {
log::error!("Invalid permissions to /dev directory... did you run with sudo?");
log::error!("{}",error);
return;
}
};
}
}
if available_ttys.is_empty(){
log::error!("No serial devices detected! Please ensure all connections.");
return;
}
let mut possible_devices:Vec<Option<Device>> = Vec::new();
let mut tty_test_threads:Vec<JoinHandle<Option<Device>>> = Vec::new();
for possible_tty in available_ttys.into_iter(){
tty_test_threads.push(
thread::spawn(move ||{
let tty_ref = possible_tty.as_ref();
match tty_ref{
Ok(tty_real_ref)=>{
let tty_path = tty_real_ref.path();
let tty_name = tty_path.to_string_lossy();
let tty_name = possible_tty.to_string_lossy();
log::debug!("Testing port {}",&tty_name);
let possible_port = TTY::new(&tty_name);
match possible_port{
@ -116,12 +152,6 @@ fn main(){
},
None=>{None}
}
},
Err(error)=>{
log::debug!("{}",error);
None
}
}
}));
}
for thread in tty_test_threads{
@ -175,12 +205,6 @@ fn main(){
for thread in iteration_threads{
thread.join().unwrap();
}
}
Err(_)=>{
log::error!("Invalid serial location! Please make sure that /dev/serial/by-path exists.");
break;
}
}
if input_filtering(Some("Would you like to run the tests again? (y/N): ")).to_string().contains("y") {}
else { break; }
}