Skip to content

Commit 87797b5

Browse files
committed
experimental using macros from modules
1 parent 73ffc5b commit 87797b5

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

esp32-s3-box/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mipidsi = "0.8.0"
2222
spooky-core = { path = "../spooky-core" }
2323
spooky-embedded = { path = "../spooky-embedded", default-features = false, features = [ "dynamic_maze", "resolution_320x240" ] }
2424
esp-display-interface-spi-dma = "0.2.0"
25+
esp-bsp = { path = "../../esp-bsp-rs" }
2526

2627
[profile.dev]
2728
# Rust debug is too slow.

esp32-s3-box/src/main.rs

+26-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#![no_main]
33

44
use esp_display_interface_spi_dma::display_interface_spi_dma;
5+
use esp_bsp::{BoardType, DisplayConfig};
6+
use esp_bsp::boards::esp32s3box::{lcd_spi, lcd_display_interface, lcd_reset_pin, lcd_backlight_init};
57

68
#[allow(unused_imports)]
79
use esp_backtrace as _;
@@ -23,6 +25,7 @@ use esp_hal::{
2325
dma::DmaPriority,
2426
gpio::{Level, Output},
2527
i2c::master::I2c,
28+
peripherals::Peripherals,
2629
prelude::*,
2730
spi::master::Spi,
2831
};
@@ -39,50 +42,40 @@ use icm42670::{Address, Icm42670};
3942

4043
#[entry]
4144
fn main() -> ! {
45+
// Initialize peripherals
4246
let peripherals = esp_hal::init(esp_hal::Config::default());
4347
esp_alloc::psram_allocator!(peripherals.PSRAM, esp_hal::psram);
4448

4549
let mut delay = Delay::new();
4650

47-
println!("About to initialize the SPI LED driver");
48-
49-
let lcd_sclk = peripherals.GPIO7;
50-
let lcd_mosi = peripherals.GPIO6;
51-
let lcd_cs = peripherals.GPIO5;
52-
let lcd_dc = Output::new(peripherals.GPIO4, Level::Low);
53-
let mut lcd_backlight = Output::new(peripherals.GPIO45, Level::Low);
54-
let lcd_reset = Output::new(peripherals.GPIO48, Level::Low);
51+
println!("Initializing SPI LCD driver for ESP32S3Box");
5552

53+
// Initialize I2C for accelerometer
5654
let i2c_sda = peripherals.GPIO8;
5755
let i2c_scl = peripherals.GPIO18;
5856
let i2c = I2c::new(peripherals.I2C0, esp_hal::i2c::master::Config::default())
5957
.with_sda(i2c_sda)
6058
.with_scl(i2c_scl);
6159

60+
// Initialize DMA for SPI
6261
let dma = Dma::new(peripherals.DMA);
6362
let dma_channel = dma.channel0;
6463

65-
let spi = Spi::new_with_config(
66-
peripherals.SPI2,
67-
esp_hal::spi::master::Config {
68-
frequency: 40u32.MHz(),
69-
..esp_hal::spi::master::Config::default()
70-
},
71-
)
72-
.with_sck(lcd_sclk)
73-
.with_mosi(lcd_mosi)
74-
.with_cs(lcd_cs)
75-
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
64+
// Use the `lcd_spi` macro to initialize the SPI interface
65+
let spi = lcd_spi!(peripherals, dma_channel);
7666

7767
println!("SPI ready");
7868

79-
let di = display_interface_spi_dma::new_no_cs(LCD_MEMORY_SIZE, spi, lcd_dc);
69+
// Use the `lcd_display_interface` macro to create the display interface
70+
let di = lcd_display_interface!(peripherals, spi);
8071

8172
// ESP32-S3-BOX display initialization workaround: Wait for the display to power up.
82-
// If delay is 250ms, picture will be fuzzy.
83-
// If there is no delay, display is blank
8473
delay.delay_ns(500_000u32);
8574

75+
// Use the `lcd_reset_pin` macro to set the reset pin
76+
let lcd_reset = lcd_reset_pin!(peripherals);
77+
78+
// Initialize the display using the builder pattern
8679
let mut display = mipidsi::Builder::new(mipidsi::models::ILI9341Rgb565, di)
8780
.display_size(240, 320)
8881
.orientation(
@@ -95,29 +88,37 @@ fn main() -> ! {
9588
.init(&mut delay)
9689
.unwrap();
9790

98-
lcd_backlight.set_high();
91+
// Use the `lcd_backlight_init` macro to turn on the backlight
92+
lcd_backlight_init!(peripherals);
9993

10094
println!("Initializing...");
95+
96+
// Render an "Initializing..." message on the display
10197
Text::new(
10298
"Initializing...",
10399
Point::new(80, 110),
104100
MonoTextStyle::new(&FONT_8X13, RgbColor::WHITE),
105101
)
106-
.draw(&mut display)
107-
.unwrap();
102+
.draw(&mut display)
103+
.unwrap();
108104

105+
// Initialize the accelerometer
109106
let icm = Icm42670::new(i2c, Address::Primary).unwrap();
110107

108+
// Initialize the random number generator
111109
let mut rng = Rng::new(peripherals.RNG);
112110
let mut seed_buffer = [0u8; 32];
113111
rng.read(&mut seed_buffer);
114112

113+
// Initialize the movement controllers
115114
let accel_movement_controller = AccelMovementController::new(icm, 0.2);
116115
let demo_movement_controller =
117116
spooky_core::demo_movement_controller::DemoMovementController::new(seed_buffer);
118117
let movement_controller =
119118
AccelCompositeController::new(demo_movement_controller, accel_movement_controller);
120119

121120
println!("Entering main loop");
121+
122+
// Enter the application loop
122123
app_loop(&mut display, seed_buffer, movement_controller);
123124
}

0 commit comments

Comments
 (0)