From a634c9230c4ffa70d38647ec8a62ef0b68992fcf Mon Sep 17 00:00:00 2001 From: Sofia Date: Fri, 12 Sep 2025 22:52:42 +0300 Subject: [PATCH] Make interactive slideshow --- images/mario.qoi | Bin 0 -> 161 bytes images/mario.xcf | Bin 0 -> 2977 bytes images/pikachu.qoi | Bin 0 -> 241 bytes images/press_btn.qoi | Bin 0 -> 174 bytes src/main.rs | 58 ++++++++++++++---------------- src/qoi.rs | 82 ++++--------------------------------------- 6 files changed, 33 insertions(+), 107 deletions(-) create mode 100644 images/mario.qoi create mode 100644 images/mario.xcf create mode 100644 images/pikachu.qoi create mode 100644 images/press_btn.qoi diff --git a/images/mario.qoi b/images/mario.qoi new file mode 100644 index 0000000000000000000000000000000000000000..296854ccf6ffca94d048adbfd6a6f4d827bb5995 GIT binary patch literal 161 zcmX9&!3_dI2*gDGR|Sy{{7}ZVVB$-!3rnyZVE0x_uo$y(h-3&eFc7xe^+H5T<&oAl zKPT$);^fixwvb`T=Ec$)*D&B^Wmh(JsN{|_ U=<1rTV$gGW~)k8AZBjC#m1Y9{TBcBG7$nzOD`j#_dO%3Dh z9jPUi+OA`H%yRl;a|WBaI~>kT55!=f!7F&wSS09LCTX_m83V&JlEmeW&c@uHuvg}E zX1Pwc+N^6;W;%U$07~8Jvz@PNdUc~$?xc=AtHBzUjV#BQTqeen z_yf}!d1Iy5ymiKiOY3i}!MTi|nAnTE9+LVDiwAON1b=?$p!C*TjpiBl-=5okd+d5{ z{jN9a_h5-@p!q>S3Bej?M7SEPRz=2hYa@% zdL9x!?kKjrIFcsgQ*&s|y9yV%osC_tu-)mTtBgI5w;K&-RyPLjOS6tkt#M$}z~O2r zy-wpG&asp6+~)NI>$+HlrM}`J7@dpC;r$?{Bmz84 z?w$V<0(a7I?-BvFxYw`W;e2BC4RIh_Z>F}U-L5tN^@zUdEVg52T~?RXk;UjV4sT0w z(Pm2VyH%z9DASn2^ouIKe{0*uOnm+x7<=Ya7!)AlD;h&S7I@c;W*Pi>23IopN(QfG@Y}#*ytEVn#Mol2&k_tpJ{34df&L_e(|-aKZJAQCGnEvk z$q?~TO24l3@}2gH_CM419La|z@>ZB9)R&@#9~Fq?FCt%#Vn_~^{m4&hl*m4fs9zvO zn8|(|%6<`kQRw@Cl;nIGQeP%=^x6-j5RV^4GNDmEl%eIQ7)ppq!x$n)MBma)hO0Xlv{qILpch?z+sw`e8|EE2l&S^C>QZW*cRcT6+^HKRJ)0y_6sq%Z; K{@pD7Ya2 literal 0 HcmV?d00001 diff --git a/images/pikachu.qoi b/images/pikachu.qoi new file mode 100644 index 0000000000000000000000000000000000000000..413f5f6967e327a598c40b2120fe066588a886c0 GIT binary patch literal 241 zcmXYs%MAiC3`B#(!FRa$x=7v%rW9I0;)JMx17*+&CuhAMB-mysV8tYCBu5^_^E|m; z&Zm^pj5wHTVEL=NC!QVqb0&Ff&?hM#mi0hs@908OwReJ?=9SAT2 E0BG(#!~g&Q literal 0 HcmV?d00001 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, }, );