1//! Pulse Width Modulation (PWM) traits.
23#[cfg(feature = "defmt-03")]
4use crate::defmt;
56/// Error
7pub trait Error: core::fmt::Debug {
8/// Convert error to a generic error kind.
9 ///
10 /// By using this method, errors freely defined by HAL implementations
11 /// can be converted to a set of generic errors upon which generic
12 /// code can act.
13fn kind(&self) -> ErrorKind;
14}
1516impl Error for core::convert::Infallible {
17#[inline]
18fn kind(&self) -> ErrorKind {
19match *self {}
20 }
21}
2223/// Error kind.
24///
25/// This represents a common set of operation errors. HAL implementations are
26/// free to define more specific or additional error types. However, by providing
27/// a mapping to these common errors, generic code can still react to them.
28#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
29#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
30#[non_exhaustive]
31pub enum ErrorKind {
32/// A different error occurred. The original error may contain more information.
33Other,
34}
3536impl Error for ErrorKind {
37#[inline]
38fn kind(&self) -> ErrorKind {
39*self
40}
41}
4243impl core::fmt::Display for ErrorKind {
44#[inline]
45fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46match self {
47Self::Other => write!(
48 f,
49"A different error occurred. The original error may contain more information"
50),
51 }
52 }
53}
5455/// Error type trait.
56///
57/// This just defines the error type, to be used by the other traits.
58pub trait ErrorType {
59/// Error type
60type Error: Error;
61}
6263impl<T: ErrorType + ?Sized> ErrorType for &mut T {
64type Error = T::Error;
65}
6667/// Single PWM channel / pin.
68pub trait SetDutyCycle: ErrorType {
69/// Get the maximum duty cycle value.
70 ///
71 /// This value corresponds to a 100% duty cycle.
72fn max_duty_cycle(&self) -> u16;
7374/// Set the duty cycle to `duty / max_duty`.
75 ///
76 /// The caller is responsible for ensuring that the duty cycle value is less than or equal to the maximum duty cycle value,
77 /// as reported by [`max_duty_cycle`].
78 ///
79 /// [`max_duty_cycle`]: SetDutyCycle::max_duty_cycle
80fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error>;
8182/// Set the duty cycle to 0%, or always inactive.
83#[inline]
84fn set_duty_cycle_fully_off(&mut self) -> Result<(), Self::Error> {
85self.set_duty_cycle(0)
86 }
8788/// Set the duty cycle to 100%, or always active.
89#[inline]
90fn set_duty_cycle_fully_on(&mut self) -> Result<(), Self::Error> {
91self.set_duty_cycle(self.max_duty_cycle())
92 }
9394/// Set the duty cycle to `num / denom`.
95 ///
96 /// The caller is responsible for ensuring that `num` is less than or equal to `denom`,
97 /// and that `denom` is not zero.
98#[inline]
99fn set_duty_cycle_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
100debug_assert!(denom != 0);
101debug_assert!(num <= denom);
102let duty = u32::from(num) * u32::from(self.max_duty_cycle()) / u32::from(denom);
103104// This is safe because we know that `num <= denom`, so `duty <= self.max_duty_cycle()` (u16)
105#[allow(clippy::cast_possible_truncation)]
106{
107self.set_duty_cycle(duty as u16)
108 }
109 }
110111/// Set the duty cycle to `percent / 100`
112 ///
113 /// The caller is responsible for ensuring that `percent` is less than or equal to 100.
114#[inline]
115fn set_duty_cycle_percent(&mut self, percent: u8) -> Result<(), Self::Error> {
116self.set_duty_cycle_fraction(u16::from(percent), 100)
117 }
118}
119120impl<T: SetDutyCycle + ?Sized> SetDutyCycle for &mut T {
121#[inline]
122fn max_duty_cycle(&self) -> u16 {
123 T::max_duty_cycle(self)
124 }
125126#[inline]
127fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
128 T::set_duty_cycle(self, duty)
129 }
130131#[inline]
132fn set_duty_cycle_fully_off(&mut self) -> Result<(), Self::Error> {
133 T::set_duty_cycle_fully_off(self)
134 }
135136#[inline]
137fn set_duty_cycle_fully_on(&mut self) -> Result<(), Self::Error> {
138 T::set_duty_cycle_fully_on(self)
139 }
140141#[inline]
142fn set_duty_cycle_fraction(&mut self, num: u16, denom: u16) -> Result<(), Self::Error> {
143 T::set_duty_cycle_fraction(self, num, denom)
144 }
145146#[inline]
147fn set_duty_cycle_percent(&mut self, percent: u8) -> Result<(), Self::Error> {
148 T::set_duty_cycle_percent(self, percent)
149 }
150}