Attempt to generalise serial init
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
32789a4546
commit
b27da2155e
3 changed files with 126 additions and 94 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -302,6 +302,12 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "glob"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "heck"
|
name = "heck"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
|
@ -618,6 +624,7 @@ dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"derivative",
|
"derivative",
|
||||||
"fern",
|
"fern",
|
||||||
|
"glob",
|
||||||
"log",
|
"log",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rppal",
|
"rppal",
|
||||||
|
|
|
@ -20,6 +20,7 @@ once_cell = "1.17.1"
|
||||||
derivative = "2.2.0"
|
derivative = "2.2.0"
|
||||||
time = "0.2.23"
|
time = "0.2.23"
|
||||||
clap = { version = "4.3.2", features = ["derive"] }
|
clap = { version = "4.3.2", features = ["derive"] }
|
||||||
|
glob = "0.3.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
time = "0.2.23"
|
time = "0.2.23"
|
||||||
|
|
64
src/main.rs
64
src/main.rs
|
@ -58,7 +58,7 @@ fn input_filtering(prompt:Option<&str>) -> String{
|
||||||
log::debug!("{}:{}",internal_prompt,user_input);
|
log::debug!("{}:{}",internal_prompt,user_input);
|
||||||
return user_input;
|
return user_input;
|
||||||
}
|
}
|
||||||
|
//Path::new(&&str).is_dir() -> bool
|
||||||
fn main(){
|
fn main(){
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
setup_logs(&args.debug);
|
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.");
|
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();
|
let gpio = &mut GpioPins::new();
|
||||||
match std::fs::read_dir("/dev/serial/by-path"){
|
let mut available_ttys:Vec<Box<Path>> = Vec::new();
|
||||||
Ok(available_ttys)=>{
|
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 possible_devices:Vec<Option<Device>> = Vec::new();
|
||||||
let mut tty_test_threads:Vec<JoinHandle<Option<Device>>> = Vec::new();
|
let mut tty_test_threads:Vec<JoinHandle<Option<Device>>> = Vec::new();
|
||||||
for possible_tty in available_ttys.into_iter(){
|
for possible_tty in available_ttys.into_iter(){
|
||||||
tty_test_threads.push(
|
tty_test_threads.push(
|
||||||
thread::spawn(move ||{
|
thread::spawn(move ||{
|
||||||
let tty_ref = possible_tty.as_ref();
|
let tty_name = possible_tty.to_string_lossy();
|
||||||
match tty_ref{
|
|
||||||
Ok(tty_real_ref)=>{
|
|
||||||
let tty_path = tty_real_ref.path();
|
|
||||||
let tty_name = tty_path.to_string_lossy();
|
|
||||||
log::debug!("Testing port {}",&tty_name);
|
log::debug!("Testing port {}",&tty_name);
|
||||||
let possible_port = TTY::new(&tty_name);
|
let possible_port = TTY::new(&tty_name);
|
||||||
match possible_port{
|
match possible_port{
|
||||||
|
@ -116,12 +152,6 @@ fn main(){
|
||||||
},
|
},
|
||||||
None=>{None}
|
None=>{None}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Err(error)=>{
|
|
||||||
log::debug!("{}",error);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for thread in tty_test_threads{
|
for thread in tty_test_threads{
|
||||||
|
@ -175,12 +205,6 @@ fn main(){
|
||||||
for thread in iteration_threads{
|
for thread in iteration_threads{
|
||||||
thread.join().unwrap();
|
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") {}
|
if input_filtering(Some("Would you like to run the tests again? (y/N): ")).to_string().contains("y") {}
|
||||||
else { break; }
|
else { break; }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue