Skip to content

Commit

Permalink
experimental using macros from modules
Browse files Browse the repository at this point in the history
  • Loading branch information
georgik committed Dec 16, 2024
1 parent 73ffc5b commit 87797b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions esp32-s3-box/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 26 additions & 25 deletions esp32-s3-box/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand All @@ -23,6 +25,7 @@ use esp_hal::{
dma::DmaPriority,
gpio::{Level, Output},
i2c::master::I2c,
peripherals::Peripherals,
prelude::*,
spi::master::Spi,
};
Expand All @@ -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(
Expand All @@ -95,29 +88,37 @@ 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);
let movement_controller =
AccelCompositeController::new(demo_movement_controller, accel_movement_controller);

println!("Entering main loop");

// Enter the application loop
app_loop(&mut display, seed_buffer, movement_controller);
}

0 comments on commit 87797b5

Please sign in to comment.