Skip to content

Commit

Permalink
fixed bug in pwm control
Browse files Browse the repository at this point in the history
  • Loading branch information
OliLay committed Oct 14, 2021
1 parent 0a7285e commit 091395e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
46 changes: 30 additions & 16 deletions src/control/pwm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use retry::{delay::Fixed, retry};
use sysfs_pwm::Pwm;

pub struct PwmControl {
Expand All @@ -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(_) => {}
Expand Down Expand Up @@ -95,6 +109,6 @@ impl PwmControl {

impl Drop for PwmControl {
fn drop(&mut self) {
self.pwm.enable(false).unwrap()
self.destroy().unwrap();
}
}

0 comments on commit 091395e

Please sign in to comment.