2
2
#![ no_main]
3
3
4
4
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} ;
5
7
6
8
#[ allow( unused_imports) ]
7
9
use esp_backtrace as _;
@@ -23,6 +25,7 @@ use esp_hal::{
23
25
dma:: DmaPriority ,
24
26
gpio:: { Level , Output } ,
25
27
i2c:: master:: I2c ,
28
+ peripherals:: Peripherals ,
26
29
prelude:: * ,
27
30
spi:: master:: Spi ,
28
31
} ;
@@ -39,50 +42,40 @@ use icm42670::{Address, Icm42670};
39
42
40
43
#[ entry]
41
44
fn main ( ) -> ! {
45
+ // Initialize peripherals
42
46
let peripherals = esp_hal:: init ( esp_hal:: Config :: default ( ) ) ;
43
47
esp_alloc:: psram_allocator!( peripherals. PSRAM , esp_hal:: psram) ;
44
48
45
49
let mut delay = Delay :: new ( ) ;
46
50
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" ) ;
55
52
53
+ // Initialize I2C for accelerometer
56
54
let i2c_sda = peripherals. GPIO8 ;
57
55
let i2c_scl = peripherals. GPIO18 ;
58
56
let i2c = I2c :: new ( peripherals. I2C0 , esp_hal:: i2c:: master:: Config :: default ( ) )
59
57
. with_sda ( i2c_sda)
60
58
. with_scl ( i2c_scl) ;
61
59
60
+ // Initialize DMA for SPI
62
61
let dma = Dma :: new ( peripherals. DMA ) ;
63
62
let dma_channel = dma. channel0 ;
64
63
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) ;
76
66
77
67
println ! ( "SPI ready" ) ;
78
68
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) ;
80
71
81
72
// 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
84
73
delay. delay_ns ( 500_000u32 ) ;
85
74
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
86
79
let mut display = mipidsi:: Builder :: new ( mipidsi:: models:: ILI9341Rgb565 , di)
87
80
. display_size ( 240 , 320 )
88
81
. orientation (
@@ -95,29 +88,37 @@ fn main() -> ! {
95
88
. init ( & mut delay)
96
89
. unwrap ( ) ;
97
90
98
- lcd_backlight. set_high ( ) ;
91
+ // Use the `lcd_backlight_init` macro to turn on the backlight
92
+ lcd_backlight_init ! ( peripherals) ;
99
93
100
94
println ! ( "Initializing..." ) ;
95
+
96
+ // Render an "Initializing..." message on the display
101
97
Text :: new (
102
98
"Initializing..." ,
103
99
Point :: new ( 80 , 110 ) ,
104
100
MonoTextStyle :: new ( & FONT_8X13 , RgbColor :: WHITE ) ,
105
101
)
106
- . draw ( & mut display)
107
- . unwrap ( ) ;
102
+ . draw ( & mut display)
103
+ . unwrap ( ) ;
108
104
105
+ // Initialize the accelerometer
109
106
let icm = Icm42670 :: new ( i2c, Address :: Primary ) . unwrap ( ) ;
110
107
108
+ // Initialize the random number generator
111
109
let mut rng = Rng :: new ( peripherals. RNG ) ;
112
110
let mut seed_buffer = [ 0u8 ; 32 ] ;
113
111
rng. read ( & mut seed_buffer) ;
114
112
113
+ // Initialize the movement controllers
115
114
let accel_movement_controller = AccelMovementController :: new ( icm, 0.2 ) ;
116
115
let demo_movement_controller =
117
116
spooky_core:: demo_movement_controller:: DemoMovementController :: new ( seed_buffer) ;
118
117
let movement_controller =
119
118
AccelCompositeController :: new ( demo_movement_controller, accel_movement_controller) ;
120
119
121
120
println ! ( "Entering main loop" ) ;
121
+
122
+ // Enter the application loop
122
123
app_loop ( & mut display, seed_buffer, movement_controller) ;
123
124
}
0 commit comments