Attempt to library-ify the crate
This commit is contained in:
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@@ -4,4 +4,5 @@
|
||||
"workbench.editor.customLabels.patterns": {
|
||||
"**/mod.rs": "mod | ${dirname}"
|
||||
},
|
||||
"rust-analyzer.cargo.target": "thumbv6m-none-eabi"
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
use crate::sprite::{Sprite, SpriteAtlas};
|
||||
mod sprite;
|
||||
|
||||
use sprite::*;
|
||||
|
||||
pub struct GraphicsBuf {
|
||||
buffer: [u8; 8192],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
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;
|
||||
|
||||
@@ -48,7 +48,6 @@ pub struct SpriteAtlas {
|
||||
|
||||
impl SpriteAtlas {
|
||||
pub fn new(from_bin: &[u8]) -> SpriteAtlas {
|
||||
|
||||
if from_bin.len() > 256 * 4 {
|
||||
panic!("SpriteAtlas::new() - from_bin is too large");
|
||||
}
|
||||
@@ -67,14 +66,15 @@ impl SpriteAtlas {
|
||||
entries[i].height = chunk[3];
|
||||
});
|
||||
|
||||
SpriteAtlas {
|
||||
entries: entries,
|
||||
}
|
||||
SpriteAtlas { entries: entries }
|
||||
}
|
||||
|
||||
pub fn draw_textfield(&self,
|
||||
x: u8, y: u8,
|
||||
width: u8, height: u8,
|
||||
pub fn draw_textfield(
|
||||
&self,
|
||||
x: u8,
|
||||
y: u8,
|
||||
width: u8,
|
||||
height: u8,
|
||||
string: &str,
|
||||
gfx_buf: &mut [u8; 8192],
|
||||
) {
|
||||
@@ -95,11 +95,7 @@ impl SpriteAtlas {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_string(&self,
|
||||
x: u8, y: u8,
|
||||
string: &str,
|
||||
gfx_buf: &mut [u8; 8192],
|
||||
) {
|
||||
pub fn draw_string(&self, x: u8, y: u8, string: &str, gfx_buf: &mut [u8; 8192]) {
|
||||
let mut x = x;
|
||||
|
||||
for c in string.chars() {
|
||||
@@ -130,12 +126,14 @@ impl SpriteAtlas {
|
||||
width
|
||||
}
|
||||
|
||||
pub fn draw_sprite(&self,
|
||||
pub fn draw_sprite(
|
||||
&self,
|
||||
sprite_index: usize,
|
||||
x: u8, y:u8,
|
||||
x: u8,
|
||||
y: u8,
|
||||
flip: SpriteFlip,
|
||||
gfx_buf: &mut [u8; 8192]) {
|
||||
|
||||
gfx_buf: &mut [u8; 8192],
|
||||
) {
|
||||
let entry = self.entries[sprite_index];
|
||||
|
||||
for sprite_y in 0..entry.height {
|
||||
@@ -152,9 +150,17 @@ impl SpriteAtlas {
|
||||
|
||||
let px = match flip {
|
||||
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::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),
|
||||
SpriteFlip::Horizontal => {
|
||||
self.px_for_entry(sprite_index, entry.width - sprite_x - 1, sprite_y)
|
||||
}
|
||||
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 {
|
||||
@@ -165,7 +171,6 @@ impl SpriteAtlas {
|
||||
gfx_buf[target_index] = px;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn px_for_entry(&self, entry_index: usize, x: u8, y: u8) -> u8 {
|
||||
@@ -199,6 +204,5 @@ impl SpriteAtlas {
|
||||
let px_byte = (sprite_row_byte & (0b1100_0000 >> (x_remain * 2))) >> (6 - x_remain * 2);
|
||||
|
||||
px_byte
|
||||
|
||||
}
|
||||
}
|
||||
4
src/lib.rs
Normal file
4
src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
#![no_std]
|
||||
|
||||
pub mod graphics;
|
||||
pub mod ssh1106;
|
||||
11
src/main.rs
11
src/main.rs
@@ -12,13 +12,6 @@ use rp2040_hal as hal;
|
||||
use defmt_rtt as _;
|
||||
use panic_probe as _;
|
||||
|
||||
mod graphics;
|
||||
mod sprite;
|
||||
mod ssh1106;
|
||||
|
||||
use crate::graphics::*;
|
||||
use crate::ssh1106::*;
|
||||
|
||||
#[link_section = ".boot_loader"]
|
||||
#[used]
|
||||
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();
|
||||
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.");
|
||||
|
||||
let mut gfx_buf = GraphicsBuf::new();
|
||||
let mut gfx_buf = sh1106_pico_rs::graphics::GraphicsBuf::new();
|
||||
|
||||
loop {
|
||||
gfx_buf.clear();
|
||||
|
||||
Reference in New Issue
Block a user