Use Result alias

This commit is contained in:
Grant Miller 2021-05-10 17:15:59 -05:00
parent 954d111959
commit be44c24eea

View file

@ -14,6 +14,8 @@ pub use embedded_hal::spi::MODE_0 as SPI_MODE;
pub use display_interface::DisplayError; pub use display_interface::DisplayError;
type Result<T = (), E = DisplayError> = core::result::Result<T, E>;
/// Trait that defines display size information /// Trait that defines display size information
pub trait DisplaySize { pub trait DisplaySize {
/// Width in pixels /// Width in pixels
@ -81,7 +83,7 @@ where
delay: &mut DELAY, delay: &mut DELAY,
mode: Orientation, mode: Orientation,
_display_size: SIZE, _display_size: SIZE,
) -> Result<Self, DisplayError> ) -> Result<Self>
where where
DELAY: DelayMs<u16>, DELAY: DelayMs<u16>,
SIZE: DisplaySize, SIZE: DisplaySize,
@ -129,17 +131,17 @@ where
Ok(ili9341) Ok(ili9341)
} }
fn command(&mut self, cmd: Command, args: &[u8]) -> Result<(), DisplayError> { fn command(&mut self, cmd: Command, args: &[u8]) -> Result {
self.interface.send_commands(U8Iter(&mut once(cmd as u8)))?; self.interface.send_commands(U8Iter(&mut once(cmd as u8)))?;
self.interface.send_data(U8Iter(&mut args.iter().cloned())) self.interface.send_data(U8Iter(&mut args.iter().cloned()))
} }
fn write_iter<I: IntoIterator<Item = u16>>(&mut self, data: I) -> Result<(), DisplayError> { fn write_iter<I: IntoIterator<Item = u16>>(&mut self, data: I) -> Result {
self.command(Command::MemoryWrite, &[])?; self.command(Command::MemoryWrite, &[])?;
self.interface.send_data(U16BEIter(&mut data.into_iter())) self.interface.send_data(U16BEIter(&mut data.into_iter()))
} }
fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result<(), DisplayError> { fn set_window(&mut self, x0: u16, y0: u16, x1: u16, y1: u16) -> Result {
self.command( self.command(
Command::ColumnAddressSet, Command::ColumnAddressSet,
&[ &[
@ -165,7 +167,7 @@ where
&mut self, &mut self,
fixed_top_lines: u16, fixed_top_lines: u16,
fixed_bottom_lines: u16, fixed_bottom_lines: u16,
) -> Result<Scroller, DisplayError> { ) -> Result<Scroller> {
let height = match self.mode { let height = match self.mode {
Orientation::Landscape | Orientation::LandscapeFlipped => self.width, Orientation::Landscape | Orientation::LandscapeFlipped => self.width,
Orientation::Portrait | Orientation::PortraitFlipped => self.height, Orientation::Portrait | Orientation::PortraitFlipped => self.height,
@ -187,11 +189,7 @@ where
Ok(Scroller::new(fixed_top_lines, fixed_bottom_lines, height)) Ok(Scroller::new(fixed_top_lines, fixed_bottom_lines, height))
} }
pub fn scroll_vertically( pub fn scroll_vertically(&mut self, scroller: &mut Scroller, num_lines: u16) -> Result {
&mut self,
scroller: &mut Scroller,
num_lines: u16,
) -> Result<(), DisplayError> {
scroller.top_offset += num_lines; scroller.top_offset += num_lines;
if scroller.top_offset > (scroller.height - scroller.fixed_bottom_lines) { if scroller.top_offset > (scroller.height - scroller.fixed_bottom_lines) {
scroller.top_offset = scroller.fixed_top_lines scroller.top_offset = scroller.fixed_top_lines
@ -223,7 +221,7 @@ where
x1: u16, x1: u16,
y1: u16, y1: u16,
data: I, data: I,
) -> Result<(), DisplayError> { ) -> Result {
self.set_window(x0, y0, x1, y1)?; self.set_window(x0, y0, x1, y1)?;
self.write_iter(data) self.write_iter(data)
} }
@ -237,20 +235,13 @@ where
/// video memory. /// video memory.
/// ///
/// The expected format is rgb565. /// The expected format is rgb565.
pub fn draw_raw( pub fn draw_raw(&mut self, x0: u16, y0: u16, x1: u16, y1: u16, data: &[u16]) -> Result {
&mut self,
x0: u16,
y0: u16,
x1: u16,
y1: u16,
data: &[u16],
) -> Result<(), DisplayError> {
self.set_window(x0, y0, x1, y1)?; self.set_window(x0, y0, x1, y1)?;
self.write_iter(data.iter().cloned()) self.write_iter(data.iter().cloned())
} }
/// Change the orientation of the screen /// Change the orientation of the screen
pub fn set_orientation(&mut self, mode: Orientation) -> Result<(), DisplayError> { pub fn set_orientation(&mut self, mode: Orientation) -> Result {
let was_landscape = match self.mode { let was_landscape = match self.mode {
Orientation::Landscape | Orientation::LandscapeFlipped => true, Orientation::Landscape | Orientation::LandscapeFlipped => true,
Orientation::Portrait | Orientation::PortraitFlipped => false, Orientation::Portrait | Orientation::PortraitFlipped => false,