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(slice_as_chunks)]
#![feature(asm_experimental_arch)] #![feature(asm_experimental_arch)]
use core::ptr::addr_of;
use atmega_hal::{ use atmega_hal::{
Adc, Eeprom, Usart, Usart,
port::{Pin, mode},
spi::{self, Settings}, spi::{self, Settings},
usart::Baudrate, usart::Baudrate,
}; };
use embedded_hal::delay::DelayNs;
use panic_halt as _; use panic_halt as _;
use crate::{ use crate::{
display::{Display, Position, Rgb565}, display::{Display, Position},
peripherals::{Button, Knob}, peripherals::Button,
qoi::{MARIO, PRESS_BTN, draw_image}, qoi::{Image, LARGE_CAT_UNSAFE, draw_image},
}; };
mod display; mod display;
@ -78,19 +78,9 @@ fn main() -> ! {
let mut button = Button::from(pins.pd5.into_pull_up_input()); let mut button = Button::from(pins.pd5.into_pull_up_input());
let mut idx = 0; let mut idx = 0;
let images = [&MARIO.as_slice()]; let images = [&Image::from(addr_of!(LARGE_CAT_UNSAFE))];
let len = images.len(); 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 { loop {
if button.poll() { if button.poll() {
idx = (idx + 1) % len; idx = (idx + 1) % len;

View File

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