Add more comments

This commit is contained in:
Sebastian H. Gabrielli 2024-12-21 11:09:21 +01:00
parent 58f37cb2e8
commit 3778eaa9da

View File

@ -46,59 +46,11 @@ async fn blink(pin: AnyPin, blink_delay: Duration) -> ! {
} }
} }
#[embassy_executor::main] fn display_demo<D>(display: &mut D) -> Result<(), D::Error>
async fn main(spawner: Spawner) { where
// Initialize the nRF D: DrawTarget<Color = Rgb565>,
let p = embassy_nrf::init(Default::default()); {
display.clear(Rgb565::CSS_DARK_OLIVE_GREEN)?;
// Spawn the blink task
spawner
.spawn(blink(p.P0_02.degrade(), Duration::from_millis(250)))
.expect("Failed to start blink task");
// Get the pins for SPI
let mosi = p.P0_13;
let miso = p.P0_12;
let sck = p.P0_30;
let cs_pin = p.P0_31;
// Create an SPI config
let mut spim_config = spim::Config::default();
spim_config.frequency = spim::Frequency::M8;
///////////////////////////////////////////
// Create a new SPI interface on the nrf //
///////////////////////////////////////////
// Grab the SPI Master mode peripheral form the nRF60
let spim = embassy_nrf::spim::Spim::new(p.SERIAL3, Irqs, sck, miso, mosi, spim_config);
// Use the SPI peripheral to create a shared bus
let spi_bus: Mutex<embassy_sync::blocking_mutex::raw::NoopRawMutex, _> =
Mutex::new(RefCell::new(spim));
// Setup the chip select as an output pin
let chip_select = Output::new(cs_pin, Level::Low, OutputDrive::Standard);
// Use the shared bus to create a SPI Device to use for the display
let spi_display = SpiDevice::new(&spi_bus, chip_select);
// Create a new SPI display interface
let direction_control = Output::new(p.P0_20, Level::High, OutputDrive::Standard);
let iface = SPIInterface::new(spi_display, direction_control);
// Create a new display
let display_reset = Output::new(p.P0_11, Level::High, OutputDrive::Standard);
let display_orientation = ili9341::Orientation::Landscape;
let display_size = ili9341::DisplaySize240x320;
let mut display = Ili9341::new(
iface,
display_reset,
&mut embassy_time::Delay,
display_orientation,
display_size,
)
.unwrap();
display.clear(Rgb565::CSS_DARK_OLIVE_GREEN).unwrap();
// Create styles used by the drawing operations. // Create styles used by the drawing operations.
let thin_stroke = PrimitiveStyle::with_stroke(Rgb565::WHITE, 1); let thin_stroke = PrimitiveStyle::with_stroke(Rgb565::WHITE, 1);
@ -117,8 +69,7 @@ async fn main(spawner: Spawner) {
display display
.bounding_box() .bounding_box()
.into_styled(border_stroke) .into_styled(border_stroke)
.draw(&mut display) .draw(display)?;
.unwrap();
// Draw a triangle. // Draw a triangle.
Triangle::new( Triangle::new(
@ -127,20 +78,17 @@ async fn main(spawner: Spawner) {
Point::new(16 + 8, yoffset), Point::new(16 + 8, yoffset),
) )
.into_styled(thin_stroke) .into_styled(thin_stroke)
.draw(&mut display) .draw(display)?;
.unwrap();
// Draw a filled square // Draw a filled square
Rectangle::new(Point::new(52, yoffset), Size::new(16, 16)) Rectangle::new(Point::new(52, yoffset), Size::new(16, 16))
.into_styled(fill) .into_styled(fill)
.draw(&mut display) .draw(display)?;
.unwrap();
// Draw a circle with a 3px wide stroke. // Draw a circle with a 3px wide stroke.
Circle::new(Point::new(88, yoffset), 17) Circle::new(Point::new(88, yoffset), 17)
.into_styled(thick_stroke) .into_styled(thick_stroke)
.draw(&mut display) .draw(display)?;
.unwrap();
// Draw centered text. // Draw centered text.
let text = "embedded-graphics"; let text = "embedded-graphics";
@ -150,17 +98,77 @@ async fn main(spawner: Spawner) {
character_style, character_style,
Alignment::Center, Alignment::Center,
) )
.draw(&mut display) .draw(display)?;
Ok(())
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
// Initialize the nRF
let p = embassy_nrf::init(Default::default());
// Spawn the blink task
spawner
.spawn(blink(p.P0_02.degrade(), Duration::from_millis(250)))
.expect("Failed to start blink task");
////////////////
// Setup pins //
////////////////
let mosi = p.P0_13;
let miso = p.P0_12;
let sck = p.P0_30;
let cs_pin = p.P0_31;
let dc_pin = p.P0_20; // Data / Command pin for the display
let disp_rst_pin = p.P0_11;
// Initialize the CS, DC, and display reset pins as outputs
let chip_select = Output::new(cs_pin, Level::High, OutputDrive::Standard);
let direction_control = Output::new(dc_pin, Level::High, OutputDrive::Standard);
let display_reset = Output::new(disp_rst_pin, Level::High, OutputDrive::Standard);
///////////////
// Setup SPI //
///////////////
// Create an SPI config with the SPI frequency at 8MHz
let mut spim_config = spim::Config::default();
spim_config.frequency = spim::Frequency::M8;
// Grab the SPI Master peripheral form the nRF60
let spim = embassy_nrf::spim::Spim::new(p.SERIAL3, Irqs, sck, miso, mosi, spim_config);
// Use the SPI peripheral to create a shared SPI bus
let spi_bus: Mutex<embassy_sync::blocking_mutex::raw::NoopRawMutex, _> =
Mutex::new(RefCell::new(spim));
// Use the shared bus to create a SPI Device to use for the display
let spi_display = SpiDevice::new(&spi_bus, chip_select);
// Create a new SPI display interface
let iface = SPIInterface::new(spi_display, direction_control);
////////////////////////
// Create the display //
////////////////////////
// Some options for the display
let display_orientation = ili9341::Orientation::Landscape;
let display_size = ili9341::DisplaySize240x320;
// Create the display
let mut display = Ili9341::new(
iface,
display_reset,
&mut embassy_time::Delay,
display_orientation,
display_size,
)
.unwrap(); .unwrap();
let mut led = Output::new(p.P0_03, Level::Low, OutputDrive::Standard); // Now loop, re-running the demo every second
loop { loop {
info!("Going high!"); display_demo(&mut display).expect("Failed to run display demo");
led.set_high(); Timer::after_secs(1).await;
Timer::after_millis(300).await;
info!("Going low!");
led.set_low();
Timer::after_millis(300).await;
} }
} }