New screen

This commit is contained in:
eraden 2024-09-26 06:54:01 +02:00
parent e2d322c9eb
commit 91e75d37a0
4 changed files with 86 additions and 50 deletions

View File

@ -25,7 +25,8 @@ impl App for MazeGame {
BinaryMapVisitor.format(&mut grid, &mut self.map.0); 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() let wall_style = PrimitiveStyleBuilder::new()
.stroke_color(TriColor::Black) .stroke_color(TriColor::Black)
.stroke_width(3) .stroke_width(3)
@ -60,15 +61,16 @@ impl App for MazeGame {
), ),
Size::new(1, 1), 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<Action> {
let Some(button) = ctx.button_pressed else { 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) => { Button::Up if self.map.can_move(self.player, Direction::North) => {
self.player.1 -= 1; self.player.1 -= 1;
} }
@ -81,9 +83,9 @@ impl App for MazeGame {
Button::Right if self.map.can_move(self.player, Direction::East) => { Button::Right if self.map.can_move(self.player, Direction::East) => {
self.player.0 += 1; self.player.0 += 1;
} }
Button::Back => return Action::GoToMenu, Button::Back => {}
_ => {} _ => {}
}; });
Action::Noop None
} }
} }

View File

@ -42,33 +42,35 @@ impl Menu {
impl App for Menu { impl App for Menu {
fn start(&mut self, _trng: &mut Trng) {} fn start(&mut self, _trng: &mut Trng) {}
fn update(&mut self, ctx: &Context) -> Action { fn update(&mut self, ctx: &mut Context) -> Option<Action> {
let Some(button) = ctx.button_pressed else { let Some(button) = ctx.button_pressed else {
return Action::Noop; return None;
}; };
match button { match button {
Button::Up => match self.selected { Button::Up => match self.selected {
MenuEntry::Labirynth => return Action::Noop, MenuEntry::Labirynth => return None,
MenuEntry::Pairs => { MenuEntry::Pairs => {
self.selected = MenuEntry::Labirynth; self.selected = MenuEntry::Labirynth;
None
} }
}, },
Button::Down => match self.selected { Button::Down => match self.selected {
MenuEntry::Labirynth => { MenuEntry::Labirynth => {
self.selected = MenuEntry::Pairs; self.selected = MenuEntry::Pairs;
None
} }
MenuEntry::Pairs => return Action::Noop, MenuEntry::Pairs => None,
}, },
Button::Circle => match self.selected { Button::Circle => match self.selected {
MenuEntry::Labirynth => return Action::StartMaze, MenuEntry::Labirynth => return Some(Action::StartMaze),
MenuEntry::Pairs => return Action::StartPairs, MenuEntry::Pairs => return Some(Action::StartPairs),
}, },
_ => return Action::Noop, _ => None,
}; }
Action::Noop
} }
fn draw(&self, display: &mut Display290TriColor) { fn draw(&self, ctx: &mut Context) {
/*
let style = MonoTextStyle::new(&PROFONT_18_POINT, TriColor::Black); let style = MonoTextStyle::new(&PROFONT_18_POINT, TriColor::Black);
let selected_style = MonoTextStyle::new(&PROFONT_18_POINT, TriColor::Red); let selected_style = MonoTextStyle::new(&PROFONT_18_POINT, TriColor::Red);
@ -97,5 +99,6 @@ impl App for Menu {
.draw(display) .draw(display)
.unwrap(); .unwrap();
}); });
*/
} }
} }

View File

@ -17,7 +17,6 @@ pub mod maze_game;
pub mod menu; pub mod menu;
pub enum Action { pub enum Action {
Noop,
GoToMenu, GoToMenu,
StartMaze, StartMaze,
StartPairs, StartPairs,
@ -29,10 +28,12 @@ pub enum Application {
} }
impl Application { impl Application {
pub fn update(&mut self, ctx: &Context, trng: &mut Trng) { pub fn update(&mut self, ctx: &mut Context, trng: &mut Trng) {
let action = match self { let Some(action) = (match self {
Self::Menu(menu) => menu.update(ctx), Self::Menu(menu) => menu.update(ctx),
Self::Maze(maze) => maze.update(ctx), Self::Maze(maze) => maze.update(ctx),
}) else {
return;
}; };
match action { match action {
Action::StartMaze => { Action::StartMaze => {
@ -45,15 +46,14 @@ impl Application {
menu.start(trng); menu.start(trng);
*self = Application::Menu(menu); *self = Application::Menu(menu);
} }
Action::Noop => {}
Action::StartPairs => {} Action::StartPairs => {}
} }
} }
pub fn draw(&self, display: &mut Display290TriColor) { pub fn draw(&self, ctx: &mut Context) {
match self { match self {
Self::Menu(menu) => menu.draw(display), Self::Menu(menu) => menu.draw(ctx),
Self::Maze(maze) => maze.draw(display), Self::Maze(maze) => maze.draw(ctx),
} }
} }
} }
@ -61,7 +61,13 @@ impl Application {
pub trait App { pub trait App {
fn start(&mut self, trng: &mut Trng); 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<Action>;
fn draw_and_update(&mut self, ctx: &mut Context) -> Option<Action> {
let action = self.update(ctx);
self.draw(ctx);
action
}
} }

View File

@ -18,24 +18,28 @@ use esp_hal::{
clock::ClockControl, clock::ClockControl,
delay::Delay, delay::Delay,
gpio::{ gpio::{
Gpio10, Gpio2, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8, Input, Io, Level, Output, Pull, Gpio10, Gpio2, Gpio20, Gpio21, Gpio22, Gpio23, Gpio3, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8,
NO_PIN, Input, Io, Level, Output, Pull, NO_PIN,
}, },
peripherals::Peripherals, peripherals::Peripherals,
prelude::*, prelude::*,
rng::Trng, rng::Trng,
spi::{master::Spi, SpiMode}, spi::{master::Spi, FullDuplexMode, SpiMode},
system::SystemControl, system::SystemControl,
}; };
use esp_println::println; use esp_println::println;
use log::LevelFilter; use log::LevelFilter;
use profont::PROFONT_24_POINT;
// use weact_studio_epd::{graphics::Display290TriColor, TriColor};
// use weact_studio_epd::{graphics::DisplayRotation, WeActStudio290TriColorDriver};
mod apps; 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<SpiBus<'b>, CS<'b>, Delay>;
type Driver<'b> = EPD3in7<SPI<'b>, BUS<'b>, DC<'b>, RST<'b>, Delay>;
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
let peripherals = Peripherals::take(); let peripherals = Peripherals::take();
@ -61,12 +65,13 @@ fn main() -> ! {
let rst = io.pins.gpio22; // D4 / GPIO22 let rst = io.pins.gpio22; // D4 / GPIO22
let busy = io.pins.gpio23; // D5 / GPIO23 let busy = io.pins.gpio23; // D5 / GPIO23
let spi_bus = Spi::new(peripherals.SPI2, 4_000.kHz(), SpiMode::Mode0, &clocks).with_pins( let spi_bus: SpiBus = Spi::new(peripherals.SPI2, 4_000.kHz(), SpiMode::Mode0, &clocks)
Some(sclk), .with_pins(
Some(mosi), Some(sclk),
NO_PIN, Some(mosi),
NO_PIN, // cs is handled by the exclusive device NO_PIN,
); NO_PIN, // cs is handled by the exclusive device
);
// Convert pins into InputPins and OutputPins // Convert pins into InputPins and OutputPins
/* /*
@ -75,15 +80,17 @@ fn main() -> ! {
DC: OutputPin, DC: OutputPin,
RST: OutputPin, RST: OutputPin,
*/ */
let cs = Output::new(cs, Level::High);
let busy = Input::new(busy, esp_hal::gpio::Pull::Up); let cs: CS = Output::new(cs, Level::High);
let dc = Output::new(dc, Level::Low); 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); let rst = Output::new(rst, Level::High);
log::info!("Intializing SPI Device..."); 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 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"); EPD3in7::new(&mut spi, busy, dc, rst, &mut delay, None).expect("eink initalize error");
let mut display = Display3in7::default(); let mut display = Display3in7::default();
@ -98,7 +105,7 @@ fn main() -> ! {
let text_style = TextStyleBuilder::new().baseline(Baseline::Top).build(); let text_style = TextStyleBuilder::new().baseline(Baseline::Top).build();
let _ = let _ =
Text::with_text_style("Witamy!", Point::new(90, 10), style, text_style).draw(&mut display); 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) .update_and_display_frame(&mut spi, display.buffer(), &mut delay)
.expect("display frame new graphics"); .expect("display frame new graphics");
delay.delay_ms(500); delay.delay_ms(500);
@ -106,7 +113,7 @@ fn main() -> ! {
// epd2in13 // epd2in13
// .set_refresh(&mut spi, &mut delay, RefreshLut::Quick) // .set_refresh(&mut spi, &mut delay, RefreshLut::Quick)
// .unwrap(); // .unwrap();
epd2in13.clear_frame(&mut spi, &mut delay).unwrap(); driver.clear_frame(&mut spi, &mut delay).unwrap();
let mut kbd = Keyboard { let mut kbd = Keyboard {
rows: Rows { rows: Rows {
@ -125,6 +132,10 @@ fn main() -> ! {
let mut ctx = Context { let mut ctx = Context {
button_pressed: None, button_pressed: None,
driver,
display,
spi,
delay,
}; };
let mut app = Application::Menu(Menu::new()); let mut app = Application::Menu(Menu::new());
let mut trng = Trng::new(peripherals.RNG, peripherals.ADC1); let mut trng = Trng::new(peripherals.RNG, peripherals.ADC1);
@ -132,16 +143,26 @@ fn main() -> ! {
//app.draw(&mut display); //app.draw(&mut display);
//driver.full_update(&display).unwrap(); //driver.full_update(&display).unwrap();
//driver.sleep().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 { loop {
ctx.button_pressed = kbd.pressed(); ctx.button_pressed = kbd.pressed();
if !ctx.button_pressed.is_none() { if !ctx.button_pressed.is_none() {
log::info!("Wake up!"); log::info!("Wake up!");
//driver.wake_up().unwrap(); ctx.driver.wake_up(&mut ctx.spi, &mut delay).unwrap();
//display.clear(TriColor::White); //display.clear(TriColor::White);
//app.update(&ctx, &mut trng); //app.update(&ctx, &mut trng);
// app.draw(&mut display); // app.draw(&mut display);
//driver.full_update(&display).unwrap(); //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..."); log::info!("Sleeping for 100ms...");
@ -182,8 +203,12 @@ pub enum Button {
Back, Back,
} }
pub struct Context { pub struct Context<'d> {
button_pressed: Option<Button>, pub button_pressed: Option<Button>,
pub display: Display3in7,
pub driver: Driver<'d>,
pub spi: SPI<'d>,
pub delay: Delay,
} }
struct Rows<'d> { struct Rows<'d> {