Update the error management of the temperatures mod
Try to remove the unwrap
This commit is contained in:
parent
bdb32de10d
commit
443a4e0c11
@ -1,5 +1,4 @@
|
|||||||
use crate::fan::Fan;
|
use crate::fan::Fan;
|
||||||
use crate::temperatures::Temperatures;
|
|
||||||
|
|
||||||
use rppal::gpio::{Gpio, OutputPin};
|
use rppal::gpio::{Gpio, OutputPin};
|
||||||
|
|
||||||
@ -25,13 +24,11 @@ impl Hysteresis {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Fan for Hysteresis {
|
impl Fan for Hysteresis {
|
||||||
fn update_temperature(&mut self, temperatures: &Temperatures) {
|
fn update_temperature(&mut self, temperature: f64) {
|
||||||
if temperatures.cpu > self.max_temperature {
|
if temperature > self.max_temperature {
|
||||||
self.pin.set_high();
|
self.pin.set_high();
|
||||||
} else {
|
} else if temperature < self.max_temperature - self.hysteresis {
|
||||||
if temperatures.cpu < self.max_temperature - self.hysteresis {
|
self.pin.set_low();
|
||||||
self.pin.set_low();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
pub mod hysteresis;
|
pub mod hysteresis;
|
||||||
pub mod pwm;
|
pub mod pwm;
|
||||||
|
|
||||||
use crate::temperatures::Temperatures;
|
|
||||||
|
|
||||||
pub trait Fan {
|
pub trait Fan {
|
||||||
fn update_temperature(&mut self, _: &Temperatures);
|
fn update_temperature(&mut self, temperature: f64);
|
||||||
fn percentage(&self) -> f64;
|
fn percentage(&self) -> f64;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use crate::fan::Fan;
|
use crate::fan::Fan;
|
||||||
use crate::temperatures::Temperatures;
|
|
||||||
|
|
||||||
use rppal::pwm::{Channel, Polarity, Pwm as Pwm_rpi};
|
use rppal::pwm::{Channel, Polarity, Pwm as Pwm_rpi};
|
||||||
|
|
||||||
@ -23,16 +22,16 @@ impl Pwm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Fan for Pwm {
|
impl Fan for Pwm {
|
||||||
fn update_temperature(&mut self, temperatures: &Temperatures) {
|
fn update_temperature(&mut self, temperature: f64) {
|
||||||
let percentage = (temperatures.cpu - self.min_temperature)
|
let percentage =
|
||||||
/ (self.max_temperature - self.min_temperature);
|
(temperature - self.min_temperature) / (self.max_temperature - self.min_temperature);
|
||||||
let percentage = if percentage > 1_f64 {
|
let percentage = if percentage > 1_f64 {
|
||||||
1_f64
|
1_f64
|
||||||
} else {
|
} else {
|
||||||
percentage
|
percentage
|
||||||
};
|
};
|
||||||
|
|
||||||
if temperatures.cpu > self.min_temperature {
|
if temperature > self.min_temperature {
|
||||||
self.pwm.enable().unwrap();
|
self.pwm.enable().unwrap();
|
||||||
self.pwm.set_duty_cycle(percentage).unwrap();
|
self.pwm.set_duty_cycle(percentage).unwrap();
|
||||||
} else {
|
} else {
|
||||||
|
26
src/main.rs
26
src/main.rs
@ -6,11 +6,12 @@ use fan::pwm::Pwm;
|
|||||||
use fan::Fan;
|
use fan::Fan;
|
||||||
|
|
||||||
use rppal::pwm::Channel;
|
use rppal::pwm::Channel;
|
||||||
|
use std::error::Error;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let mut temps = temperatures::Temperatures::default();
|
let mut temps = temperatures::Temperatures::new()?;
|
||||||
|
|
||||||
let mut fan: Box<dyn Fan> = if true {
|
let mut fan: Box<dyn Fan> = if true {
|
||||||
Box::new(Hysteresis::new(52.0, 5.0, 18))
|
Box::new(Hysteresis::new(52.0, 5.0, 18))
|
||||||
@ -20,12 +21,21 @@ fn main() {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
temps.update();
|
temps.update();
|
||||||
fan.update_temperature(&temps);
|
|
||||||
println!(
|
match temps.max() {
|
||||||
"CPU: {:.1}°C, fan is at {:.1}%",
|
Some(t) => {
|
||||||
temps.cpu,
|
fan.update_temperature(t);
|
||||||
fan.percentage() * 100_f64
|
|
||||||
);
|
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));
|
thread::sleep(Duration::from_secs(5));
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,45 @@
|
|||||||
|
use std::error::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
|
||||||
pub struct Temperatures {
|
pub struct Temperatures {
|
||||||
pub cpu: f64,
|
cpu: f64,
|
||||||
|
cpu_error_count: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Temperatures {
|
impl Temperatures {
|
||||||
fn default() -> Self {
|
pub fn new() -> Result<Self, Box<dyn Error>> {
|
||||||
Temperatures {
|
Ok(Temperatures {
|
||||||
cpu: get_cpu_temp(),
|
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<f64> {
|
||||||
|
if self.cpu_error_count < 5 {
|
||||||
|
Some(self.cpu)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Temperatures {
|
fn cpu() -> Result<f64, Box<dyn Error>> {
|
||||||
pub fn update(&mut self) {
|
let mut f = File::open("/sys/class/thermal/thermal_zone0/temp")?;
|
||||||
self.cpu = get_cpu_temp();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_cpu_temp() -> f64 {
|
|
||||||
let mut f = File::open("/sys/class/thermal/thermal_zone0/temp").unwrap();
|
|
||||||
let mut buffer = String::new();
|
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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user