Got display code working

This commit is contained in:
Zac Wilson 2025-07-05 14:16:21 +01:00
parent 8bfbab5a0c
commit f2f8705eff
2 changed files with 30 additions and 17 deletions

View file

@ -7,8 +7,7 @@ edition = "2021"
harness = false harness = false
[dependencies] [dependencies]
display-interface = "0.5.0" anyhow = "1.0.98"
display-interface-spi = "0.5.0"
esp-idf-hal = "0.45.2" esp-idf-hal = "0.45.2"
esp-idf-sys = "0.36.1" esp-idf-sys = "0.36.1"
mipidsi = "0.9.0" mipidsi = "0.9.0"

View file

@ -1,5 +1,5 @@
//! Create and initialize ST7789 display driver //! Create and initialize ST7789 display driver
use display_interface_spi::SPIInterface; // use display_interface_spi::SPIInterface;
use esp_idf_hal::{ use esp_idf_hal::{
delay::Delay, delay::Delay,
gpio::{AnyIOPin, Output, PinDriver}, gpio::{AnyIOPin, Output, PinDriver},
@ -8,10 +8,15 @@ use esp_idf_hal::{
prelude::*, prelude::*,
spi::{config::DriverConfig, SpiAnyPins, SpiConfig, SpiDeviceDriver, SpiDriver}, 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< type Drawable<'a> = Display<
SPIInterface<SpiDeviceDriver<'a, SpiDriver<'a>>, PinDriver<'a, Gpio34, Output>>, SpiInterface<'a, SpiDeviceDriver<'a, SpiDriver<'a>>, PinDriver<'a, Gpio34, Output>>,
ST7789, ST7789,
PinDriver<'a, Gpio33, Output>, PinDriver<'a, Gpio33, Output>,
>; >;
@ -21,6 +26,9 @@ pub const DISPLAY_SIZE_WIDTH: u16 = 240;
/// Display height /// Display height
pub const DISPLAY_SIZE_HEIGHT: u16 = 135; 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 /// Create and initialize display driver
/// ///
/// # Examples /// # Examples
@ -49,10 +57,12 @@ pub fn build<'a, SPI>(
cs: impl Peripheral<P = Gpio37> + 'a, cs: impl Peripheral<P = Gpio37> + 'a,
rs: impl Peripheral<P = Gpio34> + 'a, rs: impl Peripheral<P = Gpio34> + 'a,
rst: impl Peripheral<P = Gpio33> + 'a, rst: impl Peripheral<P = Gpio33> + 'a,
) -> Result<Drawable<'a>> ) -> Result<Drawable<'a>, anyhow::Error>
where where
SPI: SpiAnyPins, SPI: SpiAnyPins,
{ {
static mut DISPLAY_BUFFER: [u8; DISPLAY_BUFFER_SIZE] = [0; DISPLAY_BUFFER_SIZE];
let spi_config = SpiConfig::new().baudrate(80.MHz().into()); let spi_config = SpiConfig::new().baudrate(80.MHz().into());
let device_config = DriverConfig::new(); let device_config = DriverConfig::new();
let spi = SpiDeviceDriver::new_single( let spi = SpiDeviceDriver::new_single(
@ -67,19 +77,23 @@ where
let rs = PinDriver::output(rs)?; let rs = PinDriver::output(rs)?;
let rst = PinDriver::output(rst)?; let rst = PinDriver::output(rst)?;
let mut drawable = Builder::st7789(SPIInterface::new(spi, rs)) let mut drawable = Builder::new(
.with_invert_colors(ColorInversion::Inverted) ST7789,
.with_display_size(DISPLAY_SIZE_WIDTH, DISPLAY_SIZE_HEIGHT) SpiInterface::new(spi, rs, unsafe { DISPLAY_BUFFER.as_mut_slice() }),
.with_window_offset_handler(|_| (40, 53)) ) //st7789(SpiInterface::new(spi, rs))
.init(&mut Delay::new_default(), Some(rst)) .invert_colors(ColorInversion::Inverted)
.map_err(|e| anyhow!("{:?}", e))?; .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 drawable
.set_orientation(Orientation::Landscape(true)) .set_orientation(Orientation::new().rotate(mipidsi::options::Rotation::Deg90))
.map_err(|e| anyhow!("{:?}", e))?; .map_err(|e| anyhow::anyhow!("{:?}", e))?;
drawable drawable
.set_scroll_offset(0) .set_vertical_scroll_offset(0)
.map_err(|e| anyhow!("{:?}", e))?; .map_err(|e| anyhow::anyhow!("{:?}", e))?;
Ok(drawable) Ok(drawable)
} }