Skip to content

Commit b48c17a

Browse files
committed
Add TryFrom<u8> implementation for Channel
Fixes #168
1 parent 0219525 commit b48c17a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Diff for: CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Changelog
22

3-
## 0.21.0 (TBD)
3+
## 0.21.0 (December 10, 2024)
44

55
* Add support for Raspberry Pi Compute Module 5 Lite.
66
* Add support for Raspberry Pi 500.
7+
* **Pwm**: (Breaking change) Add `InvalidChannel` error.
8+
* **Pwm**: Add `TryFrom<u8>` implementation for `Channel`.
79

810
## 0.20.0 (November 30, 2024)
911

Diff for: src/pwm.rs

+21
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,22 @@ pub enum Error {
7575
/// doesn't provide any of the common user-accessible system files
7676
/// that are used to identify the model and SoC.
7777
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,
7886
}
7987

8088
impl fmt::Display for Error {
8189
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8290
match *self {
8391
Error::Io(ref err) => write!(f, "I/O error: {}", err),
8492
Error::UnknownModel => write!(f, "Unknown Raspberry Pi model"),
93+
Error::InvalidChannel => write!(f, "Invalid PWM channel"),
8594
}
8695
}
8796
}
@@ -118,6 +127,18 @@ impl fmt::Display for Channel {
118127
}
119128
}
120129

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+
121142
/// Output polarities.
122143
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
123144
pub enum Polarity {

0 commit comments

Comments
 (0)