Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embedded-hal-bus: Implement Display and std::error::Error for DeviceError if possible. #575

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion embedded-hal-bus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ provides mechanisms to obtain multiple `I2c` instances out of a single `I2c` ins

## Optional Cargo features

- **`std`**: enable shared bus implementations using `std::sync::Mutex`.
- **`std`**: enable shared bus implementations using `std::sync::Mutex`, and implement
`std::error::Error` for `DeviceError`.
- **`async`**: enable `embedded-hal-async` support.
- **`defmt-03`**: Derive `defmt::Format` from `defmt` 0.3 for enums and structs.

Expand Down
14 changes: 13 additions & 1 deletion embedded-hal-bus/src/spi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `SpiDevice` implementations.

use core::fmt::Debug;
use core::fmt::{self, Debug, Display, Formatter};
use embedded_hal::spi::{Error, ErrorKind};

mod exclusive;
Expand Down Expand Up @@ -29,6 +29,18 @@ pub enum DeviceError<BUS, CS> {
Cs(CS),
}

impl<BUS: Display, CS: Display> Display for DeviceError<BUS, CS> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Spi(bus) => write!(f, "SPI bus error: {}", bus),
Self::Cs(cs) => write!(f, "SPI CS error: {}", cs),
}
}
}

#[cfg(feature = "std")]
impl<BUS: Debug + Display, CS: Debug + Display> std::error::Error for DeviceError<BUS, CS> {}

impl<BUS, CS> Error for DeviceError<BUS, CS>
where
BUS: Error + Debug,
Expand Down
Loading