add custom error type to handle results

This commit is contained in:
Eric Trombly 2020-02-28 11:02:59 -06:00
parent 67c2b90b2a
commit f59959c55c
3 changed files with 25 additions and 6 deletions

16
src/error.rs Normal file
View file

@ -0,0 +1,16 @@
use core::fmt::{self, Debug};
use hal::digital::v2::OutputPin;
pub enum Error<GPIO: OutputPin> {
/// A GPIO could not be set.
Gpio(GPIO::Error),
}
impl<GPIO: OutputPin> Debug for Error<GPIO>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Gpio(_) => write!(f, "GPIO error"),
}
}
}

View file

@ -1,3 +1,4 @@
use crate::Error;
use hal; use hal;
// Section 15.2 of the HINK-E0213A07 data sheet says to hold for 10ms // Section 15.2 of the HINK-E0213A07 data sheet says to hold for 10ms
@ -143,23 +144,23 @@ where
{ {
type Error = SPI::Error; type Error = SPI::Error;
fn reset<D: hal::blocking::delay::DelayMs<u8>>(&mut self, delay: &mut D) { fn reset<D: hal::blocking::delay::DelayMs<u8>>(&mut self, delay: &mut D){
self.reset.set_low(); self.reset.set_low().map_err::<Error<RESET>, _>(Error::Gpio).unwrap();
delay.delay_ms(RESET_DELAY_MS); delay.delay_ms(RESET_DELAY_MS);
self.reset.set_high(); self.reset.set_high().map_err::<Error<RESET>, _>(Error::Gpio).unwrap();
delay.delay_ms(RESET_DELAY_MS); delay.delay_ms(RESET_DELAY_MS);
} }
fn send_command(&mut self, command: u8) -> Result<(), Self::Error> { fn send_command(&mut self, command: u8) -> Result<(), Self::Error> {
self.dc.set_low(); self.dc.set_low().map_err::<Error<DC>, _>(Error::Gpio).unwrap();
self.write(&[command])?; self.write(&[command])?;
self.dc.set_high(); self.dc.set_high().map_err::<Error<DC>, _>(Error::Gpio).unwrap();
Ok(()) Ok(())
} }
fn send_data(&mut self, data: &[u8]) -> Result<(), Self::Error> { fn send_data(&mut self, data: &[u8]) -> Result<(), Self::Error> {
self.dc.set_high(); self.dc.set_high().map_err::<Error<DC>, _>(Error::Gpio).unwrap();
self.write(data) self.write(data)
} }

View file

@ -47,12 +47,14 @@ mod color;
pub mod command; pub mod command;
pub mod config; pub mod config;
pub mod display; pub mod display;
pub mod error;
pub mod graphics; pub mod graphics;
pub mod interface; pub mod interface;
pub use color::Color; pub use color::Color;
pub use config::Builder; pub use config::Builder;
pub use display::{Dimensions, Display, Rotation}; pub use display::{Dimensions, Display, Rotation};
pub use error::Error;
pub use graphics::GraphicDisplay; pub use graphics::GraphicDisplay;
pub use interface::DisplayInterface; pub use interface::DisplayInterface;
pub use interface::Interface; pub use interface::Interface;