use super::*;
use crate::units::{Frequency, Pwm, PwmEnable, PwmMode, Raw};
use std::path::Path;
pub trait PwmSensor: SyncSensor + std::fmt::Debug {
fn read_pwm(&self) -> Result<Pwm> {
let raw = self.read_raw(SensorSubFunctionType::Pwm)?;
Pwm::from_raw(&raw).map_err(Error::from)
}
fn read_enable(&self) -> Result<PwmEnable> {
let raw = self.read_raw(SensorSubFunctionType::Enable)?;
PwmEnable::from_raw(&raw).map_err(Error::from)
}
fn read_mode(&self) -> Result<PwmMode> {
let raw = self.read_raw(SensorSubFunctionType::Mode)?;
PwmMode::from_raw(&raw).map_err(Error::from)
}
fn read_frequency(&self) -> Result<Frequency> {
let raw = self.read_raw(SensorSubFunctionType::Freq)?;
Frequency::from_raw(&raw).map_err(Error::from)
}
}
#[derive(Debug, Clone)]
pub(crate) struct PwmSensorStruct {
hwmon_path: PathBuf,
index: u16,
}
impl Sensor for PwmSensorStruct {
fn static_base() -> &'static str where Self: Sized {
"pwm"
}
fn base(&self) -> &'static str {
"pwm"
}
fn index(&self) -> u16 {
self.index
}
fn hwmon_path(&self) -> &Path {
self.hwmon_path.as_path()
}
}
impl SyncSensor for PwmSensorStruct {}
impl SyncSensorExt for PwmSensorStruct {
fn parse(hwmon: &Hwmon, index: u16) -> Result<Self> {
let sensor = Self {
hwmon_path: hwmon.path().to_path_buf(),
index,
};
inspect_sensor(sensor, SensorSubFunctionType::Pwm)
}
}
impl PwmSensor for PwmSensorStruct {}
#[cfg(feature = "writeable")]
impl WriteableSensor for PwmSensorStruct {}
#[cfg(feature = "writeable")]
pub trait WriteablePwmSensor: PwmSensor + WriteableSensor {
fn write_pwm(&self, pwm: Pwm) -> Result<()> {
self.write_raw(SensorSubFunctionType::Pwm, &pwm.to_raw())
}
fn write_enable(&self, enable: PwmEnable) -> Result<()> {
self.write_raw(SensorSubFunctionType::Enable, &enable.to_raw())
}
fn write_mode(&self, mode: PwmMode) -> Result<()> {
self.write_raw(SensorSubFunctionType::Mode, &mode.to_raw())
}
fn write_frequency(&self, freq: Frequency) -> Result<()> {
self.write_raw(SensorSubFunctionType::Freq, &freq.to_raw())
}
}
#[cfg(feature = "writeable")]
impl WriteablePwmSensor for PwmSensorStruct {}