From ff8483dbaad01fd129f4bc880bdaaf0720c9043d Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Sat, 28 Dec 2024 19:36:20 +0100 Subject: [PATCH] Put the GUI drawing into task --- src/main.rs | 125 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 48 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8db1388..8e1cef0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ #![no_main] use core::cell::RefCell; +use defmt::info; use display_interface_spi::SPIInterface; use embassy_embedded_hal::shared_bus::blocking::spi::SpiDevice; use embassy_executor::Spawner; @@ -50,6 +51,70 @@ async fn blink(pin: AnyPin, blink_delay: Duration) -> ! { } } +#[embassy_executor::task] +async fn gui_task( + peripheral: embassy_nrf::peripherals::SERIAL3, + sck: AnyPin, + miso: AnyPin, + mosi: AnyPin, + display_reset_pin: AnyPin, + cs_pin: AnyPin, + dc_pin: AnyPin, +) -> ! { + /////////////// + // 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(peripheral, Irqs, sck, miso, mosi, spim_config); + // Use the SPI peripheral to create a shared SPI bus + let spi_bus: Mutex = + Mutex::new(RefCell::new(spim)); + + //////////////// + // Setup pins // + //////////////// + + // 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(display_reset_pin, Level::High, OutputDrive::Standard); + + //////////////////////// + // Create the display // + //////////////////////// + + // 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); + + // Some options for the display + let display_orientation = ili9341::Orientation::PortraitFlipped; + let display_size = ili9341::DisplaySize240x320; + + // Create the display + let display = Ili9341::new( + iface, + display_reset, + &mut embassy_time::Delay, + display_orientation, + display_size, + ) + .unwrap(); + + let mut gui = Gui::new(display); + + loop { + gui.draw().expect("Failed to draw GUI"); + info!("Finished drawing GUI"); + } +} + #[embassy_executor::main] async fn main(spawner: Spawner) { // Initialize the nRF @@ -65,10 +130,6 @@ async fn main(spawner: Spawner) { ) .expect("Failed to start blink task"); - //////////////// - // Setup pins // - //////////////// - let mosi = p.P0_13; let miso = p.P0_12; let sck = p.P0_30; @@ -76,54 +137,22 @@ async fn main(spawner: Spawner) { 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 = - 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::PortraitFlipped; - 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(); - // Create the GUI - let mut gui = Gui::new(display); - gui.draw().unwrap(); + spawner + .spawn(gui_task( + p.SERIAL3, + sck.degrade(), + miso.degrade(), + mosi.degrade(), + disp_rst_pin.degrade(), + cs_pin.degrade(), + dc_pin.degrade(), + )) + .expect("Failed to spawn GUI task"); // Now loop, re-running the demo every second loop { + info!("Hello, world!"); Timer::after_secs(1).await; } }