Successfully render custom menu

This commit is contained in:
Sebastian H. Gabrielli 2024-12-28 19:05:58 +01:00
parent 09a920e610
commit 083263e19e
2 changed files with 8 additions and 75 deletions

View File

@ -35,5 +35,7 @@ embedded-graphics = "0.8.1"
ili9341 = "0.6.0" ili9341 = "0.6.0"
display-interface-spi = "0.5.0" display-interface-spi = "0.5.0"
gui = { path="../gui/" }
[profile.release] [profile.release]
debug = 2 debug = 2

View File

@ -24,6 +24,7 @@ use embedded_graphics::{
}, },
text::{Alignment, Text}, text::{Alignment, Text},
}; };
use gui::*;
// Setup interrupts // Setup interrupts
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
@ -49,79 +50,6 @@ async fn blink(pin: AnyPin, blink_delay: Duration) -> ! {
} }
} }
/// Create a function to run the demo
///
/// # Arguments
/// This function takes in a variable of a generic type `D`.
/// With the restriction that the generic type `D` implements
/// `DrawTarget`, aka that it is a display.
///
/// I've also said it needs to use a certain colormode, because
/// that is the color mode I use in the demo.
///
/// While this may seem a little clunky this function now accepts _any_
/// display as long as it uses the standard 565 colormode.
///
/// # Returns
/// The function returns a `Result` containing either an empty tuple
/// or the error type associated with the generic type `D`.
fn display_demo<D>(display: &mut D) -> Result<(), D::Error>
where
D: DrawTarget<Color = Rgb565>,
{
display.clear(Rgb565::CSS_DARK_OLIVE_GREEN)?;
// Create styles used by the drawing operations.
let thin_stroke = PrimitiveStyle::with_stroke(Rgb565::WHITE, 1);
let thick_stroke = PrimitiveStyle::with_stroke(Rgb565::WHITE, 3);
let border_stroke = PrimitiveStyleBuilder::new()
.stroke_color(Rgb565::WHITE)
.stroke_width(3)
.stroke_alignment(StrokeAlignment::Inside)
.build();
let fill = PrimitiveStyle::with_fill(Rgb565::WHITE);
let character_style = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
let yoffset = 10;
// Draw a 3px wide outline around the display.
display
.bounding_box()
.into_styled(border_stroke)
.draw(display)?;
// 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(display)?;
// Draw a filled square
Rectangle::new(Point::new(52, yoffset), Size::new(16, 16))
.into_styled(fill)
.draw(display)?;
// Draw a circle with a 3px wide stroke.
Circle::new(Point::new(88, yoffset), 17)
.into_styled(thick_stroke)
.draw(display)?;
// Draw centered text.
let text = "embedded-graphics";
Text::with_alignment(
text,
display.bounding_box().center() + Point::new(0, 15),
character_style,
Alignment::Center,
)
.draw(display)?;
Ok(())
}
#[embassy_executor::main] #[embassy_executor::main]
async fn main(spawner: Spawner) { async fn main(spawner: Spawner) {
// Initialize the nRF // Initialize the nRF
@ -177,7 +105,7 @@ async fn main(spawner: Spawner) {
//////////////////////// ////////////////////////
// Some options for the display // Some options for the display
let display_orientation = ili9341::Orientation::Landscape; let display_orientation = ili9341::Orientation::PortraitFlipped;
let display_size = ili9341::DisplaySize240x320; let display_size = ili9341::DisplaySize240x320;
// Create the display // Create the display
@ -190,9 +118,12 @@ async fn main(spawner: Spawner) {
) )
.unwrap(); .unwrap();
// Create the GUI
let mut gui = Gui::new(display);
gui.draw().unwrap();
// Now loop, re-running the demo every second // Now loop, re-running the demo every second
loop { loop {
display_demo(&mut display).expect("Failed to run display demo");
Timer::after_secs(1).await; Timer::after_secs(1).await;
} }
} }