Update the error management of the temperatures mod

Try to remove the unwrap
This commit is contained in:
Zykino 2020-03-14 22:10:40 +01:00
parent bdb32de10d
commit 443a4e0c11
5 changed files with 59 additions and 38 deletions

View File

@ -1,5 +1,4 @@
use crate::fan::Fan;
use crate::temperatures::Temperatures;
use rppal::gpio::{Gpio, OutputPin};
@ -25,15 +24,13 @@ 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 {
} else if temperature < self.max_temperature - self.hysteresis {
self.pin.set_low();
}
}
}
fn percentage(&self) -> f64 {
if self.pin.is_set_high() {

View File

@ -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;
}

View File

@ -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 {

View File

@ -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<dyn Error>> {
let mut temps = temperatures::Temperatures::new()?;
let mut fan: Box<dyn Fan> = if true {
Box::new(Hysteresis::new(52.0, 5.0, 18))
@ -20,12 +21,21 @@ fn main() {
loop {
temps.update();
fan.update_temperature(&temps);
match temps.max() {
Some(t) => {
fan.update_temperature(t);
println!(
"CPU: {:.1}°C, fan is at {:.1}%",
temps.cpu,
t,
fan.percentage() * 100_f64
);
}
None => {
eprintln!("Recuring problem while getting the temperature");
}
}
thread::sleep(Duration::from_secs(5));
}

View File

@ -1,28 +1,45 @@
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
pub struct Temperatures {
pub cpu: f64,
}
impl Default for Temperatures {
fn default() -> Self {
Temperatures {
cpu: get_cpu_temp(),
}
}
cpu: f64,
cpu_error_count: i32,
}
impl Temperatures {
pub fn new() -> Result<Self, Box<dyn Error>> {
Ok(Temperatures {
cpu: cpu()?,
cpu_error_count: 0,
})
}
pub fn update(&mut self) {
self.cpu = get_cpu_temp();
match cpu() {
Ok(t) => {
self.cpu = t;
self.cpu_error_count = 0;
}
Err(_) => {
self.cpu_error_count += 1;
}
}
}
pub fn max(&self) -> Option<f64> {
if self.cpu_error_count < 5 {
Some(self.cpu)
} else {
None
}
}
}
fn get_cpu_temp() -> f64 {
let mut f = File::open("/sys/class/thermal/thermal_zone0/temp").unwrap();
fn cpu() -> Result<f64, Box<dyn Error>> {
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::<f64>().unwrap() / 1_000_f64
Ok(buffer.trim().parse::<f64>()? / 1_000_f64)
}