1use core::ops::{Div, Mul};
8
9#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug)]
11#[cfg_attr(feature = "defmt", derive(defmt::Format))]
12pub struct Hertz(pub u32);
13
14impl Hertz {
15 pub const fn hz(hertz: u32) -> Self {
17 Self(hertz)
18 }
19
20 pub const fn khz(kilohertz: u32) -> Self {
22 Self(kilohertz * 1_000)
23 }
24
25 pub const fn mhz(megahertz: u32) -> Self {
27 Self(megahertz * 1_000_000)
28 }
29}
30
31pub const fn hz(hertz: u32) -> Hertz {
33 Hertz::hz(hertz)
34}
35
36pub const fn khz(kilohertz: u32) -> Hertz {
38 Hertz::khz(kilohertz)
39}
40
41pub const fn mhz(megahertz: u32) -> Hertz {
43 Hertz::mhz(megahertz)
44}
45
46impl Mul<u32> for Hertz {
47 type Output = Hertz;
48 fn mul(self, rhs: u32) -> Self::Output {
49 Hertz(self.0 * rhs)
50 }
51}
52
53impl Div<u32> for Hertz {
54 type Output = Hertz;
55 fn div(self, rhs: u32) -> Self::Output {
56 Hertz(self.0 / rhs)
57 }
58}
59
60impl Mul<u16> for Hertz {
61 type Output = Hertz;
62 fn mul(self, rhs: u16) -> Self::Output {
63 self * (rhs as u32)
64 }
65}
66
67impl Div<u16> for Hertz {
68 type Output = Hertz;
69 fn div(self, rhs: u16) -> Self::Output {
70 self / (rhs as u32)
71 }
72}
73
74impl Mul<u8> for Hertz {
75 type Output = Hertz;
76 fn mul(self, rhs: u8) -> Self::Output {
77 self * (rhs as u32)
78 }
79}
80
81impl Div<u8> for Hertz {
82 type Output = Hertz;
83 fn div(self, rhs: u8) -> Self::Output {
84 self / (rhs as u32)
85 }
86}
87
88impl Div<Hertz> for Hertz {
89 type Output = u32;
90 fn div(self, rhs: Hertz) -> Self::Output {
91 self.0 / rhs.0
92 }
93}
94
95#[repr(C)]
96#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug, Default)]
97#[cfg_attr(feature = "defmt", derive(defmt::Format))]
98pub struct MaybeHertz(u32);
104
105impl MaybeHertz {
106 pub fn to_hertz(self) -> Option<Hertz> {
108 self.into()
109 }
110}
111
112impl From<Option<Hertz>> for MaybeHertz {
113 fn from(value: Option<Hertz>) -> Self {
114 match value {
115 Some(Hertz(0)) => panic!("Hertz cannot be 0"),
116 Some(Hertz(val)) => Self(val),
117 None => Self(0),
118 }
119 }
120}
121
122impl From<MaybeHertz> for Option<Hertz> {
123 fn from(value: MaybeHertz) -> Self {
124 match value {
125 MaybeHertz(0) => None,
126 MaybeHertz(val) => Some(Hertz(val)),
127 }
128 }
129}