From 87797b5a3056b059690706488c3ce6e2ab5c8b4d Mon Sep 17 00:00:00 2001 From: Juraj Michalek Date: Mon, 16 Dec 2024 17:52:42 +0100 Subject: [PATCH] experimental using macros from modules --- esp32-s3-box/Cargo.toml | 1 + esp32-s3-box/src/main.rs | 51 ++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/esp32-s3-box/Cargo.toml b/esp32-s3-box/Cargo.toml index ec2146a..4b368a8 100644 --- a/esp32-s3-box/Cargo.toml +++ b/esp32-s3-box/Cargo.toml @@ -22,6 +22,7 @@ mipidsi = "0.8.0" spooky-core = { path = "../spooky-core" } spooky-embedded = { path = "../spooky-embedded", default-features = false, features = [ "dynamic_maze", "resolution_320x240" ] } esp-display-interface-spi-dma = "0.2.0" +esp-bsp = { path = "../../esp-bsp-rs" } [profile.dev] # Rust debug is too slow. diff --git a/esp32-s3-box/src/main.rs b/esp32-s3-box/src/main.rs index 4c682a5..eb9ee3e 100644 --- a/esp32-s3-box/src/main.rs +++ b/esp32-s3-box/src/main.rs @@ -2,6 +2,8 @@ #![no_main] use esp_display_interface_spi_dma::display_interface_spi_dma; +use esp_bsp::{BoardType, DisplayConfig}; +use esp_bsp::boards::esp32s3box::{lcd_spi, lcd_display_interface, lcd_reset_pin, lcd_backlight_init}; #[allow(unused_imports)] use esp_backtrace as _; @@ -23,6 +25,7 @@ use esp_hal::{ dma::DmaPriority, gpio::{Level, Output}, i2c::master::I2c, + peripherals::Peripherals, prelude::*, spi::master::Spi, }; @@ -39,50 +42,40 @@ use icm42670::{Address, Icm42670}; #[entry] fn main() -> ! { + // Initialize peripherals let peripherals = esp_hal::init(esp_hal::Config::default()); esp_alloc::psram_allocator!(peripherals.PSRAM, esp_hal::psram); let mut delay = Delay::new(); - println!("About to initialize the SPI LED driver"); - - let lcd_sclk = peripherals.GPIO7; - let lcd_mosi = peripherals.GPIO6; - let lcd_cs = peripherals.GPIO5; - let lcd_dc = Output::new(peripherals.GPIO4, Level::Low); - let mut lcd_backlight = Output::new(peripherals.GPIO45, Level::Low); - let lcd_reset = Output::new(peripherals.GPIO48, Level::Low); + println!("Initializing SPI LCD driver for ESP32S3Box"); + // Initialize I2C for accelerometer let i2c_sda = peripherals.GPIO8; let i2c_scl = peripherals.GPIO18; let i2c = I2c::new(peripherals.I2C0, esp_hal::i2c::master::Config::default()) .with_sda(i2c_sda) .with_scl(i2c_scl); + // Initialize DMA for SPI let dma = Dma::new(peripherals.DMA); let dma_channel = dma.channel0; - let spi = Spi::new_with_config( - peripherals.SPI2, - esp_hal::spi::master::Config { - frequency: 40u32.MHz(), - ..esp_hal::spi::master::Config::default() - }, - ) - .with_sck(lcd_sclk) - .with_mosi(lcd_mosi) - .with_cs(lcd_cs) - .with_dma(dma_channel.configure(false, DmaPriority::Priority0)); + // Use the `lcd_spi` macro to initialize the SPI interface + let spi = lcd_spi!(peripherals, dma_channel); println!("SPI ready"); - let di = display_interface_spi_dma::new_no_cs(LCD_MEMORY_SIZE, spi, lcd_dc); + // Use the `lcd_display_interface` macro to create the display interface + let di = lcd_display_interface!(peripherals, spi); // ESP32-S3-BOX display initialization workaround: Wait for the display to power up. - // If delay is 250ms, picture will be fuzzy. - // If there is no delay, display is blank delay.delay_ns(500_000u32); + // Use the `lcd_reset_pin` macro to set the reset pin + let lcd_reset = lcd_reset_pin!(peripherals); + + // Initialize the display using the builder pattern let mut display = mipidsi::Builder::new(mipidsi::models::ILI9341Rgb565, di) .display_size(240, 320) .orientation( @@ -95,23 +88,29 @@ fn main() -> ! { .init(&mut delay) .unwrap(); - lcd_backlight.set_high(); + // Use the `lcd_backlight_init` macro to turn on the backlight + lcd_backlight_init!(peripherals); println!("Initializing..."); + + // Render an "Initializing..." message on the display Text::new( "Initializing...", Point::new(80, 110), MonoTextStyle::new(&FONT_8X13, RgbColor::WHITE), ) - .draw(&mut display) - .unwrap(); + .draw(&mut display) + .unwrap(); + // Initialize the accelerometer let icm = Icm42670::new(i2c, Address::Primary).unwrap(); + // Initialize the random number generator let mut rng = Rng::new(peripherals.RNG); let mut seed_buffer = [0u8; 32]; rng.read(&mut seed_buffer); + // Initialize the movement controllers let accel_movement_controller = AccelMovementController::new(icm, 0.2); let demo_movement_controller = spooky_core::demo_movement_controller::DemoMovementController::new(seed_buffer); @@ -119,5 +118,7 @@ fn main() -> ! { AccelCompositeController::new(demo_movement_controller, accel_movement_controller); println!("Entering main loop"); + + // Enter the application loop app_loop(&mut display, seed_buffer, movement_controller); }