embassy-display-test/src/main.rs

162 lines
5.0 KiB
Rust
Raw Normal View History

2024-12-15 20:49:22 +00:00
#![no_std]
#![no_main]
2024-12-20 16:47:22 +00:00
use core::cell::RefCell;
use defmt::info;
2024-12-15 21:40:00 +00:00
use display_interface_spi::SPIInterface;
2024-12-20 16:47:22 +00:00
use embassy_embedded_hal::shared_bus::blocking::spi::SpiDevice;
2024-12-15 20:49:22 +00:00
use embassy_executor::Spawner;
2024-12-20 16:47:22 +00:00
use embassy_nrf::{
bind_interrupts,
2024-12-21 09:30:14 +00:00
gpio::{AnyPin, Level, Output, OutputDrive, Pin},
2024-12-20 16:47:22 +00:00
peripherals,
spim::{self, Spim},
};
use embassy_sync::blocking_mutex::Mutex;
2024-12-21 09:30:14 +00:00
use embassy_time::{Delay, Duration, Timer};
use embedded_hal::digital::OutputPin;
2024-12-15 21:40:00 +00:00
use ili9341::Ili9341;
2024-12-15 20:49:22 +00:00
use {defmt_rtt as _, panic_probe as _};
2024-12-15 21:40:00 +00:00
use embedded_graphics::{
2024-12-20 16:47:22 +00:00
mono_font::{ascii::FONT_6X10, MonoTextStyle},
pixelcolor::{BinaryColor, Rgb565},
prelude::*,
primitives::{
2024-12-15 21:40:00 +00:00
Circle, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, StrokeAlignment, Triangle,
2024-12-20 16:47:22 +00:00
},
text::{Alignment, Text},
2024-12-15 21:40:00 +00:00
};
// Setup interrupts
bind_interrupts!(struct Irqs {
// Setup interrupts for SPI (SERIAL3)
UARTE3_SPIM3_SPIS3_TWIM3_TWIS3 => embassy_nrf::spim::InterruptHandler<peripherals::SERIAL3>;
});
2024-12-21 09:30:14 +00:00
#[embassy_executor::task]
async fn blink(pin: AnyPin, blink_delay: Duration) -> ! {
let mut led = Output::new(pin, Level::Low, OutputDrive::Standard);
loop {
led.set_high();
Timer::after(blink_delay).await;
led.set_low();
Timer::after(blink_delay).await;
}
}
2024-12-15 20:49:22 +00:00
#[embassy_executor::main]
2024-12-21 09:30:14 +00:00
async fn main(spawner: Spawner) {
// Initialize the nRF
2024-12-15 20:49:22 +00:00
let p = embassy_nrf::init(Default::default());
2024-12-15 21:40:00 +00:00
2024-12-21 09:30:14 +00:00
// 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;
2024-12-15 21:40:00 +00:00
// Create an SPI config
let mut spim_config = spim::Config::default();
spim_config.frequency = spim::Frequency::M8;
2024-12-20 16:47:22 +00:00
///////////////////////////////////////////
// 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));
2024-12-21 09:30:14 +00:00
// Setup the chip select as an output pin
let chip_select = Output::new(cs_pin, Level::Low, OutputDrive::Standard);
2024-12-20 16:47:22 +00:00
// Use the shared bus to create a SPI Device to use for the display
let spi_display = SpiDevice::new(&spi_bus, chip_select);
2024-12-15 21:40:00 +00:00
// Create a new SPI display interface
2024-12-21 09:30:14 +00:00
let direction_control = Output::new(p.P0_20, Level::High, OutputDrive::Standard);
2024-12-20 16:47:22 +00:00
let iface = SPIInterface::new(spi_display, direction_control);
2024-12-15 21:40:00 +00:00
// Create a new display
2024-12-21 09:30:14 +00:00
let display_reset = Output::new(p.P0_11, Level::High, OutputDrive::Standard);
2024-12-15 21:40:00 +00:00
let display_orientation = ili9341::Orientation::Landscape;
let display_size = ili9341::DisplaySize240x320;
2024-12-20 16:47:22 +00:00
let mut display = Ili9341::new(
iface,
display_reset,
&mut embassy_time::Delay,
display_orientation,
display_size,
)
.unwrap();
2024-12-15 21:40:00 +00:00
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();
2024-12-21 09:30:14 +00:00
let mut led = Output::new(p.P0_03, Level::Low, OutputDrive::Standard);
2024-12-15 20:49:22 +00:00
loop {
info!("Going high!");
led.set_high();
Timer::after_millis(300).await;
info!("Going low!");
led.set_low();
Timer::after_millis(300).await;
}
2024-12-20 16:47:22 +00:00
}