Draw a very high-resolution cat
This commit is contained in:
parent
bc944008a8
commit
606d7d43ce
23
Cargo.lock
generated
23
Cargo.lock
generated
@ -50,6 +50,17 @@ dependencies = [
|
|||||||
"unwrap-infallible",
|
"unwrap-infallible",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "avr-progmem"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5e93a9dd7da8f4eb57c912367e337687e06a8db6452d8c89eeb9eff1e0d2ba2d"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"derivative",
|
||||||
|
"ufmt",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "az"
|
name = "az"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
@ -80,6 +91,17 @@ version = "1.2.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derivative"
|
||||||
|
version = "2.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "embedded-graphics-core"
|
name = "embedded-graphics-core"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
@ -184,6 +206,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"atmega-hal",
|
"atmega-hal",
|
||||||
"avr-device",
|
"avr-device",
|
||||||
|
"avr-progmem",
|
||||||
"embedded-graphics-core",
|
"embedded-graphics-core",
|
||||||
"embedded-hal 1.0.0",
|
"embedded-hal 1.0.0",
|
||||||
"panic-halt",
|
"panic-halt",
|
||||||
|
@ -11,6 +11,7 @@ atmega-hal = { git = "https://github.com/Rahix/avr-hal?tab=readme-ov-file", rev=
|
|||||||
|
|
||||||
ufmt = "0.2.0"
|
ufmt = "0.2.0"
|
||||||
embedded-graphics-core = "0.4.0"
|
embedded-graphics-core = "0.4.0"
|
||||||
|
avr-progmem = "0.4.0"
|
||||||
# nb = "1.1.0"
|
# nb = "1.1.0"
|
||||||
# pwm-pca9685 = "1.0.0"
|
# pwm-pca9685 = "1.0.0"
|
||||||
# infrared = "0.14.1"
|
# infrared = "0.14.1"
|
||||||
|
BIN
images/cat.qoi
Normal file
BIN
images/cat.qoi
Normal file
Binary file not shown.
@ -20,7 +20,7 @@ use panic_halt as _;
|
|||||||
use crate::{
|
use crate::{
|
||||||
display::{Display, Position, Rgb565},
|
display::{Display, Position, Rgb565},
|
||||||
peripherals::{Button, Knob},
|
peripherals::{Button, Knob},
|
||||||
qoi::{MARIO, PIKA, PRESS_BTN, STEVE, draw_image},
|
qoi::{MARIO, PRESS_BTN, draw_image},
|
||||||
};
|
};
|
||||||
|
|
||||||
mod display;
|
mod display;
|
||||||
@ -78,12 +78,12 @@ fn main() -> ! {
|
|||||||
let mut button = Button::from(pins.pd5.into_pull_up_input());
|
let mut button = Button::from(pins.pd5.into_pull_up_input());
|
||||||
|
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
let images = [&PIKA.as_slice(), &MARIO.as_slice(), &STEVE.as_slice()];
|
let images = [&MARIO.as_slice()];
|
||||||
let len = images.len();
|
let len = images.len();
|
||||||
|
|
||||||
match draw_image(
|
match draw_image(
|
||||||
&mut serial,
|
&mut serial,
|
||||||
&PIKA.as_slice(),
|
&MARIO.as_slice(),
|
||||||
&mut display,
|
&mut display,
|
||||||
Position { x: 0, y: 0 },
|
Position { x: 0, y: 0 },
|
||||||
) {
|
) {
|
||||||
|
43
src/qoi.rs
43
src/qoi.rs
@ -8,20 +8,44 @@ use atmega_hal::{
|
|||||||
pac::USART0,
|
pac::USART0,
|
||||||
port::{PB3, PB5, PD0, PD1, Pin, PinOps, mode},
|
port::{PB3, PB5, PD0, PD1, Pin, PinOps, mode},
|
||||||
};
|
};
|
||||||
|
use avr_progmem::{progmem, wrapper::ProgMem};
|
||||||
use embedded_hal::delay::DelayNs;
|
use embedded_hal::delay::DelayNs;
|
||||||
use ufmt::derive::uDebug;
|
use ufmt::derive::uDebug;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
CoreClock,
|
CoreClock,
|
||||||
display::{Color, Display, Position, Rgb565, Writeable},
|
display::{Color, Display, Position, Rgb565, Writeable},
|
||||||
|
qoi,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
progmem! {
|
||||||
|
/// Static byte stored in progmem!
|
||||||
|
static progmem P_BYTE: u8 = 42;
|
||||||
|
static progmem VERY_LARGE: [u8; 23731] = *include_bytes!("../images/cat.qoi");
|
||||||
|
}
|
||||||
|
|
||||||
// https://qoiformat.org/qoi-specification.pdf
|
// https://qoiformat.org/qoi-specification.pdf
|
||||||
pub static MARIO: [u8; 161] = *include_bytes!("../images/mario.qoi");
|
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");
|
pub static PRESS_BTN: [u8; 174] = *include_bytes!("../images/press_btn.qoi");
|
||||||
|
|
||||||
|
pub struct LargeIterator<const LENGTH: usize> {
|
||||||
|
buffer: ProgMem<[u8; LENGTH]>,
|
||||||
|
index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const LENGTH: usize> Iterator for LargeIterator<LENGTH> {
|
||||||
|
type Item = u8;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.index >= LENGTH {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let item = self.buffer.load_at(self.index);
|
||||||
|
self.index += 1;
|
||||||
|
Some(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, uDebug)]
|
#[derive(Debug, uDebug)]
|
||||||
pub enum QoiErr {
|
pub enum QoiErr {
|
||||||
InvalidMagicNumber,
|
InvalidMagicNumber,
|
||||||
@ -34,7 +58,12 @@ pub fn draw_image<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
|
|||||||
display: &mut Display<T, DCPin, RSTPin>,
|
display: &mut Display<T, DCPin, RSTPin>,
|
||||||
position: Position,
|
position: Position,
|
||||||
) -> Result<(), QoiErr> {
|
) -> Result<(), QoiErr> {
|
||||||
let mut iter = buffer.iter().map(|b| *b);
|
// let mut iter = buffer.iter().map(|b| *b);
|
||||||
|
|
||||||
|
let mut iter = LargeIterator {
|
||||||
|
buffer: VERY_LARGE,
|
||||||
|
index: 0,
|
||||||
|
};
|
||||||
|
|
||||||
if let (Some(113), Some(111), Some(105), Some(102)) =
|
if let (Some(113), Some(111), Some(105), Some(102)) =
|
||||||
(iter.next(), iter.next(), iter.next(), iter.next())
|
(iter.next(), iter.next(), iter.next(), iter.next())
|
||||||
@ -73,7 +102,7 @@ pub fn draw_image<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
|
|||||||
let qoi_iter = QoiIterator::from(&mut iter, width * height);
|
let qoi_iter = QoiIterator::from(&mut iter, width * height);
|
||||||
let scale_iter = ScaleIterator {
|
let scale_iter = ScaleIterator {
|
||||||
qoi: qoi_iter,
|
qoi: qoi_iter,
|
||||||
last_row: [Rgb565::yellow(); 64],
|
last_row: [Rgb565::yellow(); 120],
|
||||||
scale_factor: scale_factor as usize,
|
scale_factor: scale_factor as usize,
|
||||||
width: width as usize,
|
width: width as usize,
|
||||||
counter: 0,
|
counter: 0,
|
||||||
@ -96,7 +125,7 @@ pub fn draw_image<T: DelayNs, DCPin: PinOps, RSTPin: PinOps>(
|
|||||||
|
|
||||||
struct ScaleIterator<'a> {
|
struct ScaleIterator<'a> {
|
||||||
qoi: QoiIterator<'a>,
|
qoi: QoiIterator<'a>,
|
||||||
last_row: [Rgb565; 64],
|
last_row: [Rgb565; 120],
|
||||||
width: usize,
|
width: usize,
|
||||||
scale_factor: usize,
|
scale_factor: usize,
|
||||||
counter: usize,
|
counter: usize,
|
||||||
@ -107,6 +136,10 @@ impl<'a> Iterator for ScaleIterator<'a> {
|
|||||||
type Item = Rgb565;
|
type Item = Rgb565;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.scale_factor == 1 {
|
||||||
|
return self.qoi.next();
|
||||||
|
}
|
||||||
|
|
||||||
if self.index >= self.width * self.scale_factor {
|
if self.index >= self.width * self.scale_factor {
|
||||||
self.counter = (self.counter + 1) % self.scale_factor;
|
self.counter = (self.counter + 1) % self.scale_factor;
|
||||||
self.index = 0;
|
self.index = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user