mirror of
https://github.com/esp-rs/esp-idf-hal.git
synced 2025-09-27 12:21:02 +00:00

* Hacky async TWAI driver * Async TWAI: Custom priority and pin to core * Async TWAI: Proper configuration of alerts * Do not refer to esp-idf-sys as it is not an explicit dependency anymore
90 lines
2.5 KiB
Rust
90 lines
2.5 KiB
Rust
//! SPI example with the ST7789 using the ESP-RUST-BOARD
|
|
//!
|
|
//! Folowing pins are used:
|
|
//! RST GPIO3
|
|
//! DC GPIO4
|
|
//! BACKLIGHT GPIO5
|
|
//! SCLK GPIO6
|
|
//! SDA GPIO7
|
|
//!
|
|
//! Depending on your target and the board you are using you have to change the pins.
|
|
//!
|
|
//! For this example you need to hook up an ST7789 SPI display.
|
|
//! The display will display an image on ferris the crab on a black background.
|
|
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
use embedded_hal::spi::MODE_3;
|
|
|
|
use esp_idf_hal::delay::Ets;
|
|
use esp_idf_hal::gpio::*;
|
|
use esp_idf_hal::peripherals::Peripherals;
|
|
use esp_idf_hal::spi::*;
|
|
use esp_idf_hal::units::FromValueType;
|
|
|
|
use display_interface_spi::SPIInterfaceNoCS;
|
|
|
|
use embedded_graphics::image::*;
|
|
use embedded_graphics::pixelcolor::Rgb565;
|
|
use embedded_graphics::prelude::*;
|
|
|
|
use mipidsi::{Builder, Orientation};
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
let peripherals = Peripherals::take().unwrap();
|
|
let spi = peripherals.spi2;
|
|
|
|
let rst = PinDriver::output(peripherals.pins.gpio3)?;
|
|
let dc = PinDriver::output(peripherals.pins.gpio4)?;
|
|
let mut backlight = PinDriver::output(peripherals.pins.gpio5)?;
|
|
let sclk = peripherals.pins.gpio6;
|
|
let sda = peripherals.pins.gpio7;
|
|
let sdi = peripherals.pins.gpio8;
|
|
let cs = peripherals.pins.gpio10;
|
|
|
|
let mut delay = Ets;
|
|
|
|
// configuring the spi interface, note that in order for the ST7789 to work, the data_mode needs to be set to MODE_3
|
|
let config = config::Config::new()
|
|
.baudrate(26.MHz().into())
|
|
.data_mode(MODE_3);
|
|
|
|
let device = SpiDeviceDriver::new_single(
|
|
spi,
|
|
sclk,
|
|
sda,
|
|
Some(sdi),
|
|
Some(cs),
|
|
&SpiDriverConfig::new(),
|
|
&config,
|
|
)?;
|
|
|
|
// display interface abstraction from SPI and DC
|
|
let di = SPIInterfaceNoCS::new(device, dc);
|
|
|
|
// create driver
|
|
let mut display = Builder::st7789(di)
|
|
.with_display_size(240, 240)
|
|
// set default orientation
|
|
.with_orientation(Orientation::Portrait(false))
|
|
// initialize
|
|
.init(&mut delay, Some(rst))
|
|
.unwrap();
|
|
|
|
// turn on the backlight
|
|
backlight.set_high()?;
|
|
let raw_image_data = ImageRawLE::new(include_bytes!("../examples/assets/ferris.raw"), 86);
|
|
let ferris = Image::new(&raw_image_data, Point::new(0, 0));
|
|
|
|
// draw image on black background
|
|
display.clear(Rgb565::BLACK).unwrap();
|
|
ferris.draw(&mut display).unwrap();
|
|
|
|
println!("Image printed!");
|
|
|
|
loop {
|
|
thread::sleep(Duration::from_millis(1000));
|
|
}
|
|
}
|