Attempt to library-ify the crate

This commit is contained in:
JP Stringham
2025-12-28 10:27:14 -05:00
parent a4e99028cc
commit 5ce004d219
5 changed files with 48 additions and 44 deletions

View File

@@ -4,4 +4,5 @@
"workbench.editor.customLabels.patterns": { "workbench.editor.customLabels.patterns": {
"**/mod.rs": "mod | ${dirname}" "**/mod.rs": "mod | ${dirname}"
}, },
"rust-analyzer.cargo.target": "thumbv6m-none-eabi"
} }

View File

@@ -1,4 +1,6 @@
use crate::sprite::{Sprite, SpriteAtlas}; mod sprite;
use sprite::*;
pub struct GraphicsBuf { pub struct GraphicsBuf {
buffer: [u8; 8192], buffer: [u8; 8192],

View File

@@ -1,6 +1,6 @@
static SPRITES_COMPRESSED:[u8;16384] = *include_bytes!("../sprites/sprites_tex.bin"); static SPRITES_COMPRESSED: [u8; 16384] = *include_bytes!("../../sprites/sprites_tex.bin");
const ATLAS_WIDTH:usize = 256; const ATLAS_WIDTH: usize = 256;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Sprite { pub struct Sprite {
@@ -47,9 +47,8 @@ pub struct SpriteAtlas {
} }
impl SpriteAtlas { impl SpriteAtlas {
pub fn new(from_bin:&[u8]) -> SpriteAtlas { pub fn new(from_bin: &[u8]) -> SpriteAtlas {
if from_bin.len() > 256 * 4 {
if from_bin.len() > 256*4 {
panic!("SpriteAtlas::new() - from_bin is too large"); panic!("SpriteAtlas::new() - from_bin is too large");
} }
@@ -67,14 +66,15 @@ impl SpriteAtlas {
entries[i].height = chunk[3]; entries[i].height = chunk[3];
}); });
SpriteAtlas { SpriteAtlas { entries: entries }
entries: entries,
}
} }
pub fn draw_textfield(&self, pub fn draw_textfield(
x: u8, y: u8, &self,
width: u8, height: u8, x: u8,
y: u8,
width: u8,
height: u8,
string: &str, string: &str,
gfx_buf: &mut [u8; 8192], gfx_buf: &mut [u8; 8192],
) { ) {
@@ -89,32 +89,28 @@ impl SpriteAtlas {
cur_x = x; cur_x = x;
cur_y += 8; cur_y += 8;
} }
self.draw_string(cur_x, cur_y, word, gfx_buf); self.draw_string(cur_x, cur_y, word, gfx_buf);
cur_x += word_width as u8 + 4; cur_x += word_width as u8 + 4;
} }
} }
pub fn draw_string(&self, pub fn draw_string(&self, x: u8, y: u8, string: &str, gfx_buf: &mut [u8; 8192]) {
x: u8, y: u8,
string: &str,
gfx_buf: &mut [u8; 8192],
) {
let mut x = x; let mut x = x;
for c in string.chars() { for c in string.chars() {
if c == ' ' { if c == ' ' {
x += 4; x += 4;
continue; continue;
} }
let sprite_index = c as usize - 33; let sprite_index = c as usize - 33;
self.draw_sprite(sprite_index, x, y, SpriteFlip::None, gfx_buf); self.draw_sprite(sprite_index, x, y, SpriteFlip::None, gfx_buf);
x += self.entries[sprite_index].width + 1; x += self.entries[sprite_index].width + 1;
} }
} }
fn get_str_pixel_width(&self, str:&str) -> usize { fn get_str_pixel_width(&self, str: &str) -> usize {
let mut width = 0; let mut width = 0;
for c in str.chars() { for c in str.chars() {
@@ -130,12 +126,14 @@ impl SpriteAtlas {
width width
} }
pub fn draw_sprite(&self, pub fn draw_sprite(
&self,
sprite_index: usize, sprite_index: usize,
x: u8, y:u8, x: u8,
y: u8,
flip: SpriteFlip, flip: SpriteFlip,
gfx_buf: &mut [u8; 8192]) { gfx_buf: &mut [u8; 8192],
) {
let entry = self.entries[sprite_index]; let entry = self.entries[sprite_index];
for sprite_y in 0..entry.height { for sprite_y in 0..entry.height {
@@ -152,23 +150,30 @@ impl SpriteAtlas {
let px = match flip { let px = match flip {
SpriteFlip::None => self.px_for_entry(sprite_index, sprite_x, sprite_y), SpriteFlip::None => self.px_for_entry(sprite_index, sprite_x, sprite_y),
SpriteFlip::Horizontal => self.px_for_entry(sprite_index, entry.width - sprite_x - 1, sprite_y), SpriteFlip::Horizontal => {
SpriteFlip::Vertical => self.px_for_entry(sprite_index, sprite_x, entry.height - sprite_y - 1), self.px_for_entry(sprite_index, entry.width - sprite_x - 1, sprite_y)
SpriteFlip::Both => self.px_for_entry(sprite_index, entry.width - sprite_x - 1, entry.height - sprite_y - 1), }
SpriteFlip::Vertical => {
self.px_for_entry(sprite_index, sprite_x, entry.height - sprite_y - 1)
}
SpriteFlip::Both => self.px_for_entry(
sprite_index,
entry.width - sprite_x - 1,
entry.height - sprite_y - 1,
),
}; };
if px == 3 { if px == 3 {
continue; continue;
} }
let target_index = (tgt_y as usize)*128 + tgt_x as usize; let target_index = (tgt_y as usize) * 128 + tgt_x as usize;
gfx_buf[target_index] = px; gfx_buf[target_index] = px;
} }
} }
} }
pub fn px_for_entry(&self, entry_index:usize, x:u8, y:u8) -> u8 { pub fn px_for_entry(&self, entry_index: usize, x: u8, y: u8) -> u8 {
let entry = self.entries[entry_index]; let entry = self.entries[entry_index];
// input is uncompressed coordinate system // input is uncompressed coordinate system
@@ -181,7 +186,7 @@ impl SpriteAtlas {
// however as the compressed tiles are 2bpp (4px per byte) // however as the compressed tiles are 2bpp (4px per byte)
// we need to find the byte that contains the pixel we're looking for // we need to find the byte that contains the pixel we're looking for
let sprite_row_byte_index = (y as usize)*ATLAS_WIDTH/4 + (x as usize)/4; let sprite_row_byte_index = (y as usize) * ATLAS_WIDTH / 4 + (x as usize) / 4;
let sprite_row_byte = SPRITES_COMPRESSED[sprite_row_byte_index]; let sprite_row_byte = SPRITES_COMPRESSED[sprite_row_byte_index];
let x_remain = x % 4; let x_remain = x % 4;
@@ -196,9 +201,8 @@ impl SpriteAtlas {
// (sprite_row_byte & (0b1100_0000 >> 4)) >> 2 // (sprite_row_byte & (0b1100_0000 >> 4)) >> 2
// etc // etc
let px_byte = (sprite_row_byte & (0b1100_0000 >> (x_remain*2))) >> (6 - x_remain*2); let px_byte = (sprite_row_byte & (0b1100_0000 >> (x_remain * 2))) >> (6 - x_remain * 2);
px_byte px_byte
} }
} }

4
src/lib.rs Normal file
View File

@@ -0,0 +1,4 @@
#![no_std]
pub mod graphics;
pub mod ssh1106;

View File

@@ -12,13 +12,6 @@ use rp2040_hal as hal;
use defmt_rtt as _; use defmt_rtt as _;
use panic_probe as _; use panic_probe as _;
mod graphics;
mod sprite;
mod ssh1106;
use crate::graphics::*;
use crate::ssh1106::*;
#[link_section = ".boot_loader"] #[link_section = ".boot_loader"]
#[used] #[used]
pub static BOOT_LOADER: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; pub static BOOT_LOADER: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
@@ -72,10 +65,10 @@ fn main() -> ! {
let mut led_pin = pins.gpio25.into_push_pull_output(); let mut led_pin = pins.gpio25.into_push_pull_output();
led_pin.set_high().unwrap(); led_pin.set_high().unwrap();
let ssh1106_dev = SSH1106Dev::new(&mut delay, &mut i2c); let ssh1106_dev = sh1106_pico_rs::ssh1106::SSH1106Dev::new(&mut delay, &mut i2c);
println!("SSH1106 initialized."); println!("SSH1106 initialized.");
let mut gfx_buf = GraphicsBuf::new(); let mut gfx_buf = sh1106_pico_rs::graphics::GraphicsBuf::new();
loop { loop {
gfx_buf.clear(); gfx_buf.clear();