From 091395e47b36a86d4d5c7937a5afd255fe48f5df Mon Sep 17 00:00:00 2001 From: Oliver Layer Date: Thu, 14 Oct 2021 12:24:11 +0200 Subject: [PATCH] fixed bug in pwm control --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/control/pwm.rs | 46 ++++++++++++++++++++++++++++++---------------- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 97338d2..684f121 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -715,7 +715,7 @@ checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "smarst-fan" -version = "0.9.0" +version = "0.9.1" dependencies = [ "config", "glob", diff --git a/Cargo.toml b/Cargo.toml index 06761ad..714b618 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "smarst-fan" description = "Linux service for controlling a PWM fan" -version = "0.9.0" +version = "0.9.1" edition = "2018" authors = ["Oliver Layer"] readme = "README.md" diff --git a/src/control/pwm.rs b/src/control/pwm.rs index 99959d3..512583f 100644 --- a/src/control/pwm.rs +++ b/src/control/pwm.rs @@ -1,3 +1,4 @@ +use retry::{delay::Fixed, retry}; use sysfs_pwm::Pwm; pub struct PwmControl { @@ -24,28 +25,41 @@ impl PwmControl { let fan_control = PwmControl { pwm: pwm }; - fan_control.enable()?; - fan_control.set_frequency(25000.0)?; - fan_control.set_throttle(initial_throttle_percentage)?; - - Ok(fan_control) + fan_control.export()?; + + // retry is needed here, as exporting takes a while, + // and the method above is not blocking. + match retry(Fixed::from_millis(300), || { + fan_control.set_frequency(25000.0)?; + fan_control.set_throttle(initial_throttle_percentage)?; + fan_control.enable()?; + + Ok(()) + }) { + Err(retry::Error::Operation { + error, + total_delay: _, + tries: _, + }) => Err(error), + _ => Ok(fan_control), + } } - pub fn enable(&self) -> Result<(), String> { + fn export(&self) -> Result<(), String> { match self.pwm.export() { - Err(err) => return Err(format!("Could not export PWM. {}", err)), - Ok(_) => {} - }; + Err(err) => Err(format!("Could not export PWM. {}", err)), + Ok(_) => Ok(()), + } + } + pub fn enable(&self) -> Result<(), String> { match self.pwm.enable(true) { - Err(err) => return Err(format!("Could not enable PWM. {}", err)), - Ok(_) => {} - }; - - Ok(()) + Err(err) => Err(format!("Could not enable PWM. {}", err)), + Ok(_) => Ok(()), + } } - pub fn disable(&self) -> Result<(), String> { + pub fn destroy(&self) -> Result<(), String> { match self.pwm.enable(false) { Err(err) => return Err(format!("Could not disable PWM. {}", err)), Ok(_) => {} @@ -95,6 +109,6 @@ impl PwmControl { impl Drop for PwmControl { fn drop(&mut self) { - self.pwm.enable(false).unwrap() + self.destroy().unwrap(); } }