From 91e75d37a0f693e1bded63bb8372a7917fb4a7c4 Mon Sep 17 00:00:00 2001 From: eraden Date: Thu, 26 Sep 2024 06:54:01 +0200 Subject: [PATCH] New screen --- src/apps/maze_game.rs | 18 ++++++----- src/apps/menu.rs | 23 ++++++++------ src/apps/mod.rs | 24 +++++++++------ src/main.rs | 71 +++++++++++++++++++++++++++++-------------- 4 files changed, 86 insertions(+), 50 deletions(-) diff --git a/src/apps/maze_game.rs b/src/apps/maze_game.rs index 81a4e13..9477087 100644 --- a/src/apps/maze_game.rs +++ b/src/apps/maze_game.rs @@ -25,7 +25,8 @@ impl App for MazeGame { BinaryMapVisitor.format(&mut grid, &mut self.map.0); } - fn draw(&self, display: &mut Display290TriColor) { + fn draw(&self, ctx: &mut Context) { + /* let wall_style = PrimitiveStyleBuilder::new() .stroke_color(TriColor::Black) .stroke_width(3) @@ -60,15 +61,16 @@ impl App for MazeGame { ), Size::new(1, 1), ); - p.draw_styled(&player_style, display).unwrap(); + p.draw_styled(&player_style, ctx.display).unwrap(); } + */ } - fn update(&mut self, ctx: &Context) -> Action { + fn update(&mut self, ctx: &mut Context) -> Option { let Some(button) = ctx.button_pressed else { - return Action::Noop; + return None; }; - match button { + Some(match button { Button::Up if self.map.can_move(self.player, Direction::North) => { self.player.1 -= 1; } @@ -81,9 +83,9 @@ impl App for MazeGame { Button::Right if self.map.can_move(self.player, Direction::East) => { self.player.0 += 1; } - Button::Back => return Action::GoToMenu, + Button::Back => {} _ => {} - }; - Action::Noop + }); + None } } diff --git a/src/apps/menu.rs b/src/apps/menu.rs index 5422e5b..dce6c67 100644 --- a/src/apps/menu.rs +++ b/src/apps/menu.rs @@ -42,33 +42,35 @@ impl Menu { impl App for Menu { fn start(&mut self, _trng: &mut Trng) {} - fn update(&mut self, ctx: &Context) -> Action { + fn update(&mut self, ctx: &mut Context) -> Option { let Some(button) = ctx.button_pressed else { - return Action::Noop; + return None; }; match button { Button::Up => match self.selected { - MenuEntry::Labirynth => return Action::Noop, + MenuEntry::Labirynth => return None, MenuEntry::Pairs => { self.selected = MenuEntry::Labirynth; + None } }, Button::Down => match self.selected { MenuEntry::Labirynth => { self.selected = MenuEntry::Pairs; + None } - MenuEntry::Pairs => return Action::Noop, + MenuEntry::Pairs => None, }, Button::Circle => match self.selected { - MenuEntry::Labirynth => return Action::StartMaze, - MenuEntry::Pairs => return Action::StartPairs, + MenuEntry::Labirynth => return Some(Action::StartMaze), + MenuEntry::Pairs => return Some(Action::StartPairs), }, - _ => return Action::Noop, - }; - Action::Noop + _ => None, + } } - fn draw(&self, display: &mut Display290TriColor) { + fn draw(&self, ctx: &mut Context) { + /* let style = MonoTextStyle::new(&PROFONT_18_POINT, TriColor::Black); let selected_style = MonoTextStyle::new(&PROFONT_18_POINT, TriColor::Red); @@ -97,5 +99,6 @@ impl App for Menu { .draw(display) .unwrap(); }); + */ } } diff --git a/src/apps/mod.rs b/src/apps/mod.rs index f80866d..e78aa65 100644 --- a/src/apps/mod.rs +++ b/src/apps/mod.rs @@ -17,7 +17,6 @@ pub mod maze_game; pub mod menu; pub enum Action { - Noop, GoToMenu, StartMaze, StartPairs, @@ -29,10 +28,12 @@ pub enum Application { } impl Application { - pub fn update(&mut self, ctx: &Context, trng: &mut Trng) { - let action = match self { + pub fn update(&mut self, ctx: &mut Context, trng: &mut Trng) { + let Some(action) = (match self { Self::Menu(menu) => menu.update(ctx), Self::Maze(maze) => maze.update(ctx), + }) else { + return; }; match action { Action::StartMaze => { @@ -45,15 +46,14 @@ impl Application { menu.start(trng); *self = Application::Menu(menu); } - Action::Noop => {} Action::StartPairs => {} } } - pub fn draw(&self, display: &mut Display290TriColor) { + pub fn draw(&self, ctx: &mut Context) { match self { - Self::Menu(menu) => menu.draw(display), - Self::Maze(maze) => maze.draw(display), + Self::Menu(menu) => menu.draw(ctx), + Self::Maze(maze) => maze.draw(ctx), } } } @@ -61,7 +61,13 @@ impl Application { pub trait App { fn start(&mut self, trng: &mut Trng); - fn draw(&self, display: &mut Display290TriColor); + fn draw(&self, display: &mut Context); - fn update(&mut self, ctx: &Context) -> Action; + fn update(&mut self, ctx: &mut Context) -> Option; + + fn draw_and_update(&mut self, ctx: &mut Context) -> Option { + let action = self.update(ctx); + self.draw(ctx); + action + } } diff --git a/src/main.rs b/src/main.rs index f0c8d3f..0c2b9a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,24 +18,28 @@ use esp_hal::{ clock::ClockControl, delay::Delay, gpio::{ - Gpio10, Gpio2, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Input, Io, Level, Output, Pull, - NO_PIN, + Gpio10, Gpio2, Gpio20, Gpio21, Gpio22, Gpio23, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, + Input, Io, Level, Output, Pull, NO_PIN, }, peripherals::Peripherals, prelude::*, rng::Trng, - spi::{master::Spi, SpiMode}, + spi::{master::Spi, FullDuplexMode, SpiMode}, system::SystemControl, }; use esp_println::println; use log::LevelFilter; -use profont::PROFONT_24_POINT; - -// use weact_studio_epd::{graphics::Display290TriColor, TriColor}; -// use weact_studio_epd::{graphics::DisplayRotation, WeActStudio290TriColorDriver}; mod apps; +type CS<'b> = Output<'b, Gpio20>; +type BUS<'b> = Input<'b, Gpio23>; +type RST<'b> = Output<'b, Gpio22>; +type DC<'b> = Output<'b, Gpio21>; +type SpiBus<'b> = Spi<'b, esp_hal::peripherals::SPI2, FullDuplexMode>; +type SPI<'b> = ExclusiveDevice, CS<'b>, Delay>; +type Driver<'b> = EPD3in7, BUS<'b>, DC<'b>, RST<'b>, Delay>; + #[entry] fn main() -> ! { let peripherals = Peripherals::take(); @@ -61,12 +65,13 @@ fn main() -> ! { let rst = io.pins.gpio22; // D4 / GPIO22 let busy = io.pins.gpio23; // D5 / GPIO23 - let spi_bus = Spi::new(peripherals.SPI2, 4_000.kHz(), SpiMode::Mode0, &clocks).with_pins( - Some(sclk), - Some(mosi), - NO_PIN, - NO_PIN, // cs is handled by the exclusive device - ); + let spi_bus: SpiBus = Spi::new(peripherals.SPI2, 4_000.kHz(), SpiMode::Mode0, &clocks) + .with_pins( + Some(sclk), + Some(mosi), + NO_PIN, + NO_PIN, // cs is handled by the exclusive device + ); // Convert pins into InputPins and OutputPins /* @@ -75,15 +80,17 @@ fn main() -> ! { DC: OutputPin, RST: OutputPin, */ - let cs = Output::new(cs, Level::High); - let busy = Input::new(busy, esp_hal::gpio::Pull::Up); - let dc = Output::new(dc, Level::Low); + + let cs: CS = Output::new(cs, Level::High); + let busy: BUS = Input::new(busy, esp_hal::gpio::Pull::Up); + let dc: DC = Output::new(dc, Level::Low); let rst = Output::new(rst, Level::High); log::info!("Intializing SPI Device..."); - let mut spi = ExclusiveDevice::new(spi_bus, cs, delay).expect("SPI device initialize error"); + let mut spi: SPI = + ExclusiveDevice::new(spi_bus, cs, delay).expect("SPI device initialize error"); // let spi_interface = SPIInterface::new(spi_device, dc); - let mut epd2in13 = + let mut driver = EPD3in7::new(&mut spi, busy, dc, rst, &mut delay, None).expect("eink initalize error"); let mut display = Display3in7::default(); @@ -98,7 +105,7 @@ fn main() -> ! { let text_style = TextStyleBuilder::new().baseline(Baseline::Top).build(); let _ = Text::with_text_style("Witamy!", Point::new(90, 10), style, text_style).draw(&mut display); - epd2in13 + driver .update_and_display_frame(&mut spi, display.buffer(), &mut delay) .expect("display frame new graphics"); delay.delay_ms(500); @@ -106,7 +113,7 @@ fn main() -> ! { // epd2in13 // .set_refresh(&mut spi, &mut delay, RefreshLut::Quick) // .unwrap(); - epd2in13.clear_frame(&mut spi, &mut delay).unwrap(); + driver.clear_frame(&mut spi, &mut delay).unwrap(); let mut kbd = Keyboard { rows: Rows { @@ -125,6 +132,10 @@ fn main() -> ! { let mut ctx = Context { button_pressed: None, + driver, + display, + spi, + delay, }; let mut app = Application::Menu(Menu::new()); let mut trng = Trng::new(peripherals.RNG, peripherals.ADC1); @@ -132,16 +143,26 @@ fn main() -> ! { //app.draw(&mut display); //driver.full_update(&display).unwrap(); //driver.sleep().unwrap(); + ctx.driver.clear_frame(&mut ctx.spi, &mut delay).unwrap(); + ctx.driver + .update_and_display_frame(&mut ctx.spi, ctx.display.buffer(), &mut delay) + .expect("display frame new graphics"); + let _ = ctx.driver.sleep(&mut ctx.spi, &mut delay); loop { ctx.button_pressed = kbd.pressed(); if !ctx.button_pressed.is_none() { log::info!("Wake up!"); - //driver.wake_up().unwrap(); + ctx.driver.wake_up(&mut ctx.spi, &mut delay).unwrap(); //display.clear(TriColor::White); //app.update(&ctx, &mut trng); // app.draw(&mut display); //driver.full_update(&display).unwrap(); + ctx.driver.clear_frame(&mut ctx.spi, &mut delay).unwrap(); + ctx.driver + .update_and_display_frame(&mut ctx.spi, ctx.display.buffer(), &mut delay) + .expect("display frame new graphics"); + let _ = ctx.driver.sleep(&mut ctx.spi, &mut delay); } log::info!("Sleeping for 100ms..."); @@ -182,8 +203,12 @@ pub enum Button { Back, } -pub struct Context { - button_pressed: Option