1
1
#![ no_std]
2
2
#![ no_main]
3
3
4
+ use esp_bsp:: prelude:: * ;
4
5
use esp_display_interface_spi_dma:: display_interface_spi_dma;
5
6
6
- use esp_backtrace as _;
7
- use esp_println:: println;
8
- use hal:: {
9
- clock:: { ClockControl , CpuClock } ,
10
- dma:: DmaPriority ,
11
- gdma:: Gdma ,
12
- peripherals:: Peripherals ,
13
- prelude:: * ,
14
- spi:: {
15
- master:: { prelude:: * , Spi } ,
16
- SpiMode ,
17
- } ,
18
- Delay , Rng , IO ,
19
- } ;
20
-
21
7
use embedded_graphics:: {
22
8
mono_font:: { ascii:: FONT_8X13 , MonoTextStyle } ,
23
9
pixelcolor:: Rgb565 ,
@@ -27,10 +13,23 @@ use embedded_graphics::{
27
13
text:: Text ,
28
14
Drawable ,
29
15
} ;
16
+ #[ allow( unused_imports) ]
17
+ use esp_backtrace as _;
18
+ use esp_hal:: gpio:: OutputOpenDrain ;
19
+ use esp_hal:: gpio:: Pull ;
20
+ use esp_hal:: rng:: Rng ;
21
+ use esp_hal:: {
22
+ delay:: Delay ,
23
+ dma:: Dma ,
24
+ dma:: DmaPriority ,
25
+ gpio:: { Level , Output } ,
26
+ prelude:: * ,
27
+ spi:: master:: Spi ,
28
+ } ;
30
29
31
30
use embedded_graphics_framebuf:: FrameBuf ;
32
-
33
- use esp_bsp :: lcd_gpios ;
31
+ use embedded_hal :: delay :: DelayNs ;
32
+ use log :: info ;
34
33
35
34
// Define grid size
36
35
const WIDTH : usize = 64 ;
@@ -68,7 +67,7 @@ fn randomize_grid(rng: &mut Rng, grid: &mut [[bool; WIDTH]; HEIGHT]) {
68
67
for cell in row. iter_mut ( ) {
69
68
// Read a single byte from the RNG
70
69
let mut buf = [ 0u8 ; 1 ] ;
71
- rng. read ( & mut buf) . unwrap ( ) ;
70
+ rng. read ( & mut buf) ;
72
71
73
72
// Set the cell to be alive or dead based on the random byte
74
73
* cell = buf[ 0 ] & 1 != 0 ;
@@ -159,64 +158,21 @@ fn draw_grid<D: DrawTarget<Color = Rgb565>>(
159
158
160
159
#[ entry]
161
160
fn main ( ) -> ! {
162
- let peripherals = Peripherals :: take ( ) ;
163
- let system = peripherals. SYSTEM . split ( ) ;
164
-
165
- // let clocks = ClockControl::max(system.clock_control).freeze();
166
- let clocks = ClockControl :: configure ( system. clock_control , CpuClock :: Clock240MHz ) . freeze ( ) ;
167
- let mut delay = Delay :: new ( & clocks) ;
168
-
169
- println ! ( "About to initialize the SPI LED driver" ) ;
170
- let io = IO :: new ( peripherals. GPIO , peripherals. IO_MUX ) ;
171
- let ( lcd_sclk, lcd_mosi, lcd_cs, lcd_miso, lcd_dc, mut lcd_backlight, lcd_reset) =
172
- lcd_gpios ! ( BoardType :: ESP32S3Box , io) ;
173
-
174
- let dma = Gdma :: new ( peripherals. DMA ) ;
175
- let dma_channel = dma. channel0 ;
176
-
177
- let mut descriptors = [ 0u32 ; 8 * 3 ] ;
178
- let mut rx_descriptors = [ 0u32 ; 8 * 3 ] ;
179
-
180
- let spi = Spi :: new ( peripherals. SPI2 , 40u32 . MHz ( ) , SpiMode :: Mode0 , & clocks)
181
- . with_pins ( Some ( lcd_sclk) , Some ( lcd_mosi) , Some ( lcd_miso) , Some ( lcd_cs) )
182
- . with_dma ( dma_channel. configure (
183
- false ,
184
- & mut descriptors,
185
- & mut rx_descriptors,
186
- DmaPriority :: Priority0 ,
187
- ) ) ;
188
-
189
- println ! ( "SPI ready" ) ;
190
-
191
- let di = display_interface_spi_dma:: new_no_cs ( 320 * 240 * 2 , spi, lcd_dc) ;
192
-
193
- // ESP32-S3-BOX display initialization workaround: Wait for the display to power up.
194
- // If delay is 250ms, picture will be fuzzy.
195
- // If there is no delay, display is blank
196
- delay. delay_ms ( 500u32 ) ;
197
-
198
- let mut display = match mipidsi:: Builder :: ili9342c_rgb565 ( di)
199
- . with_display_size ( 320 , 240 )
200
- . with_orientation ( mipidsi:: Orientation :: PortraitInverted ( false ) )
201
- . with_color_order ( mipidsi:: ColorOrder :: Bgr )
202
- . init ( & mut delay, Some ( lcd_reset) )
203
- {
204
- Ok ( display) => display,
205
- Err ( _e) => {
206
- // Handle the error and possibly exit the application
207
- panic ! ( "Display initialization failed" ) ;
208
- }
209
- } ;
161
+ const LCD_MEMORY_SIZE : usize = 320 * 240 * 2 ;
162
+ let peripherals = esp_hal:: init ( esp_hal:: Config :: default ( ) ) ;
163
+ esp_println:: logger:: init_logger_from_env ( ) ;
210
164
211
- let _ = lcd_backlight. set_high ( ) ;
165
+ let spi = lcd_spi ! ( peripherals) ;
166
+ let di = lcd_display_interface ! ( peripherals, spi) ;
167
+ let mut delay = Delay :: new ( ) ;
168
+ delay. delay_ns ( 500_000u32 ) ;
212
169
213
- // setup logger
214
- // To change the log_level change the env section in .cargo/config.toml
215
- // or remove it and set ESP_LOGLEVEL manually before running cargo run
216
- // this requires a clean rebuild because of https://github.com/rust-lang/cargo/issues/10358
217
- esp_println:: logger:: init_logger_from_env ( ) ;
218
- log:: info!( "Logger is setup" ) ;
219
- println ! ( "Hello Conway!" ) ;
170
+ let mut display = lcd_display ! ( peripherals, di) . init ( & mut delay) . unwrap ( ) ;
171
+
172
+ // Use the `lcd_backlight_init` macro to turn on the backlight
173
+ lcd_backlight_init ! ( peripherals) ;
174
+
175
+ info ! ( "Hello Conway!" ) ;
220
176
221
177
let mut grid: [ [ bool ; WIDTH ] ; HEIGHT ] = [ [ false ; WIDTH ] ; HEIGHT ] ;
222
178
let mut rng = Rng :: new ( peripherals. RNG ) ;
0 commit comments