Make interactive slideshow
This commit is contained in:
parent
5c88cf9086
commit
a634c9230c
BIN
images/mario.qoi
Normal file
BIN
images/mario.qoi
Normal file
Binary file not shown.
BIN
images/mario.xcf
Normal file
BIN
images/mario.xcf
Normal file
Binary file not shown.
BIN
images/pikachu.qoi
Normal file
BIN
images/pikachu.qoi
Normal file
Binary file not shown.
BIN
images/press_btn.qoi
Normal file
BIN
images/press_btn.qoi
Normal file
Binary file not shown.
58
src/main.rs
58
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::<CoreClock>::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);
|
||||
}
|
||||
}
|
||||
|
82
src/qoi.rs
82
src/qoi.rs
@ -16,58 +16,10 @@ use crate::{
|
||||
display::{Color, Display, Position, Rgb565, Writeable},
|
||||
};
|
||||
|
||||
#[repr(u8)]
|
||||
enum NonBssByte {
|
||||
V = 1,
|
||||
}
|
||||
|
||||
pub struct NonBss<Buf> {
|
||||
_hack: NonBssByte,
|
||||
pub buf: Buf,
|
||||
}
|
||||
|
||||
impl<const N: usize> 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<const N: usize> {
|
||||
// index: usize,
|
||||
// }
|
||||
|
||||
// impl<const N: usize> Iterator for LargeIter<N> {
|
||||
// type Item = u8;
|
||||
|
||||
// fn next(&mut self) -> Option<Self::Item> {
|
||||
// if self.index >= N {
|
||||
// return None;
|
||||
// }
|
||||
// let old_idx = self.index;
|
||||
// self.index += 1;
|
||||
// Some(SHEEP[old_idx])
|
||||
// }
|
||||
// }
|
||||
|
||||
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],
|
||||
display: &mut Display<T, DCPin, RSTPin>,
|
||||
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<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
|
||||
|
||||
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,
|
||||
},
|
||||
);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user