Use pwm_min, pwm_max for converting speed to pwm

Don't hardcode 0 and 255 anymore. Instead use pwm_min and pwm_max as
boundaries when converting from a speed (0 to 100) to a pwm value.
0 gets mapped to pwm_min, 100 gets mapped to pwm_max everything inbetween
gets linearly interpoltated.
This commit is contained in:
Stefan Gehr 2021-07-20 09:53:22 +02:00
parent 90e212a158
commit 6b17d3815f
2 changed files with 10 additions and 12 deletions

View File

@ -119,12 +119,6 @@ impl From<LogLevel> for LevelFilter {
} }
} }
// linear mapping between two points
fn linear_map(x: f64, x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
let m = (y2 - y1) / (x2 - x1);
m * (x - x1) + y1
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Config { pub struct Config {
log_level: LogLevel, log_level: LogLevel,
@ -147,7 +141,7 @@ impl Config {
return self.max_speed(); return self.max_speed();
} }
linear_map(temp, self.speed_matrix[idx].temp, self.speed_matrix[idx].speed, self.speed_matrix[idx+1].temp, self.speed_matrix[idx+1].speed) crate::linear_map(temp, self.speed_matrix[idx].temp, self.speed_matrix[idx+1].temp, self.speed_matrix[idx].speed, self.speed_matrix[idx+1].speed)
} }
pub fn log_level(&self) -> LogLevel { pub fn log_level(&self) -> LogLevel {

View File

@ -3,7 +3,6 @@ mod config;
extern crate log; extern crate log;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::cmp::{min, max};
use crate::config::{load_config, Card, Config}; use crate::config::{load_config, Card, Config};
use gumdrop::Options; use gumdrop::Options;
@ -22,6 +21,12 @@ pub enum AmdFanError {
FailedReadVendor, FailedReadVendor,
} }
// linear mapping from the xrange to the yrange
fn linear_map(x: f64, x1: f64, x2: f64, y1: f64, y2: f64) -> f64 {
let m = (y2 - y1) / (x2 - x1);
m * (x - x1) + y1
}
#[derive(Debug)] #[derive(Debug)]
pub struct HwMon { pub struct HwMon {
card: Card, card: Card,
@ -124,10 +129,9 @@ impl HwMon {
} }
pub fn set_speed(&mut self, speed: f64) -> std::io::Result<()> { pub fn set_speed(&mut self, speed: f64) -> std::io::Result<()> {
let mut pwm = (speed / 100f64 * 255f64).round() as u32; let min = self.pwm_min() as f64;
// stay in the range let max = self.pwm_max() as f64;
pwm = max(pwm, self.pwm_min()); let pwm = linear_map(speed, 0f64, 100f64, min, max).round() as u32;
pwm = min(pwm, self.pwm_max());
self.set_pwm(pwm) self.set_pwm(pwm)
} }