From cd30b9c4a8d85ee5e9467d100a32feabce2da1f4 Mon Sep 17 00:00:00 2001 From: Andrei-Cristian NEAGU <andrei.neagu1910@stud.fils.upb.ro> Date: Wed, 2 Apr 2025 11:14:10 +0000 Subject: [PATCH] changed the provided display driver (the original one didint run) and modified typo in embedded-hal crate import --- website/lab/05/index.mdx | 106 ++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 29 deletions(-) diff --git a/website/lab/05/index.mdx b/website/lab/05/index.mdx index 4ac0190..892ce59 100644 --- a/website/lab/05/index.mdx +++ b/website/lab/05/index.mdx @@ -399,36 +399,84 @@ spi.transfer(&mut rx_buf, &tx_buf).await; The **ST7735** features a **128x160** screen and an integrated **SD card reader**. For now, we will ignore the SD card reader and focus on the screen. -Because writing an SPI driver for the ST7735 would be out of the scope of the lab, we will be using the one provided by the **mipidsi** crate. +Because writing an SPI driver for the ST7735 would be out of the scope of the lab, we will be using the one provided by the **st7735-lcd** crate. ```rust -use embassy_embedded_hal::shared_bus::blocking::spi::SpiDevice; -use embassy_sync::blocking_mutex::{Mutex, raw::NoopRawMutex}; +use embassy_executor::Spawner; +use embassy_rp::{gpio::{Level, Output}, peripherals::{SPI0, SPI1}, spi::{Blocking, Config as ConfigSpi, Spi}}; use embassy_time::Delay; -use display_interface_spi::SPIInterface; -use mipidsi::models::ST7735s; -use mipidsi::options::{Orientation, Rotation}; - - -let mut screen_config = embassy_rp::spi::Config::default(); -screen_config.frequency = 32_000_000u32; -screen_config.phase = embassy_rp::spi::Phase::CaptureOnSecondTransition; -screen_config.polarity = embassy_rp::spi::Polarity::IdleHigh; - -let screen_rst = Output::new(p.PIN_X, Level::Low); -let screen_dc = Output::new(p.PIN_Y, Level::Low); -let screen_cs = Output::new(p.PIN_Z, Level::High); - -let spi = Spi::new_blocking(p.SPI0, clk, mosi, miso, screen_config); -let spi_bus: Mutex<NoopRawMutex, _> = Mutex::new(RefCell::new(spi)); -let display_spi = SpiDevice::new(&spi_bus, screen_cs); - -let di = SPIInterface::new(display_spi, screen_dc); -let mut screen = mipidsi::Builder::new(ST7735s, di) - .reset_pin(screen_rst) - .orientation(Orientation::new().rotate(Rotation::Deg180)) - .init(&mut Delay) - .unwrap(); +use embassy_sync::blocking_mutex::NoopMutex; +use embassy_embedded_hal::shared_bus::blocking::spi::SpiDevice; +use defmt::info; + +// Add this code to Cargo.toml (modified version of st7735-lcd-rs crate) +// [dependencies] +// st7735-lcd = { version = "0.10.1", git = "https://github.com/mohgTheOmen/st7735-lcd-rs", branch = "master" } +// static_cell = "1.2" +// +// [dependencies.embedded-graphics] +// version = "0.7" +// optional = true +// +// [features] +// default = ["graphics"] +// graphics = ["embedded-graphics"] + +use st7735_lcd::{Orientation, ST7735}; +use core::cell::RefCell; +use static_cell::StaticCell; +use embedded_graphics::{mono_font::{ascii::FONT_6X10, MonoTextStyle}, pixelcolor::Rgb565, prelude::*, text::Text}; +use {defmt_rtt as _, panic_probe as _}; + +// static SPI_BUS: StaticCell<NoopMutex<RefCell<Spi<'static, SPI0, Blocking>>>> = StaticCell::new(); // for borrowing to a task + +// #[embassy_executor::task] +// async fn dispay_task( +// spi_bus: &'static NoopMutex<RefCell<Spi<'static, SPI0, Blocking>>> +// // other parameters +// ) { +// let spi_dev = SpiDevice::new(spi_bus, cs); +// +// // code in the task +// } + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + + let mut spiconfig1 = ConfigSpi::default(); + spiconfig1.frequency = 32_000_000; + + let miso1 = p.PIN_16; + let mosi1 = p.PIN_19; + let clk1 = p.PIN_18; + + let mut spi1 = Spi::new_blocking(p.SPI0, clk1, mosi1, miso1, spiconfig1); + let spi_bus = NoopMutex::new(RefCell::new(spi1)); + // let spi_bus = SPI_BUS.init(spi_bus); // for sending to task + + let mut cs = Output::new(p.PIN_17, Level::High); + let mut dc = Output::new(p.PIN_20, Level::Low); + let mut reset = Output::new(p.PIN_21, Level::High); + + let spi_dev = SpiDevice::new(&spi_bus, cs); + + let mut display = ST7735::new(spi_dev, dc, core::prelude::v1::Some(reset), Default::default(), false, 128, 160); + let mut delay = Delay; + + display.init(&mut delay).unwrap(); + display.set_orientation(&Orientation::Portrait).unwrap(); + + display.clear(Rgb565::BLACK).unwrap(); + + let text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::RED); + + Text::new("Hello, World!", Point::new(20, 50), text_style) + .draw(&mut display) + .unwrap(); + + info!("Display initialized and text written."); +} ``` Make sure to wire the pins on **J9** according to the pins chosen in the code and the picture below. @@ -437,7 +485,7 @@ Make sure to wire the pins on **J9** according to the pins chosen in the code an ### Embedded Graphics -Because **mipidsi** only provides a very primitive way to draw on the screen, by setting individual pixels, we will be using the **embedded_graphics** framework. This library enables us to do more complex things, such as rendering image files, drawing shapes and displaying text. +Because **st7735-lcd** only provides a very primitive way to draw on the screen, by setting individual pixels, we will be using the **embedded_graphics** framework. This library enables us to do more complex things, such as rendering image files, drawing shapes and displaying text. ```rust use embedded_graphics::{ @@ -504,7 +552,7 @@ For this, we will need to initialize the sensor to use the same SPI bus as the s ```rust // Import the Trait in order to be able to use imu_spi.transfer() -use embedded_hal_1::spi::SpiDevice as _; +use embedded_hal::spi::SpiDevice as _; let mut imu_spi = SpiDeviceWithConfig::new(&spi_bus, imu_cs, imu_config); // Note: you no longer need to manually set the chip select pin when using imu_spi.transfer() -- GitLab