diff --git a/Cargo.toml b/Cargo.toml index 26ec348..97112fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,5 +29,10 @@ embassy-nrf = { version = "0.2", features = ["defmt", "nrf9160-s", "time-driver- static_cell = {version = "2"} +# Used for display +embedded-graphics = "0.8.1" +ili9341 = "0.6.0" +display-interface-spi = "0.5.0" + [profile.release] debug = 2 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3b8de5d..4f8a3cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,104 @@ #![no_std] #![no_main] +use display_interface_spi::SPIInterface; use embassy_executor::Spawner; -use embassy_nrf::gpio::{Level, Output, OutputDrive}; +use embassy_nrf::{bind_interrupts, gpio::{Level, Output, OutputDrive}, peripherals, spim}; use embassy_time::Timer; +use ili9341::Ili9341; use {defmt_rtt as _, panic_probe as _}; use defmt::info; +use embedded_graphics::{ + mono_font::{ascii::FONT_6X10, MonoTextStyle}, pixelcolor::{BinaryColor, Rgb565}, prelude::*, primitives::{ + Circle, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, StrokeAlignment, Triangle, + }, text::{Alignment, Text} +}; + +// Setup interrupts +bind_interrupts!(struct Irqs { + // Setup interrupts for SPI (SERIAL3) + UARTE3_SPIM3_SPIS3_TWIM3_TWIS3 => embassy_nrf::spim::InterruptHandler; +}); + #[embassy_executor::main] async fn main(_spawner: Spawner) { let p = embassy_nrf::init(Default::default()); + + // Get the pins + let mosi = p.P0_31; + let miso = p.P0_30; + let sck = p.P0_20; + let cs_pin = p.P0_13; + + // 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 + let spi = embassy_nrf::spim::Spim::new(p.SERIAL3, Irqs, sck, miso, mosi, spim_config); + + // Create a new SPI display interface + let direction_control = Output::new(p.P0_07, Level::High, OutputDrive::Standard); + let chip_select = Output::new(p.P0_08, Level::Low, OutputDrive::Standard); + let iface = SPIInterface::new(spi, direction_control); + + // Create a new display + let display_reset = Output::new(p.P0_12, 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::RED).unwrap(); + + // // Create styles used by the drawing operations. + // let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1); + // let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3); + // let border_stroke = PrimitiveStyleBuilder::new() + // .stroke_color(BinaryColor::On) + // .stroke_width(3) + // .stroke_alignment(StrokeAlignment::Inside) + // .build(); + // let fill = PrimitiveStyle::with_fill(BinaryColor::On); + // let character_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On); + + // let yoffset = 10; + + // // Draw a 3px wide outline around the display. + // display + // .bounding_box() + // .into_styled(border_stroke) + // .draw(&mut display).unwrap(); + + // // Draw a triangle. + // Triangle::new( + // Point::new(16, 16 + yoffset), + // Point::new(16 + 16, 16 + yoffset), + // Point::new(16 + 8, yoffset), + // ) + // .into_styled(thin_stroke) + // .draw(&mut display).unwrap(); + + // // Draw a filled square + // Rectangle::new(Point::new(52, yoffset), Size::new(16, 16)) + // .into_styled(fill) + // .draw(&mut display).unwrap(); + + // // Draw a circle with a 3px wide stroke. + // Circle::new(Point::new(88, yoffset), 17) + // .into_styled(thick_stroke) + // .draw(&mut display).unwrap(); + + // // Draw centered text. + // let text = "embedded-graphics"; + // Text::with_alignment( + // text, + // display.bounding_box().center() + Point::new(0, 15), + // character_style, + // Alignment::Center, + // ) + // .draw(&mut display).unwrap(); + let mut led = Output::new(p.P0_02, Level::Low, OutputDrive::Standard); loop {