use super::*;
use crate::units::{Frequency, Pwm, PwmEnable, PwmMode, Raw};
use crate::{Parseable, ParsingResult};
#[cfg(feature = "writable")]
use std::convert::TryFrom;
use std::path::Path;
pub trait PwmSensor: SensorBase {
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)
}
#[cfg(feature = "writable")]
fn write_pwm(&self, pwm: Pwm) -> Result<()>
where
Self: WritableSensorBase,
{
self.write_raw(SensorSubFunctionType::Pwm, &pwm.to_raw())
}
#[cfg(feature = "writable")]
fn write_enable(&self, enable: PwmEnable) -> Result<()>
where
Self: WritableSensorBase,
{
self.write_raw(SensorSubFunctionType::Enable, &enable.to_raw())
}
#[cfg(feature = "writable")]
fn write_mode(&self, mode: PwmMode) -> Result<()>
where
Self: WritableSensorBase,
{
self.write_raw(SensorSubFunctionType::Mode, &mode.to_raw())
}
#[cfg(feature = "writable")]
fn write_frequency(&self, freq: Frequency) -> Result<()>
where
Self: WritableSensorBase,
{
self.write_raw(SensorSubFunctionType::Freq, &freq.to_raw())
}
}
#[derive(Debug, Clone)]
pub struct ReadOnlyPwm {
hwmon_path: PathBuf,
index: u16,
}
impl SensorBase for ReadOnlyPwm {
fn base(&self) -> &'static str {
"pwm"
}
fn index(&self) -> u16 {
self.index
}
fn hwmon_path(&self) -> &Path {
self.hwmon_path.as_path()
}
}
impl Parseable for ReadOnlyPwm {
type Parent = ReadOnlyHwmon;
fn parse(parent: &Self::Parent, index: u16) -> ParsingResult<Self> {
let pwm = Self {
hwmon_path: parent.path().to_path_buf(),
index,
};
inspect_sensor(pwm)
}
}
impl PwmSensor for ReadOnlyPwm {}
#[cfg(feature = "writable")]
impl From<ReadWritePwm> for ReadOnlyPwm {
fn from(write_pwm: ReadWritePwm) -> ReadOnlyPwm {
ReadOnlyPwm {
hwmon_path: write_pwm.hwmon_path,
index: write_pwm.index,
}
}
}
#[cfg(feature = "writable")]
#[derive(Debug, Clone)]
pub struct ReadWritePwm {
hwmon_path: PathBuf,
index: u16,
}
#[cfg(feature = "writable")]
impl SensorBase for ReadWritePwm {
fn base(&self) -> &'static str {
"pwm"
}
fn index(&self) -> u16 {
self.index
}
fn hwmon_path(&self) -> &Path {
self.hwmon_path.as_path()
}
}
#[cfg(feature = "writable")]
impl Parseable for ReadWritePwm {
type Parent = ReadWriteHwmon;
fn parse(parent: &Self::Parent, index: u16) -> ParsingResult<Self> {
let pwm = Self {
hwmon_path: parent.path().to_path_buf(),
index,
};
inspect_sensor(pwm)
}
}
#[cfg(feature = "writable")]
impl PwmSensor for ReadWritePwm {}
#[cfg(feature = "writable")]
impl WritableSensorBase for ReadWritePwm {}
#[cfg(feature = "writable")]
impl TryFrom<ReadOnlyPwm> for ReadWritePwm {
type Error = Error;
fn try_from(value: ReadOnlyPwm) -> std::result::Result<Self, Self::Error> {
let read_write = ReadWritePwm {
hwmon_path: value.hwmon_path,
index: value.index,
};
if read_write.supported_write_sub_functions().is_empty() {
return Err(Error::InsufficientRights {
path: read_write.hwmon_path.join(format!(
"{}{}",
read_write.base(),
read_write.index(),
)),
});
}
Ok(read_write)
}
}