Minor readability and usability tweaks

- Privatised TTY constants, as they weren't being used elsewhere
- shifted imports slightly in Main
- TTY now has a constant defining serial timeout
- gpio_facade::get_unassigned_addresses() now returns a reference,
  rather than a mutable reference, allowiing to remove a copy in Main
This commit is contained in:
Blizzard Finnegan 2023-05-11 20:45:53 -04:00
parent 2f93f493c4
commit 04e1cf9f3f
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
4 changed files with 18 additions and 19 deletions

View file

@ -13,11 +13,12 @@ impl GpioPins{
return output;
}
pub fn remove_address(&mut self, address:u8) -> () {
pub fn remove_address(&mut self, address:u8) -> &mut Self {
self.unassigned_addresses.retain(|x| *x != address);
self
}
pub fn get_unassigned_addresses(&mut self) -> &mut Vec<u8>{
return &mut self.unassigned_addresses;
pub fn get_unassigned_addresses(&self) -> &Vec<u8>{
return &self.unassigned_addresses;
}
}

View file

@ -22,7 +22,7 @@ pub fn setup_logs() -> Result<(), fern::InitError>{
.level(log::LevelFilter::Trace)
.chain(fern::log_file(
format!("logs/{0}.log",
chrono_now.format("%Y-%m-%d_%H.%M.%S").to_string()
chrono_now.format("%Y-%m-%d_%H.%M").to_string()
))?),
)
.chain(

View file

@ -1,8 +1,6 @@
#[allow(unused_imports)]
use log::{warn,error,debug,info,trace};
use serialport;
use crate::serialport::SerialPortInfo;
use seymour_poc_rust::{device, tty,log_facade,gpio_facade::GpioPins, tty::TTY};
use seymour_poc_rust::{device::Device, tty::{self,TTY,Response},log_facade,gpio_facade::GpioPins};
use std::io::{stdin,stdout,Write};
use std::thread::{self, JoinHandle};
@ -33,24 +31,25 @@ fn input_filtering(prompt:Option<&str>) -> String{
if let Some('\r')=user_input.chars().next_back() {
user_input.pop();
}
log::debug!("{}:{}",internal_prompt,user_input);
return user_input;
}
fn main(){
log_facade::setup_logs().unwrap();
_ = log_facade::setup_logs();
let gpio = &mut GpioPins::new();
let available_ttys:Vec<SerialPortInfo> = tty::AVAILABLE_TTYS.clone();
let mut possible_devices:Vec<Option<device::Device>> = Vec::new();
let mut tty_test_threads:Vec<JoinHandle<Option<device::Device>>> = Vec::new();
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.to_vec(){
tty_test_threads.push(thread::spawn(move ||{
let mut possible_port = TTY::new(possible_tty.port_name.to_string());
log::info!("Testing port {}. This may take a moment...",possible_tty.port_name);
possible_port.write_to_device(tty::Command::Newline);
let response = possible_port.read_from_device(Some(":"));
if response != tty::Response::Empty{
if response != Response::Empty{
log::debug!("{} is valid port!",possible_tty.port_name);
Some(device::Device::new(possible_port,Some(response)))
Some(Device::new(possible_port,Some(response)))
}
else{
None
@ -62,7 +61,7 @@ fn main(){
possible_devices.push(output);
}
let mut devices:Vec<device::Device> = Vec::new();
let mut devices:Vec<Device> = Vec::new();
for possible_device in possible_devices.into_iter(){
if let Some(device) = possible_device{
devices.push(device);
@ -80,11 +79,9 @@ fn main(){
device.brighten_screen()
.set_serial(&input_filtering(Some("Enter the serial of the device with the bright screen: ")).to_string())
.darken_screen();
let unassigned_addresses:Vec<u8> = gpio.get_unassigned_addresses().to_vec();
log::debug!("Number of unassigned addresses: {}",unassigned_addresses.len());
for address in unassigned_addresses{
log::debug!("Number of unassigned addresses: {}",gpio.get_unassigned_addresses().len());
for &address in gpio.get_unassigned_addresses(){
device.set_pin_address(address).start_temp();
thread::sleep(std::time::Duration::new(3,0));
if device.is_temp_running(){
device.stop_temp();
gpio.remove_address(address);

View file

@ -3,7 +3,8 @@ use once_cell::sync::Lazy;
use serialport::{SerialPortInfo,SerialPort};
use derivative::Derivative;
pub const BAUD_RATE:u32 = 115200;
const BAUD_RATE:u32 = 115200;
const SERIAL_READ_TIMEOUT: std::time::Duration = Duration::from_millis(500);
pub const AVAILABLE_TTYS: Lazy<Vec<SerialPortInfo>> = Lazy::new(||serialport::available_ports().unwrap());
@ -90,7 +91,7 @@ impl TTY{
}
else {
return TTY {
tty: serialport::new(serial_location,BAUD_RATE).timeout(Duration::new(1, 0)).open().unwrap()
tty: serialport::new(serial_location,BAUD_RATE).timeout(SERIAL_READ_TIMEOUT).open().unwrap()
};
}
}