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,106 +80,130 @@ 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 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();
log::debug!("Testing port {}",&tty_name);
let possible_port = TTY::new(&tty_name);
match possible_port{
Some(mut port) =>{
port.write_to_device(tty::Command::Newline);
let response = port.read_from_device(Some(":"));
if response != Response::Empty{
log::debug!("{} is valid port!",tty_name);
let new_device = Device::new(port,Some(response));
match new_device{
Ok(mut device) => {
device.darken_screen();
if !args.manual {
device.auto_set_serial();
}
Some(device)
},
Err(_) => None
}
}
else { None }
},
None=>{None}
}
},
Err(error)=>{
log::debug!("{}",error);
None
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_name = possible_tty.to_string_lossy();
log::debug!("Testing port {}",&tty_name);
let possible_port = TTY::new(&tty_name);
match possible_port{
Some(mut port) =>{
port.write_to_device(tty::Command::Newline);
let response = port.read_from_device(Some(":"));
if response != Response::Empty{
log::debug!("{} is valid port!",tty_name);
let new_device = Device::new(port,Some(response));
match new_device{
Ok(mut device) => {
device.darken_screen();
if !args.manual {
device.auto_set_serial();
}
Some(device)
},
Err(_) => None
}
}
}));
}
for thread in tty_test_threads{
let output = thread.join().unwrap_or_else(|x|{log::trace!("{:?}",x); None});
possible_devices.push(output);
}
let mut serials_set:bool = true;
let mut devices:Vec<Device> = Vec::new();
for possible_device in possible_devices.into_iter(){
if let Some(device) = possible_device{
if device.get_serial().eq("uninitialised"){
serials_set = false;
}
devices.push(device);
else { None }
},
None=>{None}
}
}
}));
}
for thread in tty_test_threads{
let output = thread.join().unwrap_or_else(|x|{log::trace!("{:?}",x); None});
possible_devices.push(output);
}
log::info!("--------------------------------------");
log::info!("Number of devices detected: {}",devices.len());
log::info!("--------------------------------------\n\n");
log::info!("Setting up probe wells for all devices. This may take several minutes...");
for device in devices.iter_mut(){
if !serials_set || args.manual {
device.brighten_screen();
device.manual_set_serial(&input_filtering(Some("Enter the serial of the device with the bright screen: ")).to_string());
device.darken_screen();
}
log::info!("Checking probe well of device {}",device.get_serial());
log::debug!("Number of unassigned addresses: {}",gpio.get_unassigned_addresses().len());
if !find_gpio(device, gpio){
device.set_pin_address(21);
log::error!("Unable to find probe-well for device {}. Please ensure that the probe well is installed properly, and the calibration key is plugged in.",device.get_serial());
device.brighten_screen();
panic!();
}
}
let mut iteration_threads = Vec::new();
while let Some(mut device) = devices.pop(){
iteration_threads.push(thread::spawn(move||{
device.init_temp_count();
for i in 1..=iteration_count{
log::info!("Starting iteration {} of {} for device {}...",
i,iteration_count,device.get_serial());
device.test_cycle(None);
}
}));
}
for thread in iteration_threads{
thread.join().unwrap();
let mut serials_set:bool = true;
let mut devices:Vec<Device> = Vec::new();
for possible_device in possible_devices.into_iter(){
if let Some(device) = possible_device{
if device.get_serial().eq("uninitialised"){
serials_set = false;
}
devices.push(device);
}
Err(_)=>{
log::error!("Invalid serial location! Please make sure that /dev/serial/by-path exists.");
break;
}
log::info!("--------------------------------------");
log::info!("Number of devices detected: {}",devices.len());
log::info!("--------------------------------------\n\n");
log::info!("Setting up probe wells for all devices. This may take several minutes...");
for device in devices.iter_mut(){
if !serials_set || args.manual {
device.brighten_screen();
device.manual_set_serial(&input_filtering(Some("Enter the serial of the device with the bright screen: ")).to_string());
device.darken_screen();
}
log::info!("Checking probe well of device {}",device.get_serial());
log::debug!("Number of unassigned addresses: {}",gpio.get_unassigned_addresses().len());
if !find_gpio(device, gpio){
device.set_pin_address(21);
log::error!("Unable to find probe-well for device {}. Please ensure that the probe well is installed properly, and the calibration key is plugged in.",device.get_serial());
device.brighten_screen();
panic!();
}
}
let mut iteration_threads = Vec::new();
while let Some(mut device) = devices.pop(){
iteration_threads.push(thread::spawn(move||{
device.init_temp_count();
for i in 1..=iteration_count{
log::info!("Starting iteration {} of {} for device {}...",
i,iteration_count,device.get_serial());
device.test_cycle(None);
}
}));
}
for thread in iteration_threads{
thread.join().unwrap();
}
if input_filtering(Some("Would you like to run the tests again? (y/N): ")).to_string().contains("y") {}
else { break; }