From 57bd79ee07fbf8c51f341ea37dd2d2f57596bbd6 Mon Sep 17 00:00:00 2001 From: Ralf Fuest Date: Sun, 4 Feb 2024 17:30:11 +0100 Subject: [PATCH] Remove reset pin parameter from Builder::init --- mipidsi/CHANGELOG.md | 1 + mipidsi/docs/MIGRATION.md | 12 +++++- mipidsi/src/builder.rs | 82 +++++++++++++++++++++++++++++++++------ mipidsi/src/lib.rs | 16 ++++---- 4 files changed, 92 insertions(+), 19 deletions(-) diff --git a/mipidsi/CHANGELOG.md b/mipidsi/CHANGELOG.md index 4737131..fb15fa0 100644 --- a/mipidsi/CHANGELOG.md +++ b/mipidsi/CHANGELOG.md @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - bumped MSRV to `v1.75` - fixed `DrawTarget::fill_contiguous` for images that overlap the edge of the framebuffer - replaced model specific `Builder` constructors (like `Builder::gc9a01`) with one generic `Builder::new` constructor +- replaced rest pin parameter in `Builder::init` by `Builder::with_reset_pin` setter ### Removed diff --git a/mipidsi/docs/MIGRATION.md b/mipidsi/docs/MIGRATION.md index 9b52ea0..adf6d6d 100644 --- a/mipidsi/docs/MIGRATION.md +++ b/mipidsi/docs/MIGRATION.md @@ -5,6 +5,16 @@ ### Users * The model specific constructors (like `Builder::ili9341_rgb565`) have been removed. Use the generic `Builder::new` constructor instead: + ```rust + // 0.7 + use mipidsi::Builder; + let display = Builder::ili9341_rgb565(di).init(&mut delay, None)?; + + // 0.8 + use mipidsi::{Builder, models::ILI9341Rgb565}; + let display = Builder::new(ILI9341Rgb565, di).init(&mut delay)?; + ``` +* The reset pin parameter from `Builder::init` has been removed. Use the `Builder::with_reset_pin` setter instead: ```rust // 0.7 use mipidsi::Builder; @@ -12,7 +22,7 @@ // 0.8 use mipidsi::{Builder, models::ILI9341Rgb565}; - let display = Builder::new(ILI9341Rgb565, di).init(&mut delay, Some(rst))?; + let display = Builder::new(ILI9341Rgb565, di).with_reset_pin(rst).init(&mut delay)?; ``` ## v0.6 -> 0.7 diff --git a/mipidsi/src/builder.rs b/mipidsi/src/builder.rs index e6c1c9f..5a90147 100644 --- a/mipidsi/src/builder.rs +++ b/mipidsi/src/builder.rs @@ -1,6 +1,7 @@ //! [super::Display] builder module use display_interface::WriteOnlyDataCommand; +use embedded_hal::digital; use embedded_hal::{delay::DelayNs, digital::OutputPin}; use crate::{dcs::Dcs, error::InitError, models::Model, Display}; @@ -20,21 +21,23 @@ use crate::options::{ColorInversion, ColorOrder, ModelOptions, Orientation, Refr /// # let rst = mipidsi::_mock::MockOutputPin; /// # let mut delay = mipidsi::_mock::MockDelay; /// let mut display = Builder::new(ILI9342CRgb565, di) +/// .with_reset_pin(rst) /// .with_color_order(ColorOrder::Bgr) /// .with_display_size(320, 240) -/// .init(&mut delay, Some(rst)).unwrap(); +/// .init(&mut delay).unwrap(); /// ``` -pub struct Builder +pub struct Builder where DI: WriteOnlyDataCommand, MODEL: Model, { di: DI, model: MODEL, + rst: Option, options: ModelOptions, } -impl Builder +impl Builder where DI: WriteOnlyDataCommand, MODEL: Model, @@ -46,10 +49,18 @@ where Self { di, model, + rst: None, options: ModelOptions::full_size::(), } } +} +impl Builder +where + DI: WriteOnlyDataCommand, + MODEL: Model, + RST: OutputPin, +{ /// /// Sets the invert color flag /// @@ -98,6 +109,16 @@ where self } + /// Sets the reset pin. + pub fn with_reset_pin(self, rst: RST2) -> Builder { + Builder { + di: self.di, + model: self.model, + rst: Some(rst), + options: self.options, + } + } + /// /// Consumes the builder to create a new [Display] with an optional reset [OutputPin]. /// Blocks using the provided [DelayUs] `delay_source` to perform the display initialization. @@ -106,22 +127,19 @@ where /// ### WARNING /// The reset pin needs to be in *high* state in order for the display to operate. /// If it wasn't provided the user needs to ensure this is the case. - pub fn init( + pub fn init( mut self, delay_source: &mut impl DelayNs, - mut rst: Option, - ) -> Result, InitError> - where - RST: OutputPin, - { + ) -> Result, InitError> { let mut dcs = Dcs::write_only(self.di); let madctl = self .model - .init(&mut dcs, delay_source, &self.options, &mut rst)?; + .init(&mut dcs, delay_source, &self.options, &mut self.rst)?; + let display = Display { dcs, model: self.model, - rst, + rst: self.rst, options: self.options, madctl, sleeping: false, // TODO: init should lock state @@ -130,3 +148,45 @@ where Ok(display) } } + +/// Marker type for no reset pin. +pub enum NoResetPin {} + +impl digital::OutputPin for NoResetPin { + fn set_low(&mut self) -> Result<(), Self::Error> { + Ok(()) + } + + fn set_high(&mut self) -> Result<(), Self::Error> { + Ok(()) + } +} + +impl digital::ErrorType for NoResetPin { + type Error = core::convert::Infallible; +} + +#[cfg(test)] +mod tests { + use crate::{ + _mock::{MockDelay, MockDisplayInterface, MockOutputPin}, + models::ILI9341Rgb565, + }; + + use super::*; + + #[test] + fn init_without_reset_pin() { + let _: Display<_, _, NoResetPin> = Builder::new(ILI9341Rgb565, MockDisplayInterface) + .init(&mut MockDelay) + .unwrap(); + } + + #[test] + fn init_with_reset_pin() { + let _: Display<_, _, MockOutputPin> = Builder::new(ILI9341Rgb565, MockDisplayInterface) + .with_reset_pin(MockOutputPin) + .init(&mut MockDelay) + .unwrap(); + } +} diff --git a/mipidsi/src/lib.rs b/mipidsi/src/lib.rs index 36f11ca..6a8204e 100644 --- a/mipidsi/src/lib.rs +++ b/mipidsi/src/lib.rs @@ -38,7 +38,8 @@ //! //! // Create the ILI9486 display driver from the display interface and optional RST pin //! let mut display = Builder::new(ILI9486Rgb666, di) -//! .init(&mut delay, Some(rst)).unwrap(); +//! .with_reset_pin(rst) +//! .init(&mut delay).unwrap(); //! //! // Clear the display to black //! display.clear(Rgb666::BLACK).unwrap(); @@ -78,13 +79,14 @@ //! //! // Create the ILI9341 display driver from the display interface with the RGB666 color space //! let mut display = Builder::new(ILI9341Rgb666, di) +//! .with_reset_pin(rst) //! .with_color_order(mipidsi::options::ColorOrder::Bgr) -//! .init(&mut delay, Some(rst)).unwrap(); +//! .init(&mut delay).unwrap(); //! //! // Clear the display to black //! display.clear(Rgb666::RED).unwrap(); //! ``` -//! Use the appropiate display interface crate for your needs: +//! Use the appropriate display interface crate for your needs: //! - [`display-interface-spi`](https://docs.rs/display-interface-spi/) //! - [`display-interface-parallel-gpio`](https://docs.rs/display-interface-parallel-gpio) //! - [`display-interface-i2c`](https://docs.rs/display-interface-i2c/) @@ -105,7 +107,7 @@ pub mod options; use options::MemoryMapping; mod builder; -pub use builder::Builder; +pub use builder::{Builder, NoResetPin}; pub mod dcs; @@ -373,11 +375,11 @@ pub mod _mock { use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand}; use embedded_hal::{delay::DelayNs, digital, spi}; - use crate::{models::ILI9341Rgb565, Builder, Display}; + use crate::{models::ILI9341Rgb565, Builder, Display, NoResetPin}; - pub fn new_mock_display() -> Display { + pub fn new_mock_display() -> Display { Builder::new(ILI9341Rgb565, MockDisplayInterface) - .init(&mut MockDelay, Some(MockOutputPin)) + .init(&mut MockDelay) .unwrap() }