use super::*;
use crate::units::{Frequency, Pwm, PwmEnable, PwmMode, Raw};
use std::path::Path;
#[async_trait]
pub trait AsyncPwmSensor: AsyncSensor + std::fmt::Debug {
async fn read_pwm(&self) -> Result<Pwm> {
let raw = self.read_raw(SensorSubFunctionType::Pwm).await?;
Pwm::from_raw(&raw).map_err(Error::from)
}
async fn read_enable(&self) -> Result<PwmEnable> {
let raw = self.read_raw(SensorSubFunctionType::Enable).await?;
PwmEnable::from_raw(&raw).map_err(Error::from)
}
async fn read_mode(&self) -> Result<PwmMode> {
let raw = self.read_raw(SensorSubFunctionType::Mode).await?;
PwmMode::from_raw(&raw).map_err(Error::from)
}
async fn read_frequency(&self) -> Result<Frequency> {
let raw = self.read_raw(SensorSubFunctionType::Freq).await?;
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 AsyncSensor for PwmSensorStruct {}
#[async_trait]
impl AsyncSensorExt for PwmSensorStruct {
async fn parse(hwmon: &Hwmon, index: u16) -> Result<Self> {
let sensor = Self {
hwmon_path: hwmon.path().to_path_buf(),
index,
};
inspect_sensor(sensor, SensorSubFunctionType::Pwm).await
}
}
impl AsyncPwmSensor for PwmSensorStruct {}
#[cfg(feature = "writeable")]
impl AsyncWriteableSensor for PwmSensorStruct {}
#[cfg(feature = "writeable")]
#[async_trait]
pub trait AsyncWriteablePwmSensor: AsyncPwmSensor + AsyncWriteableSensor {
async fn write_pwm(&self, pwm: Pwm) -> Result<()> {
self.write_raw(SensorSubFunctionType::Pwm, &pwm.to_raw())
.await
}
async fn write_enable(&self, enable: PwmEnable) -> Result<()> {
self.write_raw(SensorSubFunctionType::Enable, &enable.to_raw())
.await
}
async fn write_mode(&self, mode: PwmMode) -> Result<()> {
self.write_raw(SensorSubFunctionType::Mode, &mode.to_raw())
.await
}
async fn write_frequency(&self, freq: Frequency) -> Result<()> {
self.write_raw(SensorSubFunctionType::Freq, &freq.to_raw())
.await
}
}
#[cfg(feature = "writeable")]
impl AsyncWriteablePwmSensor for PwmSensorStruct {}