diff --git a/images/mario.qoi b/images/mario.qoi new file mode 100644 index 0000000..296854c Binary files /dev/null and b/images/mario.qoi differ diff --git a/images/mario.xcf b/images/mario.xcf new file mode 100644 index 0000000..2a05e00 Binary files /dev/null and b/images/mario.xcf differ diff --git a/images/pikachu.qoi b/images/pikachu.qoi new file mode 100644 index 0000000..413f5f6 Binary files /dev/null and b/images/pikachu.qoi differ diff --git a/images/press_btn.qoi b/images/press_btn.qoi new file mode 100644 index 0000000..a9fe652 Binary files /dev/null and b/images/press_btn.qoi differ diff --git a/src/main.rs b/src/main.rs index 8b32267..e28435b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use panic_halt as _; use crate::{ display::{Display, Position, Rgb565}, peripherals::{Button, Knob}, - qoi::draw_image, + qoi::{MARIO, PIKA, PRESS_BTN, draw_image}, }; mod display; @@ -83,41 +83,35 @@ fn main() -> ! { adc, }; - let colors = [ - Rgb565::red(), - Rgb565::green(), - Rgb565::blue(), - Rgb565::cyan(), - Rgb565::magenta(), - Rgb565::yellow(), - ]; - let mut idx = 0; - let mut delay = atmega_hal::delay::Delay::::new(); - let mut last_input = 0f32; + let mut idx = 0; + let mut images = [&PIKA.as_slice(), &MARIO.as_slice()]; + let len = images.len(); + + match draw_image( + &mut serial, + &PRESS_BTN.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 { - let input = knob.poll(); - let button_poll = button.poll(); + if button.poll() { + idx = (idx + 1) % len; - if button_poll { - idx = (idx + 1) % colors.len(); + match draw_image( + &mut serial, + &images[idx], + &mut display, + Position { x: 0, y: 0 }, + ) { + Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(), + Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(), + } } - - // if last_input != input || button_poll { - // last_input = input; - // display.draw_rect( - // Position { x: 0, y: 0 }, - // Position { x: 200, y: 200 }, - // (colors[idx] * input).as_color(), - // ); - // } - - match draw_image(&mut serial, &mut display, Position { x: 0, y: 0 }) { - Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(), - Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(), - } - - delay.delay_ms(1000); } } diff --git a/src/qoi.rs b/src/qoi.rs index 2d7c05e..1e326cd 100644 --- a/src/qoi.rs +++ b/src/qoi.rs @@ -16,58 +16,10 @@ use crate::{ display::{Color, Display, Position, Rgb565, Writeable}, }; -#[repr(u8)] -enum NonBssByte { - V = 1, -} - -pub struct NonBss { - _hack: NonBssByte, - pub buf: Buf, -} - -impl NonBss<[u8; N]> { - pub const fn new(buf: [u8; N]) -> Self { - Self { - _hack: NonBssByte::V, - buf, - } - } -} - -macro_rules! non_bss_statics {( - $( - $(#$attr:tt)* - $pub:vis - static ref $NAME:ident : [$u8:ty] = *include_bytes!($($args:tt)*); - )* -) => ( - $( - - $(#$attr)* - $pub - const $NAME: &[u8] = { - static $NAME: NonBss<[$u8; ::core::include_bytes!($($args)*).len()]> = - NonBss::new(*::core::include_bytes!($($args)*)); - &$NAME.buf - }; - )* -)} - // https://qoiformat.org/qoi-specification.pdf -// static LARGE: [u8; 1966] = *include_bytes!("../images/large.qoi"); -// static SHEEP: [u8; 852] = *include_bytes!("../images/sheep.qoi"); - -// non_bss_statics! { -// static ref SHEEP: [u8] = *include_bytes!("../images/sheep.qoi"); -// // static ref LARGE: [u8] = *include_bytes!("../images/large.qoi"); -// } -static SHEEP: [u8; 1077] = *include_bytes!("../images/sheep.qoi"); - -// const LARGE_C1: [u8; 512] = LARGE.as_chunks().0[0]; -// const LARGE_C2: [u8; 512] = LARGE.as_chunks().0[1]; -// const LARGE_C3: [u8; 512] = LARGE.as_chunks().0[2]; -// const LARGE_C4: &[u8] = LARGE.as_chunks::<512>().1; +pub static MARIO: [u8; 161] = *include_bytes!("../images/mario.qoi"); +pub static PIKA: [u8; 241] = *include_bytes!("../images/pikachu.qoi"); +pub static PRESS_BTN: [u8; 174] = *include_bytes!("../images/press_btn.qoi"); #[derive(Debug, uDebug)] pub enum QoiErr { @@ -75,33 +27,13 @@ pub enum QoiErr { UnexpectedEOF, } -// #[derive(Default)] -// struct LargeIter { -// index: usize, -// } - -// impl Iterator for LargeIter { -// type Item = u8; - -// fn next(&mut self) -> Option { -// if self.index >= N { -// return None; -// } -// let old_idx = self.index; -// self.index += 1; -// Some(SHEEP[old_idx]) -// } -// } - pub fn draw_image( serial: &mut Usart, Pin, CoreClock>, + buffer: &[u8], display: &mut Display, position: Position, ) -> Result<(), QoiErr> { - // let a = LARGE[black_box(50)]; - // ufmt::uwriteln!(serial, "Successfully read QOI header {}", a).unwrap(); - - let mut iter = SHEEP.iter().map(|v| *v); + let mut iter = buffer.iter().map(|b| *b); if let (Some(113), Some(111), Some(105), Some(102)) = (iter.next(), iter.next(), iter.next(), iter.next()) @@ -127,13 +59,13 @@ pub fn draw_image( ufmt::uwriteln!(serial, "Successfully read QOI header").unwrap(); - let scale_factor = 10; + let scale_factor = 240 / width; display.set_window( position, Position { x: position.x + (width * scale_factor) - 1, - y: position.y + (width * scale_factor) - 1, + y: position.y + (height * scale_factor) - 1, }, );