Fixing binary map

This commit is contained in:
Adrian Woźniak 2024-10-01 18:31:43 +02:00
parent 6e5d7a7fb0
commit 2e2bf8e35a
4 changed files with 143 additions and 40 deletions

View File

@ -29,6 +29,7 @@ pub const HEIGHT: u32 = 480;
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White; pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
const IS_BUSY_LOW: bool = false; const IS_BUSY_LOW: bool = false;
const IS_BUSY_HIGH: bool = true;
const SINGLE_BYTE_WRITE: bool = true; const SINGLE_BYTE_WRITE: bool = true;
@ -259,21 +260,30 @@ where
width: u32, width: u32,
height: u32, height: u32,
) -> Result<(), SPI::Error> { ) -> Result<(), SPI::Error> {
self.interface let i = &mut self.interface;
.cmd(spi, Command::SetRamXAddressStartEndPosition); i.cmd(spi, Command::SetRamXAddressStartEndPosition);
i.data(spi, &[(x >> 8) as u8])?;
let tmp = x & 0xf8;
i.data(spi, &[tmp as u8])?; // x should be the multiple of 8, the last 3 bit will always be ignored
let tmp = tmp + width - 1;
i.data(spi, &[(tmp >> 8) as u8])?;
i.data(spi, &[(tmp | 0x07) as u8])?;
self.interface.data(spi, &[x as u8 & 0xff]); i.cmd(spi, Command::SetRamYAddressStartEndPosition);
self.interface.data(spi, &[(x >> 8) as u8 & 0x03]); i.data(spi, &[(y >> 8) as u8])?;
self.interface.data(spi, &[(width - x) as u8 & 0xff]); i.data(spi, &[y as u8])?;
self.interface.data(spi, &[((width - x) as u8 >> 8) & 0x03]); i.data(spi, &[((y + height - 1) >> 8) as u8])?;
i.data(spi, &[(y + height - 1) as u8])?;
self.interface i.cmd_with_data(spi, Command::WriteRam, buffer);
.cmd(spi, Command::SetRamYAddressStartEndPosition);
self.interface.data(spi, y as u8 & 0xff); //load lut 2
self.interface.data(spi, (y as u8 >> 8) & 0x03); i.cmd_with_data(spi, Command::WriteLutRegister, &LUT_1GRAY_DU);
self.interface.data(spi, (height - y) as u8 & 0xff);
self.interface.data(spi, ((height - y) as u8 >> 8) & 0x03); i.cmd(spi, Command::DisplayUpdateSequence);
todo!() i.wait_until_idle(delay, IS_BUSY_LOW);
Ok(())
} }
fn display_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> { fn display_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {

View File

@ -409,7 +409,7 @@ impl<const X: usize, const Y: usize, const SIZE: usize> AsciiBroad<X, Y, SIZE> {
// const LINE_SIZE: usize = X * Self::CELL_SIZE + 1; // const LINE_SIZE: usize = X * Self::CELL_SIZE + 1;
// const BUFFER_SIZE: usize = Y + Self::CELL_SIZE * Self::LINE_SIZE; // const BUFFER_SIZE: usize = Y + Self::CELL_SIZE * Self::LINE_SIZE;
fn format(&self, grid: &Grid<X, Y, SIZE>, output: &mut impl Write) { pub fn format(&self, grid: &Grid<X, Y, SIZE>, output: &mut impl Write) {
write!(output, "#").unwrap(); write!(output, "#").unwrap();
(0..X).into_iter().for_each(|_| { (0..X).into_iter().for_each(|_| {
write!(output, "##").unwrap(); write!(output, "##").unwrap();
@ -456,10 +456,12 @@ impl<'s> BufWriter<'s> {
pub fn new(b: &'s mut [MazePart]) -> Self { pub fn new(b: &'s mut [MazePart]) -> Self {
Self(b, 0) Self(b, 0)
} }
pub fn push(&mut self, part: MazePart) { pub fn push(&mut self, part: MazePart) {
self.0[self.1] = part; self.0[self.1] = part;
self.1 += 1; self.1 += 1;
} }
pub fn copy(&mut self, src: &[MazePart]) { pub fn copy(&mut self, src: &[MazePart]) {
src.iter() src.iter()
.take_while(|p| **p != MazePart::Noop) .take_while(|p| **p != MazePart::Noop)
@ -678,6 +680,87 @@ mod print_tests {
let mut map = BinaryMap::<122, 122, 14884>::new(); let mut map = BinaryMap::<122, 122, 14884>::new();
BinaryMapVisitor.format(&mut grid, &mut map.0); BinaryMapVisitor.format(&mut grid, &mut map.0);
} }
#[test]
fn binary_map() {
let mut g = Grid::<5, 5, 25>::new();
g.cells[0] = Cell::EAST;
g.cells[1] = Cell::default();
g.cells[2] = Cell::EAST;
g.cells[3] = Cell::WEST;
g.cells[4] = Cell::default();
g.cells[5] = Cell::EAST | Cell::WEST;
g.cells[6] = Cell::EAST | Cell::WEST;
g.cells[7] = Cell::EAST | Cell::WEST;
g.cells[8] = Cell::EAST | Cell::WEST;
g.cells[9] = Cell::EAST | Cell::WEST;
g.cells[10] = Cell::EAST | Cell::WEST;
g.cells[11] = Cell::SOUTH;
g.cells[12] = Cell::NORTH;
g.cells[13] = Cell::WEST | Cell::SOUTH;
g.cells[14] = Cell::EAST | Cell::WEST;
g.cells[15] = Cell::WEST;
g.cells[16] = Cell::WEST | Cell::NORTH | Cell::SOUTH;
g.cells[17] = Cell::WEST | Cell::SOUTH;
g.cells[18] = Cell::EAST | Cell::NORTH;
g.cells[19] = Cell::EAST | Cell::WEST;
g.cells[20] = Cell::WEST | Cell::SOUTH;
g.cells[21] = Cell::SOUTH | Cell::NORTH;
g.cells[22] = Cell::SOUTH | Cell::NORTH;
g.cells[23] = Cell::SOUTH | Cell::EAST;
g.cells[24] = Cell::WEST | Cell::SOUTH;
let mut map = BinaryMap::<12, 12, 144>::new();
BinaryMapVisitor.format(&mut g, &mut map.0);
use MazePart::Passage as P;
use MazePart::Wall as W;
use MazePart::*;
#[derive(PartialEq)]
struct MB([MazePart; 144]);
impl std::fmt::Debug for MB {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\n");
for x in 0..12 {
for y in 0..12 {
let c = match self.0[y * 12 + x] {
P => ' ',
W => '#',
Noop => '?',
Player => 'O',
};
write!(f, "{c} ");
}
write!(f, "\n");
}
Ok(())
}
}
assert_eq!(
MB(map.0),
MB([
W, W, W, W, W, W, W, W, W, W, W, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, P, P, P, P, P, P, P, P, P, P, W, //
W, W, W, W, W, W, W, W, W, W, W, W,
])
);
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,12 +1,15 @@
use epd_waveshare::color::Color; use epd_waveshare::color::Color;
use maze::{BinaryMapVisitor, Direction}; use maze::{AsciiBroad, BinaryMapVisitor, Direction};
use crate::Button; use crate::Button;
use super::*; use super::*;
pub const MAZE_WIDTH: usize = 42;
pub const MAZE_HEIGHT: usize = 42;
pub struct MazeGame { pub struct MazeGame {
map: maze::BinaryMap<122, 122, 14884>, map: maze::BinaryMap<MAZE_WIDTH, MAZE_HEIGHT, 1764>,
player: Point, player: Point,
old_player: Option<Point>, old_player: Option<Point>,
} }
@ -22,8 +25,8 @@ impl MazeGame {
} }
impl MazeGame { impl MazeGame {
const X_OFFSET: i32 = 2; const X_OFFSET: i32 = 4;
const Y_OFFSET: i32 = 2; const Y_OFFSET: i32 = 4;
fn player_pos(&self) -> (u16, u16) { fn player_pos(&self) -> (u16, u16) {
(self.player.x as u16, self.player.y as u16) (self.player.x as u16, self.player.y as u16)
@ -36,14 +39,15 @@ impl MazeGame {
.fill_color(Color::Black) .fill_color(Color::Black)
.build(); .build();
for x in 0..122 { for x in 0..MAZE_WIDTH {
for y in 0..122 { for y in 0..MAZE_HEIGHT {
if self.map.at(x, y) == maze::MazePart::Wall { if self.map.at(x as u16, y as u16) == maze::MazePart::Wall {
let p = Rectangle::new( Rectangle::new(
Point::new(x as i32 + Self::X_OFFSET, y as i32 + Self::Y_OFFSET), Point::new(x as i32 * Self::X_OFFSET, y as i32 * Self::Y_OFFSET),
Size::new(1, 1), Size::new(3, 3),
); )
p.draw_styled(&wall_style, &mut ctx.epaper.display).unwrap(); .draw_styled(&wall_style, &mut ctx.epaper.display)
.unwrap();
} }
} }
} }
@ -56,23 +60,27 @@ impl MazeGame {
.fill_color(Color::Black) .fill_color(Color::Black)
.build(); .build();
let p = Rectangle::new( Rectangle::new(
Point::new( Point::new(
self.player.x + Self::X_OFFSET, self.player.x * Self::X_OFFSET,
self.player.y + Self::Y_OFFSET, self.player.y * Self::Y_OFFSET,
), ),
Size::new(1, 1), Size::new(1, 1),
); )
p.draw_styled(&player_style, &mut ctx.epaper.display) .draw_styled(&player_style, &mut ctx.epaper.display)
.unwrap(); .unwrap();
} }
} }
impl App for MazeGame { impl App for MazeGame {
fn start(&mut self, trng: &mut Trng) { fn start(&mut self, trng: &mut Trng) {
let mut grid = maze::Grid::<60, 60, 3600>::new(); let mut grid = maze::Grid::<20, 20, 400>::new();
maze::RecursiveDivision.generate(&mut grid, trng); maze::RecursiveDivision.generate(&mut grid, trng);
BinaryMapVisitor.format(&mut grid, &mut self.map.0); BinaryMapVisitor.format(&mut grid, &mut self.map.0);
let mut result = heapless::String::<500>::new();
AsciiBroad.format(&grid, &mut result);
println!("{result}");
} }
fn draw(&self, ctx: &mut Context) { fn draw(&self, ctx: &mut Context) {

View File

@ -14,8 +14,8 @@ use esp_hal::{
clock::ClockControl, clock::ClockControl,
delay::Delay, delay::Delay,
gpio::{ gpio::{
Gpio0, Gpio1, Gpio10, Gpio20, Gpio21, Gpio22, Gpio23, Gpio4, Gpio5, Gpio0, Gpio1, Gpio10, Gpio20, Gpio21, Gpio22, Gpio23, Gpio4, Gpio5, Gpio6, Gpio7, Gpio8,
Gpio6, Gpio7, Gpio8, Input, Io, Level, Output, OutputOpenDrain, Pull, Input, Io, Level, Output, OutputOpenDrain, Pull,
}, },
peripherals::Peripherals, peripherals::Peripherals,
prelude::*, prelude::*,
@ -152,10 +152,10 @@ impl Button {
10 => Button::D, 10 => Button::D,
11 => Button::Z, 11 => Button::Z,
// //
12 => Button::A, 12 => Button::M1,
13 => Button::A, 13 => Button::M2,
14 => Button::A, 14 => Button::M3,
15 => Button::A, 15 => Button::Back,
_ => return None, _ => return None,
}) })
} }
@ -203,7 +203,9 @@ impl<'d> Keypad<'d> {
.find(|(_idx, b)| *b) .find(|(_idx, b)| *b)
.and_then(|(idx, _)| Button::from_idx(idx)); .and_then(|(idx, _)| Button::from_idx(idx));
if c.is_some() {
println!("Button: {c:?}"); println!("Button: {c:?}");
}
c c
} }