Compare commits

..

3 Commits

Author SHA1 Message Date
309ca727fc Finish slideshow with larger images from program memory 2025-09-13 13:30:15 +03:00
c1bd4acfd0 Make Image into a proper struct 2025-09-13 13:16:27 +03:00
ed3265f156 Remove progmem 2025-09-13 13:08:32 +03:00
11 changed files with 67 additions and 66 deletions

23
Cargo.lock generated
View File

@ -50,17 +50,6 @@ dependencies = [
"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]]
name = "az"
version = "1.2.1"
@ -91,17 +80,6 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "embedded-graphics-core"
version = "0.4.0"
@ -206,7 +184,6 @@ version = "0.1.0"
dependencies = [
"atmega-hal",
"avr-device",
"avr-progmem",
"embedded-graphics-core",
"embedded-hal 1.0.0",
"panic-halt",

View File

@ -11,7 +11,6 @@ atmega-hal = { git = "https://github.com/Rahix/avr-hal?tab=readme-ov-file", rev=
ufmt = "0.2.0"
embedded-graphics-core = "0.4.0"
avr-progmem = "0.4.0"
# nb = "1.1.0"
# pwm-pca9685 = "1.0.0"
# infrared = "0.14.1"

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
images/rick.qoi Normal file

Binary file not shown.

Binary file not shown.

BIN
images/xp_desktop.qoi Normal file

Binary file not shown.

View File

@ -6,21 +6,21 @@
#![feature(iter_array_chunks)]
#![feature(const_slice_make_iter)]
#![feature(slice_as_chunks)]
#![feature(generic_const_exprs)]
#![feature(asm_experimental_arch)]
use core::ptr::addr_of;
use atmega_hal::{
Adc, Eeprom, Usart,
port::{Pin, mode},
Usart,
spi::{self, Settings},
usart::Baudrate,
};
use embedded_hal::delay::DelayNs;
use panic_halt as _;
use crate::{
display::{Display, Position, Rgb565},
peripherals::{Button, Knob},
qoi::{MARIO, PRESS_BTN, draw_image},
display::{Display, Position},
peripherals::Button,
qoi::{Image, LARGE_CAT_UNSAFE, PRESS_BTN_UNSAFE, RICK_UNSAFE, XP_DESKTOP_UNSAFE, draw_image},
};
mod display;
@ -78,12 +78,16 @@ fn main() -> ! {
let mut button = Button::from(pins.pd5.into_pull_up_input());
let mut idx = 0;
let images = [&MARIO.as_slice()];
let images = [
Image::from(addr_of!(LARGE_CAT_UNSAFE)),
Image::from(addr_of!(XP_DESKTOP_UNSAFE)),
Image::from(addr_of!(RICK_UNSAFE)),
];
let len = images.len();
match draw_image(
&mut serial,
&MARIO.as_slice(),
&Image::from(addr_of!(PRESS_BTN_UNSAFE)),
&mut display,
Position { x: 0, y: 0 },
) {
@ -93,8 +97,6 @@ fn main() -> ! {
loop {
if button.poll() {
idx = (idx + 1) % len;
match draw_image(
&mut serial,
&images[idx],
@ -104,6 +106,8 @@ fn main() -> ! {
Ok(_) => ufmt::uwriteln!(serial, "Successfully read QOI").unwrap(),
Err(e) => ufmt::uwriteln!(serial, "Error: {:?}", e).unwrap(),
}
idx = (idx + 1) % len;
}
}
}

View File

@ -1,48 +1,75 @@
use core::{
hint::black_box,
slice::{Chunks, Iter},
};
use core::{arch::asm, ptr::addr_of};
use atmega_hal::{
Atmega, Usart,
Usart,
pac::USART0,
port::{PB3, PB5, PD0, PD1, Pin, PinOps, mode},
port::{PD0, PD1, Pin, PinOps, mode},
};
use avr_progmem::{progmem, wrapper::ProgMem};
use embedded_hal::delay::DelayNs;
use ufmt::derive::uDebug;
use crate::{
CoreClock,
display::{Color, Display, Position, Rgb565, Writeable},
qoi,
display::{Display, Position, Rgb565, Writeable},
};
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
#[unsafe(link_section = ".progmem.data")]
pub static LARGE_CAT_UNSAFE: [u8; 8765] = *include_bytes!("../images/cat.qoi");
#[unsafe(link_section = ".progmem.data")]
pub static XP_DESKTOP_UNSAFE: [u8; 7360] = *include_bytes!("../images/xp_desktop.qoi");
#[unsafe(link_section = ".progmem.data")]
pub static RICK_UNSAFE: [u8; 2391] = *include_bytes!("../images/rick.qoi");
#[unsafe(link_section = ".progmem.data")]
pub static PRESS_BTN_UNSAFE: [u8; 1225] = *include_bytes!("../images/press_btn.qoi");
pub struct Image {
ptr: *const u8,
length: usize,
}
// https://qoiformat.org/qoi-specification.pdf
pub static MARIO: [u8; 161] = *include_bytes!("../images/mario.qoi");
pub static PRESS_BTN: [u8; 174] = *include_bytes!("../images/press_btn.qoi");
pub struct LargeIterator<const LENGTH: usize> {
buffer: ProgMem<[u8; LENGTH]>,
pub struct ImageIterator {
ptr: *const u8,
length: usize,
index: usize,
}
impl<const LENGTH: usize> Iterator for LargeIterator<LENGTH> {
impl Image {
pub const fn from<const N: usize>(addr: *const [u8; N]) -> Image {
Image {
ptr: addr.cast(),
length: N,
}
}
pub fn iter(&self) -> ImageIterator {
ImageIterator {
ptr: self.ptr,
length: self.length,
index: 0,
}
}
}
impl Iterator for ImageIterator {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= LENGTH {
if self.index >= self.length {
return None;
}
let item = self.buffer.load_at(self.index);
let elem_ptr = self.ptr.wrapping_add(self.index);
let res: u8;
unsafe {
asm!(
"lpm {}, Z",
out(reg) res,
in("Z") elem_ptr,
)
}
self.index += 1;
Some(item)
Some(res)
}
}
@ -54,17 +81,11 @@ pub enum QoiErr {
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],
image: &Image,
display: &mut Display<T, DCPin, RSTPin>,
position: Position,
) -> Result<(), QoiErr> {
// let mut iter = buffer.iter().map(|b| *b);
let mut iter = LargeIterator {
buffer: VERY_LARGE,
index: 0,
};
let mut iter = image.iter();
if let (Some(113), Some(111), Some(105), Some(102)) =
(iter.next(), iter.next(), iter.next(), iter.next())
{