From f2f8705effd7791c478bd36e85d5847f9548911b Mon Sep 17 00:00:00 2001 From: Zac Wilson Date: Sat, 5 Jul 2025 14:16:21 +0100 Subject: [PATCH] Got display code working --- cardputer-bsc/Cargo.toml | 3 +-- cardputer-bsc/src/display.rs | 44 ++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/cardputer-bsc/Cargo.toml b/cardputer-bsc/Cargo.toml index 6900020..8631a3e 100644 --- a/cardputer-bsc/Cargo.toml +++ b/cardputer-bsc/Cargo.toml @@ -7,8 +7,7 @@ edition = "2021" harness = false [dependencies] -display-interface = "0.5.0" -display-interface-spi = "0.5.0" +anyhow = "1.0.98" esp-idf-hal = "0.45.2" esp-idf-sys = "0.36.1" mipidsi = "0.9.0" diff --git a/cardputer-bsc/src/display.rs b/cardputer-bsc/src/display.rs index 505e984..9b764fd 100644 --- a/cardputer-bsc/src/display.rs +++ b/cardputer-bsc/src/display.rs @@ -1,5 +1,5 @@ //! Create and initialize ST7789 display driver -use display_interface_spi::SPIInterface; +// use display_interface_spi::SPIInterface; use esp_idf_hal::{ delay::Delay, gpio::{AnyIOPin, Output, PinDriver}, @@ -8,10 +8,15 @@ use esp_idf_hal::{ prelude::*, spi::{config::DriverConfig, SpiAnyPins, SpiConfig, SpiDeviceDriver, SpiDriver}, }; -use mipidsi::{models::ST7789, options::Orientation, Builder, options::ColorInversion, Display}; +use mipidsi::{ + interface::SpiInterface, + models::ST7789, + options::{ColorInversion, Orientation}, + Builder, Display, +}; type Drawable<'a> = Display< - SPIInterface>, PinDriver<'a, Gpio34, Output>>, + SpiInterface<'a, SpiDeviceDriver<'a, SpiDriver<'a>>, PinDriver<'a, Gpio34, Output>>, ST7789, PinDriver<'a, Gpio33, Output>, >; @@ -21,6 +26,9 @@ pub const DISPLAY_SIZE_WIDTH: u16 = 240; /// Display height pub const DISPLAY_SIZE_HEIGHT: u16 = 135; +pub const DISPLAY_BUFFER_SIZE: usize = + DISPLAY_SIZE_WIDTH as usize * DISPLAY_SIZE_HEIGHT as usize * 2; + /// Create and initialize display driver /// /// # Examples @@ -49,10 +57,12 @@ pub fn build<'a, SPI>( cs: impl Peripheral

+ 'a, rs: impl Peripheral

+ 'a, rst: impl Peripheral

+ 'a, -) -> Result> +) -> Result, anyhow::Error> where SPI: SpiAnyPins, { + static mut DISPLAY_BUFFER: [u8; DISPLAY_BUFFER_SIZE] = [0; DISPLAY_BUFFER_SIZE]; + let spi_config = SpiConfig::new().baudrate(80.MHz().into()); let device_config = DriverConfig::new(); let spi = SpiDeviceDriver::new_single( @@ -67,19 +77,23 @@ where let rs = PinDriver::output(rs)?; let rst = PinDriver::output(rst)?; - let mut drawable = Builder::st7789(SPIInterface::new(spi, rs)) - .with_invert_colors(ColorInversion::Inverted) - .with_display_size(DISPLAY_SIZE_WIDTH, DISPLAY_SIZE_HEIGHT) - .with_window_offset_handler(|_| (40, 53)) - .init(&mut Delay::new_default(), Some(rst)) - .map_err(|e| anyhow!("{:?}", e))?; + let mut drawable = Builder::new( + ST7789, + SpiInterface::new(spi, rs, unsafe { DISPLAY_BUFFER.as_mut_slice() }), + ) //st7789(SpiInterface::new(spi, rs)) + .invert_colors(ColorInversion::Inverted) + .display_size(DISPLAY_SIZE_HEIGHT, DISPLAY_SIZE_WIDTH) // deliberately reversed order + .display_offset(40, 53) + .reset_pin(rst) + .init(&mut Delay::new_default()) + .map_err(|e| anyhow::anyhow!("{:?}", e))?; drawable - .set_orientation(Orientation::Landscape(true)) - .map_err(|e| anyhow!("{:?}", e))?; + .set_orientation(Orientation::new().rotate(mipidsi::options::Rotation::Deg90)) + .map_err(|e| anyhow::anyhow!("{:?}", e))?; drawable - .set_scroll_offset(0) - .map_err(|e| anyhow!("{:?}", e))?; + .set_vertical_scroll_offset(0) + .map_err(|e| anyhow::anyhow!("{:?}", e))?; Ok(drawable) -} \ No newline at end of file +}