Improve GPIO facade implementation
This commit is contained in:
parent
3c818950f3
commit
605ea16b3a
2 changed files with 54 additions and 32 deletions
|
@ -1,6 +1,5 @@
|
|||
use rppal::gpio::OutputPin;
|
||||
use std::{fs::{self, File}, path::Path, io::{BufReader, BufRead}, thread, time::Duration};
|
||||
use crate::tty;
|
||||
use crate::{tty, gpio_facade::Relay};
|
||||
|
||||
const BOOT_TIME:Duration = Duration::new(60, 0);
|
||||
const BP_START:Duration = Duration::new(60, 0);
|
||||
|
@ -16,7 +15,7 @@ pub enum State{
|
|||
pub struct Device{
|
||||
usb_tty: tty::TTY,
|
||||
output_file: Option<File>,
|
||||
pin: Option<OutputPin>,
|
||||
pin: Option<Relay>,
|
||||
serial: String,
|
||||
current_state: State,
|
||||
reboots: u64,
|
||||
|
@ -160,19 +159,19 @@ impl Device{
|
|||
self.load_values();
|
||||
return self;
|
||||
}
|
||||
pub fn set_gpio(&mut self, gpio_pin: OutputPin) -> &mut Self{
|
||||
pub fn set_gpio(&mut self, gpio_pin: Relay) -> &mut Self{
|
||||
self.pin = Some(gpio_pin);
|
||||
return self;
|
||||
}
|
||||
pub fn start_temp(&mut self) -> &mut Self {
|
||||
if let Some(ref mut gpio_pin) = self.pin{
|
||||
gpio_pin.set_high();
|
||||
gpio_pin.high();
|
||||
}
|
||||
return self;
|
||||
}
|
||||
pub fn stop_temp(&mut self) -> &mut Self {
|
||||
if let Some(ref mut gpio_pin) = self.pin{
|
||||
gpio_pin.set_high();
|
||||
gpio_pin.low();
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
|
|
@ -1,31 +1,54 @@
|
|||
pub mod gpio_facade{
|
||||
use rppal::gpio::{Gpio, OutputPin};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::ValuesMut;
|
||||
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];
|
||||
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{
|
||||
relays:HashMap<u8,OutputPin>
|
||||
pub struct GpioFacade{
|
||||
unassigned_relays:Vec<Box<Relay>>
|
||||
}
|
||||
|
||||
pub struct Relay{
|
||||
relay:Box<OutputPin>
|
||||
}
|
||||
|
||||
impl Relay{
|
||||
pub fn new(pin:OutputPin) -> Self{
|
||||
Self{ relay:Box::new(pin) }
|
||||
}
|
||||
|
||||
impl GpioFacade{
|
||||
pub fn new() -> Self {
|
||||
let mut output = Self { relays:HashMap::new() };
|
||||
for pin in RELAY_ADDRESSES.iter(){
|
||||
output.relays.entry(*pin).or_insert(GPIO.get(*pin).unwrap().into_output());
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn remove_pin(&mut self, address:u8) -> OutputPin{
|
||||
return self.relays.remove(&address).unwrap();
|
||||
}
|
||||
|
||||
pub fn get_unassigned_pins(&mut self) -> ValuesMut<u8,OutputPin>{
|
||||
return self.relays.values_mut();
|
||||
}
|
||||
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{
|
||||
pub fn new() -> Self {
|
||||
let mut output = Self { unassigned_relays:Vec::new() };
|
||||
for pin in RELAY_ADDRESSES.iter(){
|
||||
output.unassigned_relays.push(Box::new(Relay::new(GPIO.get(*pin).unwrap().into_output())));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn remove_pin(&mut self, address:u8) -> Option<&mut Box<Relay>>{
|
||||
for relay in self.unassigned_relays.iter_mut() {
|
||||
if &relay.address() == &address{
|
||||
|
||||
return Some(relay);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
pub fn get_unassigned_relays(&mut self) -> &Vec<Box<Relay>>{
|
||||
return &self.unassigned_relays;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue