diff --git a/src/fan/hysteresis.rs b/src/fan/hysteresis.rs index c99b3ba..d782729 100644 --- a/src/fan/hysteresis.rs +++ b/src/fan/hysteresis.rs @@ -1,5 +1,4 @@ use crate::fan::Fan; -use crate::temperatures::Temperatures; use rppal::gpio::{Gpio, OutputPin}; @@ -25,13 +24,11 @@ impl Hysteresis { } impl Fan for Hysteresis { - fn update_temperature(&mut self, temperatures: &Temperatures) { - if temperatures.cpu > self.max_temperature { + fn update_temperature(&mut self, temperature: f64) { + if temperature > self.max_temperature { self.pin.set_high(); - } else { - if temperatures.cpu < self.max_temperature - self.hysteresis { - self.pin.set_low(); - } + } else if temperature < self.max_temperature - self.hysteresis { + self.pin.set_low(); } } diff --git a/src/fan/mod.rs b/src/fan/mod.rs index a4a7f82..3420581 100644 --- a/src/fan/mod.rs +++ b/src/fan/mod.rs @@ -1,9 +1,7 @@ pub mod hysteresis; pub mod pwm; -use crate::temperatures::Temperatures; - pub trait Fan { - fn update_temperature(&mut self, _: &Temperatures); + fn update_temperature(&mut self, temperature: f64); fn percentage(&self) -> f64; } diff --git a/src/fan/pwm.rs b/src/fan/pwm.rs index 0d47cde..c28f98f 100644 --- a/src/fan/pwm.rs +++ b/src/fan/pwm.rs @@ -1,5 +1,4 @@ use crate::fan::Fan; -use crate::temperatures::Temperatures; use rppal::pwm::{Channel, Polarity, Pwm as Pwm_rpi}; @@ -23,16 +22,16 @@ impl Pwm { } impl Fan for Pwm { - fn update_temperature(&mut self, temperatures: &Temperatures) { - let percentage = (temperatures.cpu - self.min_temperature) - / (self.max_temperature - self.min_temperature); + fn update_temperature(&mut self, temperature: f64) { + let percentage = + (temperature - self.min_temperature) / (self.max_temperature - self.min_temperature); let percentage = if percentage > 1_f64 { 1_f64 } else { percentage }; - if temperatures.cpu > self.min_temperature { + if temperature > self.min_temperature { self.pwm.enable().unwrap(); self.pwm.set_duty_cycle(percentage).unwrap(); } else { diff --git a/src/main.rs b/src/main.rs index ccb6c1e..a003618 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,11 +6,12 @@ use fan::pwm::Pwm; use fan::Fan; use rppal::pwm::Channel; +use std::error::Error; use std::thread; use std::time::Duration; -fn main() { - let mut temps = temperatures::Temperatures::default(); +fn main() -> Result<(), Box> { + let mut temps = temperatures::Temperatures::new()?; let mut fan: Box = if true { Box::new(Hysteresis::new(52.0, 5.0, 18)) @@ -20,12 +21,21 @@ fn main() { loop { temps.update(); - fan.update_temperature(&temps); - println!( - "CPU: {:.1}°C, fan is at {:.1}%", - temps.cpu, - fan.percentage() * 100_f64 - ); + + match temps.max() { + Some(t) => { + fan.update_temperature(t); + + println!( + "CPU: {:.1}°C, fan is at {:.1}%", + t, + fan.percentage() * 100_f64 + ); + } + None => { + eprintln!("Recuring problem while getting the temperature"); + } + } thread::sleep(Duration::from_secs(5)); } diff --git a/src/temperatures.rs b/src/temperatures.rs index 8a7cc12..a963981 100644 --- a/src/temperatures.rs +++ b/src/temperatures.rs @@ -1,28 +1,45 @@ +use std::error::Error; use std::fs::File; use std::io::prelude::*; pub struct Temperatures { - pub cpu: f64, + cpu: f64, + cpu_error_count: i32, } -impl Default for Temperatures { - fn default() -> Self { - Temperatures { - cpu: get_cpu_temp(), +impl Temperatures { + pub fn new() -> Result> { + Ok(Temperatures { + cpu: cpu()?, + cpu_error_count: 0, + }) + } + + pub fn update(&mut self) { + match cpu() { + Ok(t) => { + self.cpu = t; + self.cpu_error_count = 0; + } + Err(_) => { + self.cpu_error_count += 1; + } + } + } + + pub fn max(&self) -> Option { + if self.cpu_error_count < 5 { + Some(self.cpu) + } else { + None } } } -impl Temperatures { - pub fn update(&mut self) { - self.cpu = get_cpu_temp(); - } -} - -fn get_cpu_temp() -> f64 { - let mut f = File::open("/sys/class/thermal/thermal_zone0/temp").unwrap(); +fn cpu() -> Result> { + let mut f = File::open("/sys/class/thermal/thermal_zone0/temp")?; let mut buffer = String::new(); - f.read_to_string(&mut buffer).unwrap(); + f.read_to_string(&mut buffer)?; - buffer.trim().parse::().unwrap() / 1_000_f64 + Ok(buffer.trim().parse::()? / 1_000_f64) }