Compare commits

..

5 Commits

Author SHA1 Message Date
bc944008a8 Finish slideshow 2025-09-12 23:03:03 +03:00
a634c9230c Make interactive slideshow 2025-09-12 22:52:42 +03:00
5c88cf9086 Add cat to sheep.qoi and render it 2025-09-12 20:34:10 +03:00
98c1bf0840 Add scaling iterator 2025-09-12 20:18:47 +03:00
6e61b3df5c Make small tweaks 2025-09-12 19:45:13 +03:00
17 changed files with 93 additions and 281 deletions

95
Cargo.lock generated
View File

@ -68,16 +68,6 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cc"
version = "1.2.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65193589c6404eb80b450d618eaf9a2cafaaafd57ecce47370519ef674a7bd44"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.3"
@ -132,43 +122,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "723dce4e9f25b6e6c5f35628e144794e5b459216ed7da97b7c4b66cdb3fa82ca"
[[package]]
name = "find-msvc-tools"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fd99930f64d146689264c637b5af2f0233a933bef0d8570e2526bf9e083192d"
[[package]]
name = "hash32"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606"
dependencies = [
"byteorder",
]
[[package]]
name = "heapless"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
dependencies = [
"hash32",
"stable_deref_trait",
]
[[package]]
name = "mipidsi"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ba34dcbf61182ffa6992b5a4d9b566d5a99df127fd93f6d314213347329e92f"
dependencies = [
"embedded-graphics-core",
"embedded-hal 1.0.0",
"heapless",
"nb 1.1.0",
]
[[package]]
name = "nb"
version = "0.1.3"
@ -214,52 +167,6 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
]
[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
"semver-parser",
]
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "stackalloc"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b5b7088084b7f78305a42468c9a3693918620f0b8ec86260f1132dc0521e78"
dependencies = [
"cc",
"rustc_version",
]
[[package]]
name = "syn"
version = "1.0.109"
@ -279,9 +186,7 @@ dependencies = [
"avr-device",
"embedded-graphics-core",
"embedded-hal 1.0.0",
"mipidsi",
"panic-halt",
"stackalloc",
"ufmt",
]

View File

@ -10,9 +10,7 @@ embedded-hal = "1.0.0"
atmega-hal = { git = "https://github.com/Rahix/avr-hal?tab=readme-ov-file", rev="cd3edea", version = "0.1.0", features=["atmega328p"] }
ufmt = "0.2.0"
mipidsi = "0.9.0"
embedded-graphics-core = "0.4.0"
stackalloc = { version = "1.2.1", features = ["no_std"] }
# nb = "1.1.0"
# pwm-pca9685 = "1.0.0"
# infrared = "0.14.1"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
images/mario.qoi Normal file

Binary file not shown.

BIN
images/pikachu.qoi Normal file

Binary file not shown.

BIN
images/press_btn.qoi Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
images/steve.qoi Normal file

Binary file not shown.

View File

