use core::fmt::Display;
use core::ops::{Div, Mul};
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug, Default)]
pub struct Hertz(pub u32);
impl Display for Hertz {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{} Hz", self.0)
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for Hertz {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{=u32} Hz", self.0)
}
}
impl Hertz {
pub const fn hz(hertz: u32) -> Self {
Self(hertz)
}
pub const fn khz(kilohertz: u32) -> Self {
Self(kilohertz * 1_000)
}
pub const fn mhz(megahertz: u32) -> Self {
Self(megahertz * 1_000_000)
}
}
pub(crate) trait Prescaler {
fn num(&self) -> u32;
fn denom(&self) -> u32;
}
impl<T: Prescaler> Div<T> for Hertz {
type Output = Hertz;
fn div(self, rhs: T) -> Self::Output {
self * rhs.denom() / rhs.num()
}
}
impl<T: Prescaler> Mul<T> for Hertz {
type Output = Hertz;
fn mul(self, rhs: T) -> Self::Output {
self * rhs.num() / rhs.denom()
}
}
pub const fn hz(hertz: u32) -> Hertz {
Hertz::hz(hertz)
}
pub const fn khz(kilohertz: u32) -> Hertz {
Hertz::khz(kilohertz)
}
pub const fn mhz(megahertz: u32) -> Hertz {
Hertz::mhz(megahertz)
}
impl Mul<u32> for Hertz {
type Output = Hertz;
fn mul(self, rhs: u32) -> Self::Output {
Hertz(self.0 * rhs)
}
}
impl Div<u32> for Hertz {
type Output = Hertz;
fn div(self, rhs: u32) -> Self::Output {
Hertz(self.0 / rhs)
}
}
impl Mul<u16> for Hertz {
type Output = Hertz;
fn mul(self, rhs: u16) -> Self::Output {
self * (rhs as u32)
}
}
impl Div<u16> for Hertz {
type Output = Hertz;
fn div(self, rhs: u16) -> Self::Output {
self / (rhs as u32)
}
}
impl Mul<u8> for Hertz {
type Output = Hertz;
fn mul(self, rhs: u8) -> Self::Output {
self * (rhs as u32)
}
}
impl Div<u8> for Hertz {
type Output = Hertz;
fn div(self, rhs: u8) -> Self::Output {
self / (rhs as u32)
}
}
impl Div<Hertz> for Hertz {
type Output = u32;
fn div(self, rhs: Hertz) -> Self::Output {
self.0 / rhs.0
}
}
#[repr(C)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MaybeHertz(u32);
impl MaybeHertz {
pub fn to_hertz(self) -> Option<Hertz> {
self.into()
}
}
impl From<Option<Hertz>> for MaybeHertz {
fn from(value: Option<Hertz>) -> Self {
match value {
Some(Hertz(0)) => panic!("Hertz cannot be 0"),
Some(Hertz(val)) => Self(val),
None => Self(0),
}
}
}
impl From<MaybeHertz> for Option<Hertz> {
fn from(value: MaybeHertz) -> Self {
match value {
MaybeHertz(0) => None,
MaybeHertz(val) => Some(Hertz(val)),
}
}
}