From 35532b1eb7a17c951b63aad75d6780dd7dc88bf9 Mon Sep 17 00:00:00 2001 From: Stefan Gehr Date: Mon, 2 Aug 2021 00:02:03 +0200 Subject: [PATCH] Simplifications and fixes for edge cases monitor.rs: Removed hardcoded pwm-range of 0 to 255 and instead correctly interpolate between pwm_min() and pwm_max() main.rs: Simpler comparison to check whether temperature changed since last time hw_mon.rs: Removed unnecessary conversions between u32 and u64 --- src/hw_mon.rs | 6 +++--- src/main.rs | 2 +- src/monitor.rs | 16 ++++++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/hw_mon.rs b/src/hw_mon.rs index 39cfab5..932c5ea 100644 --- a/src/hw_mon.rs +++ b/src/hw_mon.rs @@ -121,17 +121,17 @@ impl HwMon { self.write("pwm1_enable", 2) } - pub fn set_pwm(&self, value: u32) -> std::io::Result<()> { + pub fn set_pwm(&self, value: u64) -> std::io::Result<()> { if self.is_fan_automatic() { self.set_manual()?; } - self.write("pwm1", value as u64) + self.write("pwm1", value) } pub fn set_speed(&mut self, speed: f64) -> std::io::Result<()> { let min = self.pwm_min() as f64; let max = self.pwm_max() as f64; - let pwm = linear_map(speed, 0f64, 100f64, min, max).round() as u32; + let pwm = linear_map(speed, 0f64, 100f64, min, max).round() as u64; self.set_pwm(pwm) } diff --git a/src/main.rs b/src/main.rs index 8a6fcff..a5d2479 100644 --- a/src/main.rs +++ b/src/main.rs @@ -152,7 +152,7 @@ fn service(config: Config) -> std::io::Result<()> { log::debug!("Current {} temperature: {}", hw_mon.card, gpu_temp); let last = *cache.entry(*hw_mon.card).or_insert(1_000f64); - if ((last - 0.001f64)..(last + 0.001f64)).contains(&gpu_temp) { + if (last - gpu_temp).abs() < 0.001f64 { log::debug!("Temperature didn't change"); continue; }; diff --git a/src/monitor.rs b/src/monitor.rs index c5a40c3..24703fb 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -48,14 +48,16 @@ pub fn verbose(config: Config) -> std::io::Result<()> { for hw_mon in controllers.iter_mut() { println!("Card {:3}", hw_mon.card.to_string().replace("card", "")); println!(" MIN | MAX | PWM | %"); + let min = hw_mon.pwm_min(); + let max = hw_mon.pwm_max(); println!( " {:>4} | {:>4} | {:>6} | {:>3}", - hw_mon.pwm_min(), - hw_mon.pwm_max(), + min, + max, hw_mon .pwm() .map_or_else(|_e| String::from("FAILED"), |f| f.to_string()), - (hw_mon.pwm().unwrap_or_default() as f32 / 2.55).round(), + (crate::linear_map(hw_mon.pwm().unwrap_or_default() as f64, min as f64, max as f64, 0f64, 100f64)).round(), ); println!(); @@ -83,13 +85,15 @@ pub fn short(config: Config) -> std::io::Result<()> { "Card {:3} | Temp | MIN | MAX | PWM | %", hw_mon.card.to_string().replace("card", "") ); + let min = hw_mon.pwm_min(); + let max = hw_mon.pwm_max(); println!( " | {:>5.2} | {:>4} | {:>4} | {:>4} | {:>3}", hw_mon.max_gpu_temp().unwrap_or_default(), - hw_mon.pwm_min(), - hw_mon.pwm_max(), + min, + max, hw_mon.pwm().unwrap_or_default(), - (hw_mon.pwm().unwrap_or_default() as f32 / 2.55).round(), + crate::linear_map(hw_mon.pwm().unwrap_or_default() as f64, min as f64, max as f64, 0f64, 100f64).round(), ); } std::thread::sleep(std::time::Duration::from_secs(4));