Skip to content

Commit 53adf3c

Browse files
authored
Merge pull request #14 from DavidR86/master
Implement Error and Display for SenseHatError
2 parents 3a7a629 + ed7960a commit 53adf3c

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/lib.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ mod hts221;
5151
mod lps25h;
5252
mod rh;
5353

54+
use std::error::Error;
55+
use std::fmt::Display;
56+
5457
pub use measurements::Angle;
5558
pub use measurements::Pressure;
5659
pub use measurements::Temperature;
@@ -127,10 +130,27 @@ pub enum SenseHatError {
127130
GenericError,
128131
I2CError(LinuxI2CError),
129132
LSM9DS1Error(lsm9ds1::Error),
133+
#[cfg(feature = "led-matrix")]
130134
ScreenError(sensehat_screen::error::ScreenError),
131135
CharacterError(std::string::FromUtf16Error),
132136
}
133137

138+
impl Display for SenseHatError {
139+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
140+
match self {
141+
SenseHatError::NotReady => write!(f, "Not ready error"),
142+
SenseHatError::GenericError => write!(f, "Generic error"),
143+
SenseHatError::I2CError(err) => write!(f, "I2C error: {}", err),
144+
SenseHatError::LSM9DS1Error(err) => write!(f, "LSM9DS1 error: {}", err),
145+
#[cfg(feature = "led-matrix")]
146+
SenseHatError::ScreenError(err) => write!(f, "Screen error: {}", err),
147+
SenseHatError::CharacterError(err) => write!(f, "Character error: {}", err),
148+
}
149+
}
150+
}
151+
152+
impl Error for SenseHatError {}
153+
134154
/// A shortcut for Results that can return `T` or `SenseHatError`.
135155
pub type SenseHatResult<T> = Result<T, SenseHatError>;
136156

@@ -154,7 +174,7 @@ impl<'a> SenseHat<'a> {
154174
let status = self.pressure_chip.status()?;
155175
if (status & 1) != 0 {
156176
Ok(Temperature::from_celsius(
157-
self.pressure_chip.get_temp_celcius()?
177+
self.pressure_chip.get_temp_celcius()?,
158178
))
159179
} else {
160180
Err(SenseHatError::NotReady)
@@ -166,7 +186,7 @@ impl<'a> SenseHat<'a> {
166186
let status = self.pressure_chip.status()?;
167187
if (status & 2) != 0 {
168188
Ok(Pressure::from_hectopascals(
169-
self.pressure_chip.get_pressure_hpa()?
189+
self.pressure_chip.get_pressure_hpa()?,
170190
))
171191
} else {
172192
Err(SenseHatError::NotReady)
@@ -279,7 +299,13 @@ impl<'a> SenseHat<'a> {
279299
/// * a string from the [W3C basic keywords](https://www.w3.org/TR/css-color-3/#html4) like `"white"` or `"purple"`, or
280300
/// * an RGB 8-bit triple like `(0, 0xFF, 0)`.
281301
#[cfg(feature = "led-matrix")]
282-
pub fn show_message<INT, FG, BG>(&mut self, message: &str, interval: INT, fg: FG, bg: BG) -> SenseHatResult<()>
302+
pub fn show_message<INT, FG, BG>(
303+
&mut self,
304+
message: &str,
305+
interval: INT,
306+
fg: FG,
307+
bg: BG,
308+
) -> SenseHatResult<()>
283309
where
284310
INT: Into<::std::time::Duration>,
285311
FG: Into<Colour>,
@@ -303,7 +329,6 @@ impl<'a> SenseHat<'a> {
303329
::std::thread::sleep(wait_time);
304330
});
305331
Ok(())
306-
307332
}
308333

309334
/// Displays a scrolling message on the LED matrix. Blocks until the
@@ -354,6 +379,7 @@ impl From<std::string::FromUtf16Error> for SenseHatError {
354379
}
355380
}
356381

382+
#[cfg(feature = "led-matrix")]
357383
impl From<sensehat_screen::error::ScreenError> for SenseHatError {
358384
fn from(err: sensehat_screen::error::ScreenError) -> SenseHatError {
359385
SenseHatError::ScreenError(err)

src/lsm9ds1.rs

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
//! a C wrapper of the `RTIMULib` C++ API. We then call that unsafe C wrapper
1010
//! here, ensuring that any memory allocations were undone on drop.
1111
12+
use std::fmt::Display;
13+
1214
use super::{Angle, ImuData, Orientation, Vector3D};
1315
use libc;
1416

@@ -63,6 +65,16 @@ pub enum Error {
6365
RTIMULibError,
6466
}
6567

68+
impl Display for Error {
69+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70+
match self {
71+
Error::RTIMULibError => write!(f, "RTIMULib error"),
72+
}
73+
}
74+
}
75+
76+
impl std::error::Error for Error {}
77+
6678
pub(crate) struct Lsm9ds1<'a> {
6779
rtimulib_ref: &'a mut RTIMULibContext,
6880
}

src/lsm9ds1_dummy.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
//! This is just a placeholder so the the docs build without RTIMULib.
44
55
use super::ImuData;
6-
use std::marker::PhantomData;
6+
use std::{fmt::Display, marker::PhantomData};
77

88
#[derive(Debug)]
99
pub enum Error {
1010
RTIMULibError,
1111
}
1212

13+
impl Display for Error {
14+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15+
match self {
16+
Error::RTIMULibError => write!(f, "RTIMULib Error"),
17+
}
18+
}
19+
}
20+
1321
pub(crate) struct Lsm9ds1<'a> {
1422
phantom: PhantomData<&'a u32>,
1523
}

0 commit comments

Comments
 (0)