Add configuration options

Needing the yaml pull request to be merged in confy before switching to the official crate.
This commit is contained in:
Zykino
2020-03-27 18:45:39 +01:00
parent 443a4e0c11
commit 3686da12ae
6 changed files with 300 additions and 27 deletions

36
src/config.rs Normal file
View File

@ -0,0 +1,36 @@
use crate::fan::hysteresis::HysteresisConfig;
use crate::fan::pwm::PwmConfig;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub check_interval: Duration,
pub mode: FanMode,
pub verbosity_mode: VerbosityMode,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum FanMode {
Hysteresis(HysteresisConfig),
Pwm(PwmConfig),
}
#[derive(Debug, Serialize, Deserialize)]
pub enum VerbosityMode {
Human,
Machine,
Quiet,
}
impl Default for Config {
fn default() -> Self {
Config {
check_interval: Duration::from_secs(10),
mode: FanMode::Hysteresis(HysteresisConfig::default()),
//mode: FanMode::Pwm(PwmConfig::default()),
verbosity_mode: VerbosityMode::Human,
}
}
}

View File

@ -1,23 +1,41 @@
use crate::fan::Fan;
use rppal::gpio::{Gpio, OutputPin};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct HysteresisConfig {
max_temperature: f64,
hysteresis: f64,
bcm_pin: u8,
}
impl Default for HysteresisConfig {
fn default() -> Self {
HysteresisConfig {
max_temperature: 70_f64,
hysteresis: 10_f64,
bcm_pin: 18,
}
}
}
pub struct Hysteresis {
pub max_temperature: f64,
pub hysteresis: f64,
max_temperature: f64,
hysteresis: f64,
pin: OutputPin,
}
impl Hysteresis {
pub fn new(max_temperature: f64, hysteresis: f64, bcm_pin: u8) -> Hysteresis {
pub fn new(config: HysteresisConfig) -> Self {
let gpio = Gpio::new().unwrap();
let mut pin = gpio.get(bcm_pin).unwrap().into_output();
let mut pin = gpio.get(config.bcm_pin).unwrap().into_output();
pin.set_low();
Hysteresis {
max_temperature,
hysteresis,
max_temperature: config.max_temperature,
hysteresis: config.hysteresis,
pin,
}
}

View File

@ -1,21 +1,56 @@
use crate::fan::Fan;
use rppal::pwm::{Channel, Polarity, Pwm as Pwm_rpi};
use serde::{Deserialize, Serialize};
// See: https://serde/rs/remote-derive.html
#[derive(Serialize, Deserialize)]
#[serde(remote = "Channel")]
pub enum ChannelDef {
Pwm0,
Pwm1,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PwmConfig {
max_temperature: f64,
min_temperature: f64,
frequency: f64,
#[serde(with = "ChannelDef")]
channel: Channel,
}
impl Default for PwmConfig {
fn default() -> Self {
PwmConfig {
max_temperature: 70_f64,
min_temperature: 60_f64,
frequency: 0.1_f64,
channel: Channel::Pwm0,
}
}
}
pub struct Pwm {
pub max_temperature: f64,
pub min_temperature: f64,
max_temperature: f64,
min_temperature: f64,
pwm: Pwm_rpi,
}
impl Pwm {
pub fn new(max_temperature: f64, min_temperature: f64, bcm_pin: Channel) -> Pwm {
let pwm =
Pwm_rpi::with_frequency(bcm_pin, 0.1_f64, 0_f64, Polarity::Normal, false).unwrap();
pub fn new(config: PwmConfig) -> Self {
let pwm = Pwm_rpi::with_frequency(
config.channel,
config.frequency,
0_f64,
Polarity::Normal,
false,
)
.unwrap();
Pwm {
max_temperature,
min_temperature,
max_temperature: config.max_temperature,
min_temperature: config.min_temperature,
pwm,
}
}

View File

@ -1,22 +1,26 @@
mod config;
mod fan;
mod temperatures;
use config::{Config, FanMode::*, VerbosityMode::*};
use fan::hysteresis::Hysteresis;
use fan::pwm::Pwm;
use fan::Fan;
use temperatures::Temperatures;
use rppal::pwm::Channel;
use std::error::Error;
use std::thread;
use std::time::Duration;
fn main() -> Result<(), Box<dyn Error>> {
let mut temps = temperatures::Temperatures::new()?;
let mut temps = Temperatures::new()?;
let config: Config = confy::load("tempi").unwrap();
let mut fan: Box<dyn Fan> = if true {
Box::new(Hysteresis::new(52.0, 5.0, 18))
} else {
Box::new(Pwm::new(52.0, 42.0, Channel::Pwm0))
// Can be used to check configuration and/or save default one.
//confy::store("tempi", dbg!(&config)).unwrap();
let mut fan: Box<dyn Fan> = match config.mode {
Hysteresis(h) => Box::new(Hysteresis::new(h)),
Pwm(p) => Box::new(Pwm::new(p)),
};
loop {
@ -26,17 +30,21 @@ fn main() -> Result<(), Box<dyn Error>> {
Some(t) => {
fan.update_temperature(t);
println!(
"CPU: {:.1}°C, fan is at {:.1}%",
t,
fan.percentage() * 100_f64
);
match config.verbosity_mode {
Human => println!(
"CPU: {:.1}°C, fan is at {:.1}%",
t,
fan.percentage() * 100_f64
),
Machine => println!("{} | {}", t, fan.percentage()),
Quiet => (),
}
}
None => {
eprintln!("Recuring problem while getting the temperature");
}
}
thread::sleep(Duration::from_secs(5));
thread::sleep(config.check_interval);
}
}