@@ -59,19 +59,66 @@ impl<T: ErrorType> ErrorType for &mut T {
59
59
60
60
/// Single PWM channel / pin
61
61
pub trait SetDuty : ErrorType {
62
- /// Set the duty cycle.
62
+ /// Get the maximum duty cycle value .
63
63
///
64
- /// `duty` is the duty cycle. Valid values span the entire `u16` range:
64
+ /// This value corresponds to a 100% duty cycle.
65
+ fn get_max_duty ( & self ) -> u16 ;
66
+
67
+ /// Set the duty cycle to `duty / max_duty`.
68
+ ///
69
+ /// The caller is responsible for ensuring that the duty cycle value is less than or equal to the maximum duty cycle value,
70
+ /// as reported by `get_max_duty`.
71
+ fn set_duty ( & mut self , duty : u16 ) -> Result < ( ) , Self :: Error > ;
72
+
73
+ /// Set the duty cycle to 0%, or always inactive.
74
+ #[ inline]
75
+ fn set_off ( & mut self ) -> Result < ( ) , Self :: Error > {
76
+ self . set_duty ( 0 )
77
+ }
78
+
79
+ /// Set the duty cycle to 100%, or always active.
80
+ #[ inline]
81
+ fn set_on ( & mut self ) -> Result < ( ) , Self :: Error > {
82
+ self . set_duty ( self . get_max_duty ( ) )
83
+ }
84
+
85
+ /// Set the duty cycle to `num / denom`.
65
86
///
66
- /// - `duty = 0` is considered 0% duty, which makes the pin permanently low.
67
- /// - `duty = u16::MAX` is considered 100% duty, which makes the pin permanently high.
87
+ /// The caller is responsible for ensuring that `num` is less than or equal to `denom`,
88
+ /// and that `denom` is not zero.
89
+ #[ inline]
90
+ fn set_fraction ( & mut self , num : u16 , denom : u16 ) -> Result < ( ) , Self :: Error > {
91
+ let duty = num as u32 * self . get_max_duty ( ) as u32 / denom as u32 ;
92
+ self . set_duty ( duty as u16 )
93
+ }
94
+
95
+ /// Set the duty cycle to `percent / 100`
68
96
///
69
- /// Implementations must scale the duty value linearly to the range required by the hardware.
70
- fn set_duty ( & mut self , duty : u16 ) -> Self :: Error ;
97
+ /// The caller is responsible for ensuring that `percent` is less than or equal to 100.
98
+ #[ inline]
99
+ fn set_percent ( & mut self , percent : u8 ) -> Result < ( ) , Self :: Error > {
100
+ self . set_fraction ( percent as u16 , 100 )
101
+ }
71
102
}
72
103
73
104
impl < T : SetDuty > SetDuty for & mut T {
74
- fn set_duty ( & mut self , duty : u16 ) -> Self :: Error {
105
+ fn get_max_duty ( & self ) -> u16 {
106
+ T :: get_max_duty ( self )
107
+ }
108
+ fn set_duty ( & mut self , duty : u16 ) -> Result < ( ) , Self :: Error > {
75
109
T :: set_duty ( self , duty)
76
110
}
111
+
112
+ fn set_off ( & mut self ) -> Result < ( ) , Self :: Error > {
113
+ T :: set_off ( self )
114
+ }
115
+ fn set_on ( & mut self ) -> Result < ( ) , Self :: Error > {
116
+ T :: set_on ( self )
117
+ }
118
+ fn set_fraction ( & mut self , num : u16 , denom : u16 ) -> Result < ( ) , Self :: Error > {
119
+ T :: set_fraction ( self , num, denom)
120
+ }
121
+ fn set_percent ( & mut self , percent : u8 ) -> Result < ( ) , Self :: Error > {
122
+ T :: set_percent ( self , percent)
123
+ }
77
124
}
0 commit comments