Make Image into a proper struct

This commit is contained in:
Sofia 2025-09-13 13:16:27 +03:00
parent ed3265f156
commit c1bd4acfd0
2 changed files with 31 additions and 45 deletions

View File

@ -8,19 +8,19 @@
#![feature(slice_as_chunks)]
#![feature(asm_experimental_arch)]
use core::ptr::addr_of;
use atmega_hal::{
Adc, Eeprom, Usart,
port::{Pin, mode},
Usart,
spi::{self, Settings},
usart::Baudrate,
};
use embedded_hal::delay::DelayNs;
use panic_halt as _;
use crate::{
display::{Display, Position, Rgb565},
peripherals::{Button, Knob},
qoi::{MARIO, PRESS_BTN, draw_image},
display::{Display, Position},
peripherals::Button,
qoi::{Image, LARGE_CAT_UNSAFE, draw_image},
};
mod display;
@ -78,19 +78,9 @@ fn main() -> ! {
let mut button = Button::from(pins.pd5.into_pull_up_input());
let mut idx = 0;
let images = [&MARIO.as_slice()];
let images = [&Image::from(addr_of!(LARGE_CAT_UNSAFE))];
let len = images.len();
match draw_image(
&mut serial,
&MARIO.as_slice(),
&mut display,
Position { x: 0, y: 0 },
) {
Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(),
Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(),
}
loop {
if button.poll() {
idx = (idx + 1) % len;

View File

@ -1,52 +1,52 @@
use core::{
arch::asm,
hint::black_box,
ptr::addr_of,
slice::{Chunks, Iter},
};
use core::{arch::asm, ptr::addr_of};
use atmega_hal::{
Atmega, Usart,
Usart,
pac::USART0,
port::{PB3, PB5, PD0, PD1, Pin, PinOps, mode},
port::{PD0, PD1, Pin, PinOps, mode},
};
use embedded_hal::delay::DelayNs;
use ufmt::derive::uDebug;
use crate::{
CoreClock,
display::{Color, Display, Position, Rgb565, Writeable},
qoi,
display::{Display, Position, Rgb565, Writeable},
};
// progmem! {
// /// Static byte stored in progmem!
// static progmem P_BYTE: u8 = 42;
// static progmem VERY_LARGE: [u8; 23731] = *include_bytes!("../images/cat.qoi");
// }
// https://qoiformat.org/qoi-specification.pdf
#[unsafe(link_section = ".progmem.data")]
static LARGE_CAT: [u8; 23731] = *include_bytes!("../images/cat.qoi");
pub static LARGE_CAT_UNSAFE: [u8; 23731] = *include_bytes!("../images/cat.qoi");
pub struct LargeIterator {
pub struct Image {
ptr: *const u8,
length: usize,
}
pub struct ImageIterator {
ptr: *const u8,
length: usize,
index: usize,
}
impl LargeIterator {
pub fn from<const N: usize>(addr: *const [u8; N]) -> LargeIterator {
LargeIterator {
impl Image {
pub const fn from<const N: usize>(addr: *const [u8; N]) -> Image {
Image {
ptr: addr.cast(),
length: N,
}
}
pub fn iter(&self) -> ImageIterator {
ImageIterator {
ptr: self.ptr,
length: self.length,
index: 0,
}
}
}
impl Iterator for LargeIterator {
impl Iterator for ImageIterator {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
@ -75,15 +75,11 @@ pub enum QoiErr {
pub fn draw_image<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
serial: &mut Usart<USART0, Pin<mode::Input, PD0>, Pin<mode::Output, PD1>, CoreClock>,
buffer: &[u8],
image: &Image,
display: &mut Display<T, DCPin, RSTPin>,
position: Position,
) -> Result<(), QoiErr> {
// let mut iter = buffer.iter().map(|b| *b);
let addr = addr_of!(LARGE_CAT);
let mut iter = LargeIterator::from(addr);
let mut iter = image.iter();
if let (Some(113), Some(111), Some(105), Some(102)) =
(iter.next(), iter.next(), iter.next(), iter.next())
{