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
[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"

View file

@ -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<SpiDeviceDriver<'a, SpiDriver<'a>>, 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<P = Gpio37> + 'a,
rs: impl Peripheral<P = Gpio34> + 'a,
rst: impl Peripheral<P = Gpio33> + 'a,
) -> Result<Drawable<'a>>
) -> Result<Drawable<'a>, 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)
}
}