@ -6,6 +6,7 @@
#![feature(iter_array_chunks)]
#![feature(const_slice_make_iter)]
#![feature(slice_as_chunks)]
#![feature(generic_const_exprs)]
use atmega_hal::{
Adc, Eeprom, Usart,
@ -19,7 +20,7 @@ use panic_halt as _;
use crate::{
display::{Display, Position, Rgb565},
peripherals::{Button, Knob},
qoi::draw_image,
qoi::{MARIO, PIKA, PRESS_BTN, STEVE, draw_image},
};
mod display;
@ -48,7 +49,7 @@ fn main() -> ! {
// ufmt::uwriteln!(serial, "Eeprom capacity: {}", eeprom.capacity()).unwrap();
let cs = pins.pb2.into_output();
let (mut spi, mut cs) = spi::Spi::new(
let (spi, cs) = spi::Spi::new(
dp.SPI,
pins.pb5.into_output(),
pins.pb3.into_output(),
@ -74,49 +75,35 @@ fn main() -> ! {
display.init();
let mut adc: Adc<CoreClock> = atmega_hal::Adc::new(dp.ADC, Default::default());
let mut button = Button::from(pins.pd5.into_pull_up_input());
let mut knob = Knob {
pin: pins.pc1.into_analog_input(&mut adc),
adc,
};
let colors = [
Rgb565::red(),
Rgb565::green(),
Rgb565::blue(),
Rgb565::cyan(),
Rgb565::magenta(),
Rgb565::yellow(),
];
let mut idx = 0;
let images = [&PIKA.as_slice(), &MARIO.as_slice(), &STEVE.as_slice()];
let len = images.len();
let mut delay = atmega_hal::delay::Delay::<CoreClock>::new();
let mut last_input = 0f32;
loop {
let input = knob.poll();
let button_poll = button.poll();
if button_poll {
idx = (idx + 1) % colors.len();
}
// 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 }) {
match draw_image(
&mut serial,
&PIKA.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(),
}
delay.delay_ms(1000);
loop {
if button.poll() {
idx = (idx + 1) % 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(),
}
}
}
}

View File

@ -16,57 +16,11 @@ 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");
}
// 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 STEVE: [u8; 187] = *include_bytes!("../images/steve.qoi");
pub static PRESS_BTN: [u8; 174] = *include_bytes!("../images/press_btn.qoi");
#[derive(Debug, uDebug)]
pub enum QoiErr {
@ -74,33 +28,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())
@ -121,56 +55,88 @@ pub fn draw_image<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
next(&mut iter)?,
next(&mut iter)?,
]) as u16;
let channels = next(&mut iter)?;
let colorspace = next(&mut iter)?;
let _channels = next(&mut iter)?;
let _colorspace = next(&mut iter)?;
ufmt::uwriteln!(serial, "Successfully read QOI header").unwrap();
let scale_factor = 240 / width;
display.set_window(
position,
Position {
x: position.x + width - 1,
y: position.y + height - 1,
x: position.x + (width * scale_factor) - 1,
y: position.y + (height * scale_factor) - 1,
},
);
let qoi_iter = QoiIterator::from(&mut iter, serial, width * height);
let qoi_iter = QoiIterator::from(&mut iter, width * height);
let scale_iter = ScaleIterator {
qoi: qoi_iter,
last_row: [Rgb565::yellow(); 64],
scale_factor: scale_factor as usize,
width: width as usize,
counter: 0,
index: 0,
};
// let mut chunks = qoi_iter.array_chunks::<BUFFER_SIZE>();
// while let Some(array) = chunks.next() {
// let mut colors = [0u8; BUFFER_SIZE * 2];
// for (i, pixel) in array.iter().enumerate() {
// let [c1, c2] = pixel.as_color().bytes;
// colors[i * 2] = c1;
// colors[i * 2 + 1] = c2;
// }
// display.write(Writeable::Data(&colors));
// }
let mut counter = 0u32;
for pixel in qoi_iter {
for pixel in scale_iter {
let [c1, c2] = pixel.as_color().bytes;
display.write(Writeable::Data(&[c1, c2]));
counter += 1;
}
ufmt::uwriteln!(serial, "Counter: {}", counter).unwrap();
// display.draw_rect(
// position,
// Position {
// x: position.x + 100,
// y: position.y + 100,
// },
// Rgb565::green().as_color(),
// );
Ok(())
} else {
Err(QoiErr::InvalidMagicNumber)
}
}
struct ScaleIterator<'a> {
qoi: QoiIterator<'a>,
last_row: [Rgb565; 64],
width: usize,
scale_factor: usize,
counter: usize,
index: usize,
}
impl<'a> Iterator for ScaleIterator<'a> {
type Item = Rgb565;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.width * self.scale_factor {
self.counter = (self.counter + 1) % self.scale_factor;
self.index = 0;
}
let index_div = self.index / self.scale_factor;
if self.counter % self.scale_factor == 0 {
if (self.index % self.scale_factor) == 0 {
if let Some(pixel) = self.qoi.next() {
self.last_row[index_div] = pixel;
self.index += 1;
Some(pixel)
} else {
None
}
} else {
let pixel = self.last_row[index_div];
self.index += 1;
Some(pixel)
}
} else {
let pixel = self.last_row[index_div];
self.index += 1;
Some(pixel)
}
}
}
struct QoiIterator<'a> {
inner: &'a mut dyn Iterator<Item = u8>,
serial: &'a mut Usart<USART0, Pin<mode::Input, PD0>, Pin<mode::Output, PD1>, CoreClock>,
prev_pixels: [Rgb565; 64],
last_pixel: Rgb565,
repeat: u8,
@ -179,14 +145,9 @@ struct QoiIterator<'a> {
}
impl<'a> QoiIterator<'a> {
fn from(
bytes: &'a mut dyn Iterator<Item = u8>,
serial: &'a mut Usart<USART0, Pin<mode::Input, PD0>, Pin<mode::Output, PD1>, CoreClock>,
expected: u16,
) -> Self {
fn from(bytes: &'a mut dyn Iterator<Item = u8>, expected: u16) -> Self {
QoiIterator {
inner: bytes,
serial,
prev_pixels: [Rgb565(0, 255, 0); 64],
last_pixel: Rgb565(0, 0, 0),
repeat: 0,
@ -201,7 +162,6 @@ impl<'a> Iterator for QoiIterator<'a> {
fn next(&mut self) -> Option<Self::Item> {
if self.parsed_colors >= self.expected_colors {
ufmt::uwriteln!(self.serial, "Reached expected").unwrap();
return None;
}
self.parsed_colors += 1;
@ -216,61 +176,35 @@ impl<'a> Iterator for QoiIterator<'a> {
let green = self.inner.next().unwrap();
let blue = self.inner.next().unwrap();
let _alpha = self.inner.next().unwrap();
// ufmt::uwriteln!(self.serial, "RGBA: {} {} {} {}", red, green, blue, alpha).unwrap();
Rgb565(red, green, blue)
} else if byte == 0b11111110 {
let red = self.inner.next().unwrap();
let green = self.inner.next().unwrap();
let blue = self.inner.next().unwrap();
// ufmt::uwriteln!(self.serial, "RGB: {} {} {}", red, green, blue).unwrap();
let red = self.inner.next()?;
let green = self.inner.next()?;
let blue = self.inner.next()?;
Rgb565(red, green, blue)
} else {
let tag = (0b11000000 & byte) >> 6;
let data = 0b00111111 & byte;
// ufmt::uwriteln!(self.serial, "Tag: {}", tag).unwrap();
if tag == 0 {
self.prev_pixels[data as usize]
} else if tag == 1 {
let dr = ((0b110000 & data) >> 4) as i8 - 2;
let dg = ((0b001100 & data) >> 2) as i8 - 2;
let db = (0b000011 & data) as i8 - 2;
// ufmt::uwriteln!(
// self.serial,
// "Diffs: {} {} {}",
// ((0b110000 & data) >> 4),
// ((0b001100 & data) >> 2),
// (0b000011 & data)
// )
// .unwrap();
Rgb565(
(self.last_pixel.0 as i8).wrapping_add(dr) as u8,
(self.last_pixel.1 as i8).wrapping_add(dg) as u8,
(self.last_pixel.2 as i8).wrapping_add(db) as u8,
)
} else if tag == 2 {
let second = self.inner.next().unwrap();
let second = self.inner.next()?;
let dg = data as i8 - 32;
let dr_dg = ((0b11110000 & second) >> 4) as i8 - 8;
let db_dg = (0b00001111 & second) as i8 - 8;
let dr = dr_dg + dg;
let db = db_dg + dg;
let a = self.last_pixel.0 as i8;
let b = self.last_pixel.1 as i8;
let c = self.last_pixel.2 as i8;
// ufmt::uwriteln!(self.serial, "Hello there! {} {} {}", a, b, c).unwrap();
// ufmt::uwriteln!(self.serial, "Values: {} {} {}", dr_dg, dg, db_dg).unwrap();
// ufmt::uwriteln!(self.serial, "Diffs: {} {} {}", dr, dg, db).unwrap();
// ufmt::uwriteln!(
// self.serial,
// "Res: {} {} {}",
// self.last_pixel.0 as i8 + dr,
// self.last_pixel.1 as i8 + dg,
// self.last_pixel.2 as i8 + db
// )
// .unwrap();
Rgb565(
(self.last_pixel.0 as i8 + dr) as u8,
(self.last_pixel.1 as i8 + dg) as u8,
@ -279,7 +213,6 @@ impl<'a> Iterator for QoiIterator<'a> {
} else if tag == 3 {
// QOI_OP_RUN
self.repeat = data;
// ufmt::uwriteln!(self.serial, "Repeat: {}", self.repeat).unwrap();
self.last_pixel
} else {
Rgb565::green()
@ -290,20 +223,9 @@ impl<'a> Iterator for QoiIterator<'a> {
((color.0 as u32 * 3 + color.1 as u32 * 5 + color.2 as u32 * 7 + 255 as u32 * 11)
% 64) as usize;
// ufmt::uwriteln!(
// self.serial,
// "Color: {} {} {} {}, Hash: {}",
// color.0,
// color.1,
// color.2,
// alpha,
// hash
// )
// .unwrap();
self.prev_pixels[hash] = color;
Some(color)
} else {
ufmt::uwriteln!(self.serial, "End reached").unwrap();
None
}
}