Update API again, rename QOI to graphics

This commit is contained in:
Sofia 2025-09-13 14:10:05 +03:00
parent 0ad742df48
commit e6a712bb96
3 changed files with 56 additions and 41 deletions

View File

@ -61,7 +61,7 @@ impl Mul<f32> for Rgb565 {
}
#[derive(Default, Clone, Copy)]
pub struct Position {
pub struct Vec2 {
pub x: u16,
pub y: u16,
}
@ -179,8 +179,8 @@ impl<T: DelayNs, DCPin: PinOps, RSTPin: PinOps> Display<T, DCPin, RSTPin> {
self.set_inversion(true);
self.write(Writeable::Command(Command::NoRon));
self.draw_rect(
Position { x: 0, y: 0 },
Position { x: 240, y: 240 },
Vec2 { x: 0, y: 0 },
Vec2 { x: 240, y: 240 },
Color::default(),
);
@ -246,18 +246,18 @@ impl<T: DelayNs, DCPin: PinOps, RSTPin: PinOps> Display<T, DCPin, RSTPin> {
self.write(Writeable::Data(&[start1, start2, end1, end2]));
}
pub fn set_window(&mut self, pos0: Position, pos1: Position) {
pub fn set_window(&mut self, pos0: Vec2, pos1: Vec2) {
self.set_columns(pos0.x, pos1.x);
self.set_rows(pos0.y, pos1.y);
self.write(Writeable::Command(Command::RamWR));
}
pub fn pixel(&mut self, position: Position, color: Color) {
pub fn pixel(&mut self, position: Vec2, color: Color) {
self.set_window(position, position);
self.write(Writeable::Data(&color.bytes));
}
pub fn draw_rect(&mut self, pos0: Position, pos1: Position, color: Color) {
pub fn draw_rect(&mut self, pos0: Vec2, pos1: Vec2, color: Color) {
self.set_window(pos0, pos1);
self.dc.set_high();
let width = pos1.x - pos0.x;

View File

@ -10,7 +10,7 @@ use ufmt::derive::uDebug;
use crate::{
CoreClock,
display::{Display, Position, Rgb565, Writeable},
display::{Display, Rgb565, Vec2, Writeable},
};
// https://qoiformat.org/qoi-specification.pdf
@ -79,7 +79,7 @@ pub fn draw_image<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
serial: &mut Usart<USART0, Pin<mode::Input, PD0>, Pin<mode::Output, PD1>, CoreClock>,
iter: &mut dyn Iterator<Item = u8>,
display: &mut Display<T, DCPin, RSTPin>,
position: Position,
position: Vec2,
) -> Result<(), QoiErr> {
if let (Some(113), Some(111), Some(105), Some(102)) =
(iter.next(), iter.next(), iter.next(), iter.next())
@ -98,40 +98,55 @@ pub fn draw_image<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
let scale_factor = 240 / width;
display.set_window(
let mut qoi_iter = QoiIterator::from(iter, width * height);
draw_stream(
&mut qoi_iter,
display,
position,
Position {
x: position.x + (width * scale_factor) - 1,
y: position.y + (height * scale_factor) - 1,
Vec2 {
x: width,
y: height,
},
);
let qoi_iter = QoiIterator::from(iter, width * height);
let scale_iter = ScaleIterator {
qoi: qoi_iter,
last_row: [Rgb565::yellow(); 120],
scale_factor: scale_factor as usize,
width: width as usize,
counter: 0,
index: 0,
};
let mut counter = 0u32;
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();
Ok(())
scale_factor,
)
} else {
Err(QoiErr::InvalidMagicNumber)
}
}
pub fn draw_stream<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
stream: &mut dyn Iterator<Item = Rgb565>,
display: &mut Display<T, DCPin, RSTPin>,
position: Vec2,
scale: Vec2,
scale_factor: u16,
) -> Result<(), QoiErr> {
let scale_iter = ScaleIterator {
iter: stream,
last_row: [Rgb565::yellow(); 120],
scale_factor: scale_factor as usize,
width: scale.x as usize,
counter: 0,
index: 0,
};
display.set_window(
position,
Vec2 {
x: position.x + (scale.x * scale_factor) - 1,
y: position.y + (scale.y * scale_factor) - 1,
},
);
for pixel in scale_iter {
let [c1, c2] = pixel.as_color().bytes;
display.write(Writeable::Data(&[c1, c2]));
}
Ok(())
}
struct ScaleIterator<'a> {
qoi: QoiIterator<'a>,
iter: &'a mut dyn Iterator<Item = Rgb565>,
last_row: [Rgb565; 120],
width: usize,
scale_factor: usize,
@ -144,7 +159,7 @@ impl<'a> Iterator for ScaleIterator<'a> {
fn next(&mut self) -> Option<Self::Item> {
if self.scale_factor == 1 {
return self.qoi.next();
return self.iter.next();
}
if self.index >= self.width * self.scale_factor {
@ -155,7 +170,7 @@ impl<'a> Iterator for ScaleIterator<'a> {
if self.counter % self.scale_factor == 0 {
if (self.index % self.scale_factor) == 0 {
if let Some(pixel) = self.qoi.next() {
if let Some(pixel) = self.iter.next() {
self.last_row[index_div] = pixel;
self.index += 1;
Some(pixel)

View File

@ -18,15 +18,15 @@ use atmega_hal::{
use panic_halt as _;
use crate::{
display::{Display, Position},
display::{Display, Vec2},
graphics::{Image, LARGE_CAT_UNSAFE, PRESS_BTN_UNSAFE, draw_image},
peripherals::Button,
qoi::{Image, LARGE_CAT_UNSAFE, PRESS_BTN_UNSAFE, draw_image},
};
mod display;
mod font;
mod graphics;
mod peripherals;
mod qoi;
type CoreClock = atmega_hal::clock::MHz8;
@ -85,7 +85,7 @@ fn main() -> ! {
&mut serial,
&mut Image::from(addr_of!(PRESS_BTN_UNSAFE)).iter(),
&mut display,
Position { x: 0, y: 0 },
Vec2 { x: 0, y: 0 },
) {
Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(),
Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(),
@ -97,7 +97,7 @@ fn main() -> ! {
&mut serial,
&mut images[idx].iter(),
&mut display,
Position { x: 0, y: 0 },
Vec2 { x: 0, y: 0 },
) {
Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(),
Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(),