@@ -75,13 +75,22 @@ pub enum Error {
75
75
/// doesn't provide any of the common user-accessible system files
76
76
/// that are used to identify the model and SoC.
77
77
UnknownModel ,
78
+ /// Invalid channel.
79
+ ///
80
+ /// The selected PWM channel is not available on this device. You may
81
+ /// encounter this error if the Raspberry Pi model only has a limited
82
+ /// number of channels, the selected channel hasn't been properly
83
+ /// configured in `/boot/firmware/config.txt`, or the channel isn't
84
+ /// supported by RPPAL.
85
+ InvalidChannel ,
78
86
}
79
87
80
88
impl fmt:: Display for Error {
81
89
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
82
90
match * self {
83
91
Error :: Io ( ref err) => write ! ( f, "I/O error: {}" , err) ,
84
92
Error :: UnknownModel => write ! ( f, "Unknown Raspberry Pi model" ) ,
93
+ Error :: InvalidChannel => write ! ( f, "Invalid PWM channel" ) ,
85
94
}
86
95
}
87
96
}
@@ -118,6 +127,18 @@ impl fmt::Display for Channel {
118
127
}
119
128
}
120
129
130
+ impl TryFrom < u8 > for Channel {
131
+ type Error = Error ;
132
+
133
+ fn try_from ( value : u8 ) -> result:: Result < Self , Self :: Error > {
134
+ match value {
135
+ 0 => Ok ( Channel :: Pwm0 ) ,
136
+ 1 => Ok ( Channel :: Pwm1 ) ,
137
+ _ => Err ( Error :: InvalidChannel ) ,
138
+ }
139
+ }
140
+ }
141
+
121
142
/// Output polarities.
122
143
#[ derive( Debug , PartialEq , Eq , Copy , Clone ) ]
123
144
pub enum Polarity {
0 commit comments