Hopefully first complete build

This commit is contained in:
Blizzard Finnegan 2023-05-10 13:00:23 -04:00
parent 0b73b71dfc
commit ddfdc7894f
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
4 changed files with 54 additions and 67 deletions

View file

@ -1,9 +1,10 @@
use std::{fs::{self, File}, path::Path, io::{BufReader, BufRead, BufWriter, Write}, thread, time::Duration};
use crate::{tty, gpio_facade::Relay};
use crate::tty;
use rppal::gpio::Gpio;
const BOOT_TIME:Duration = Duration::new(60, 0);
const BP_START:Duration = Duration::new(60, 0);
const BP_RUN:Duration = Duration::new(60, 0);
const BP_START:Duration = Duration::new(3, 0);
const BP_RUN:Duration = Duration::new(75, 0);
const REBOOTS_SECTION: &str = "Reboots: ";
const BP_SECTION: &str = "Successful BP tests: ";
const TEMP_SECTION: &str = "Successful temp tests: ";
@ -20,7 +21,8 @@ pub enum State{
pub struct Device{
usb_tty: tty::TTY,
output_file: Option<File>,
pin: Option<&'static mut Relay>,
gpio: rppal::gpio::Gpio,
pin: Option<u8>,
serial: String,
current_state: State,
reboots: u64,
@ -65,6 +67,7 @@ impl Device{
pub fn new(usb_port:tty::TTY) -> Self{
let mut output = Self{
usb_tty: usb_port,
gpio: Gpio::new().unwrap(),
pin: None,
output_file: None,
serial: UNINITIALISED_SERIAL.to_string(),
@ -180,19 +183,22 @@ impl Device{
self.load_values();
return self;
}
pub fn set_gpio(&mut self, gpio_pin:Relay) -> &mut Self{
self.pin = Some(&mut gpio_pin);
pub fn get_serial(&mut self) -> &str{
&self.serial
}
pub fn set_pin_address(&mut self, address:u8) -> &mut Self{
self.pin = Some(address);
return self;
}
pub fn start_temp(&mut self) -> &mut Self {
if let Some(ref mut gpio_pin) = self.pin{
gpio_pin.high();
if let Some(_) = self.pin {
self.gpio.get(self.pin.unwrap()).unwrap().into_output().set_high();
}
return self;
}
pub fn stop_temp(&mut self) -> &mut Self {
if let Some(ref mut gpio_pin) = self.pin{
gpio_pin.low();
if let Some(_) = self.pin {
self.gpio.get(self.pin.unwrap()).unwrap().into_output().set_low();
}
return self;
}

View file

@ -1,58 +1,22 @@
use rppal::gpio::{Gpio, OutputPin};
use once_cell::sync::Lazy;
const GPIO:Lazy<rppal::gpio::Gpio> = Lazy::new(|| Gpio::new().unwrap());
const RELAY_ADDRESSES: [u8;10] = [4,5,6,12,13,17,18,19,20,26];
pub struct GpioFacade{
pub struct GpioPins{
unassigned_addresses:Vec<u8>
}
pub struct Relay{
relay:Box<OutputPin>
}
impl Relay{
pub fn new(pin:OutputPin) -> Self{
Self{ relay:Box::new(pin) }
}
pub fn low(&mut self) -> &mut Self{
self.relay.set_low();
return self;
}
pub fn high(&mut self) -> &mut Self{
self.relay.set_high();
return self;
}
pub fn address(&mut self) -> u8 {
return self.relay.pin();
}
}
impl GpioFacade{
impl GpioPins{
pub fn new() -> Self {
let mut output = Self { unassigned_addresses:Vec::new() };
for pin in RELAY_ADDRESSES.iter(){
output.unassigned_addresses.push(*pin);
//output.unassigned_addresses.push(Box::new(Relay::new(GPIO.get(*pin).unwrap().into_output())));
}
return output;
}
pub fn remove_pin(&mut self, address:u8) -> Option<Relay>{
let mut removed_address:u8 = 0;
for unassigned_address in self.unassigned_addresses.iter_mut() {
if address == *unassigned_address{
removed_address = address;
}
}
if removed_address > 0{
self.unassigned_addresses.retain(|&assigned_address| assigned_address != removed_address );
return Some(Relay::new(GPIO.get(removed_address).unwrap().into_output()));
}
return None;
}
pub fn remove_address(&mut self, address:u8) -> () {
self.unassigned_addresses.retain(|&x| x != address);
}
pub fn get_unassigned_addresses(&mut self) -> &mut Vec<u8>{
return &mut self.unassigned_addresses;
}

View file

@ -2,13 +2,13 @@
use log::{warn,error,debug,info,trace};
use serialport;
use crate::serialport::SerialPortInfo;
use seymour_poc_rust::{device, tty,log_facade,gpio_facade::{Relay, self, GpioFacade}, tty::TTY};
use seymour_poc_rust::{device, tty,log_facade,gpio_facade::GpioPins, tty::TTY};
use std::io::{stdin,stdout,Write};
#[allow(unused_imports)]
use std::{thread, time::Duration};
fn int_input_filtering(prompt:Option<&str>) -> i64{
fn int_input_filtering(prompt:Option<&str>) -> u64{
let internal_prompt = prompt.unwrap_or(">>>");
let mut user_input:String = String::new();
print!("{}",internal_prompt);
@ -20,7 +20,7 @@ fn int_input_filtering(prompt:Option<&str>) -> i64{
if let Some('\r')=user_input.chars().next_back() {
user_input.pop();
}
return user_input.parse().unwrap_or(-1);
return user_input.parse().unwrap_or(0);
}
fn input_filtering(prompt:Option<&str>) -> String{
@ -38,9 +38,9 @@ fn input_filtering(prompt:Option<&str>) -> String{
return user_input;
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main(){
log_facade::setup_logs().unwrap();
let mut gpio = GpioFacade::new();
let gpio = &mut GpioPins::new();
let available_ttys:Vec<SerialPortInfo> = tty::AVAILABLE_TTYS.clone();
let mut possible_devices:Vec<Option<device::Device>> = Vec::new();
for possible_tty in available_ttys.to_vec(){
@ -73,14 +73,31 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
device.brighten_screen()
.set_serial(&input_filtering(Some("Enter the serial of the device with the bright screen: ")).to_string())
.darken_screen();
let unassigned_relays:&Vec<Box<Relay>> = gpio.get_unassigned_relays();
for relay in unassigned_relays.into_iter(){
_ = &relay.high();
let mut unassigned_addresses:Vec<u8> = gpio.get_unassigned_addresses().to_vec();
for address in unassigned_addresses.iter_mut(){
device.set_pin_address(*address).start_temp();
if device.is_temp_running(){
device.set_gpio(**relay);
device.stop_temp();
break;
}
else{
device.stop_temp();
}
}
}
Ok(())
let mut iteration_count:u64 = 0;
while iteration_count < 1{
iteration_count = int_input_filtering(Some("Enter the number of iterations to complete: "));
}
while let Some(mut device) = devices.pop(){
thread::spawn(move||{
for i in 1..=iteration_count{
println!("Starting iteration {} of {} for device {}...",
i,iteration_count,device.get_serial());
device.test_cycle(None, None);
}
}).join().unwrap();
}
}

View file

@ -1,6 +1,6 @@
use std::{collections::HashMap, io::{BufReader, BufRead}};
use std::{collections::HashMap, io::{BufReader, BufRead, Write}};
use once_cell::sync::Lazy;
use serialport::{SerialPortInfo, SerialPort};
use serialport::{SerialPortInfo, TTYPort};
use derivative::Derivative;
pub const BAUD_RATE:u32 = 115200;
@ -68,7 +68,7 @@ const RESPONSE_MAP:Lazy<HashMap<&str,Response>> = Lazy::new(||HashMap::from([
]));
pub struct TTY{
tty: Box<dyn SerialPort>
tty: Box<TTYPort>
}
impl TTY{
@ -78,13 +78,13 @@ impl TTY{
}
else {
return TTY {
tty: serialport::new(serial_location, BAUD_RATE).open().unwrap()
tty: Box::new(TTYPort::open(&serialport::new(serial_location, BAUD_RATE)).unwrap())
};
}
}
pub fn write_to_device(&mut self,command:Command) -> bool {
return self.tty.write(COMMAND_MAP.get(&command).unwrap().as_bytes()).unwrap() > 0;
return self.tty.write_all(COMMAND_MAP.get(&command).unwrap().as_bytes()).is_ok();
}
pub fn read_from_device(&mut self,break_char:Option<&str>) -> Response {