Updates to make it a bit more of a cogent standalone product

This commit is contained in:
JP Stringham
2025-12-26 19:00:53 -05:00
parent 2e48b4927b
commit 605b789682
2 changed files with 32 additions and 57 deletions

View File

@@ -1,3 +1,8 @@
// Intended to be used with a 1.3" OLED
// resolution is 128*64
//
#![no_std] #![no_std]
#![no_main] #![no_main]
@@ -24,6 +29,7 @@ use crate::sprite::*;
#[used] #[used]
pub static BOOT_LOADER: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; pub static BOOT_LOADER: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;
#[allow(unused)]
#[repr(u8)] #[repr(u8)]
#[derive(PartialEq, Copy, Clone)] #[derive(PartialEq, Copy, Clone)]
enum SH1106Cmd { enum SH1106Cmd {
@@ -36,7 +42,7 @@ enum SH1106Cmd {
SetPage = 0xB0, SetPage = 0xB0,
} }
const SH1106_ADDR:u8 = 0x78u8 >> 1; const SH1106_I2C_ADDR:u8 = 0x78u8 >> 1;
#[rp2040_hal::entry] #[rp2040_hal::entry]
fn main() -> ! { fn main() -> ! {
@@ -74,14 +80,7 @@ fn main() -> ! {
); );
// Configure two pins as being I²C, not GPIO // Create the I²C drive, using the two pre-configured pins.
// let sda_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio0.reconfigure();
// let scl_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio1.reconfigure();
// let not_an_scl_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio20.reconfigure();
// Create the I²C drive, using the two pre-configured pins. This will fail
// at compile time if the pins are in the wrong mode, or if this I²C
// peripheral isn't available on these pins!
let mut i2c = hal::I2C::i2c0( let mut i2c = hal::I2C::i2c0(
pac.I2C0, pac.I2C0,
pins.gpio8.reconfigure(), pins.gpio8.reconfigure(),
@@ -107,29 +106,17 @@ fn main() -> ! {
let mut gfx_buf = [0u8; 8192]; let mut gfx_buf = [0u8; 8192];
// gfx_buf.copy_from_slice(&TILES[0..8192]);
let sprite_atlas = SpriteAtlas::new(include_bytes!("../sprites/sprites_atlas.bin")); let sprite_atlas = SpriteAtlas::new(include_bytes!("../sprites/sprites_atlas.bin"));
let mut sprites = [Sprite::new(); 30]; let mut sprites = [Sprite::new(); 30];
// for (i, sprite) in sprites.iter_mut().enumerate() {
// sprite.sprite_index = i+30;
// let i = i as i32;
// sprite.x = (i*10) % 128;
// sprite.y = (i*10) % 64;
// sprite.x_vel = i % 3 - 2;
// sprite.y_vel = i % 4 - 1;
// }
sprites[0].sprite_index = 94; sprites[0].sprite_index = 94;
sprites[0].x = 90; sprites[0].x = 90;
sprites[0].y = 30; sprites[0].y = 30;
sprites[0].visible = true; sprites[0].visible = true;
let mut scanline = 0u8;
loop { loop {
gfx_buf.iter_mut().for_each(|x| *x = 0); gfx_buf.iter_mut().for_each(|x| *x = 0);
@@ -144,9 +131,18 @@ fn main() -> ! {
sprite_atlas.draw_textfield(10, 20, 110, 10, "I think this font has a *lot* of 'character'! ;)", &mut gfx_buf); sprite_atlas.draw_textfield(10, 20, 110, 10, "I think this font has a *lot* of 'character'! ;)", &mut gfx_buf);
draw_bresenham_line(0, scanline, 128, scanline, &mut gfx_buf);
draw_bresenham_line(0, 64, 127, 64, &mut gfx_buf);
draw_bresenham_line(0, 63, 128, 63, &mut gfx_buf);
draw_bresenham_line(0, 0, 0, 63, &mut gfx_buf);
draw_bresenham_line(127, 0, 127, 63, &mut gfx_buf);
blit_framebuffer(&mut i2c, &gfx_buf); blit_framebuffer(&mut i2c, &gfx_buf);
delay.delay_ms(1); delay.delay_ms(1);
// scanline += 1;
// if scanline >= 64 {
// scanline = 0;
// }
} }
} }
@@ -155,11 +151,16 @@ fn draw_bresenham_line(
x1: u8, y1: u8, x1: u8, y1: u8,
gfx_buf: &mut [u8; 8192], gfx_buf: &mut [u8; 8192],
) { ) {
let mut dx = x1 as i32 - x0 as i32; let x0 = (x0 as i32).clamp(0,127);
let mut dy = y1 as i32 - y0 as i32; let x1 = (x1 as i32).clamp(0, 127);
let y0 = (y0 as i32).clamp(0, 63);
let y1 = (y1 as i32).clamp(0, 63);
let mut x = x0 as i32; let mut dx = x1 - x0;
let mut y = y0 as i32; let mut dy = y1 - y0;
let mut x = x0;
let mut y = y0;
let mut err = 0; let mut err = 0;
@@ -241,32 +242,6 @@ fn blit_framebuffer(
} }
// as our display is in 8 pages of 128 columns, we need to transform the graphic
// to match the display's orientation
// fn transform_hcompress_graphic(graphic: [u8; 1024]) -> [u8; 1024] {
// let mut cel:[u8;8];
// let mut transformed_graphic = [0u8;1024];
// for row in 0..8 {
// for x in 0..16 {
// cel = [0u8; 8];
// for y in 0..8 {
// let byte = graphic[(row*8+y)*16+x];
// for i in 0..8 {
// cel[i] |= ((byte & (0b1000_0000 >> i)) << i) >> y;
// }
// }
// transformed_graphic[row*128+x*8..row*128+x*8+8].copy_from_slice(&cel[0..8]);
// // transformed_graphic[x*8..x*8+8].copy_from_slice(&cel[0..8]);
// }
// }
// transformed_graphic
// }
fn set_col(i2c: &mut hal::I2C<pac::I2C0, (Pin<Gpio8, FunctionI2C, PullUp>, Pin<Gpio9, FunctionI2C, PullUp>)>, fn set_col(i2c: &mut hal::I2C<pac::I2C0, (Pin<Gpio8, FunctionI2C, PullUp>, Pin<Gpio9, FunctionI2C, PullUp>)>,
col: u8) { col: u8) {
let col_lo = col & 0b0000_1111; let col_lo = col & 0b0000_1111;
@@ -287,7 +262,7 @@ fn clr_screen(i2c: &mut hal::I2C<pac::I2C0, (Pin<Gpio8, FunctionI2C, PullUp>, Pi
writebuf[0] = 0b0100_0000; writebuf[0] = 0b0100_0000;
for i in 0..8 { for i in 0..8 {
match i2c.write(SH1106_ADDR, &writebuf) { match i2c.write(SH1106_I2C_ADDR, &writebuf) {
_ => () _ => ()
}; };
@@ -305,7 +280,7 @@ fn write_cel_pixels(
writebuf[0] = 0b0100_0000; writebuf[0] = 0b0100_0000;
writebuf[1..].copy_from_slice(&data[0..8]); writebuf[1..].copy_from_slice(&data[0..8]);
match i2c.write(SH1106_ADDR, &writebuf) { match i2c.write(SH1106_I2C_ADDR, &writebuf) {
_ => () _ => ()
}; };
} }
@@ -319,7 +294,7 @@ fn send_command(i2c: &mut hal::I2C<pac::I2C0, (Pin<Gpio8, FunctionI2C, PullUp>,
writebuf[0] = 0b1000_0000; writebuf[0] = 0b1000_0000;
writebuf[1] = (command.clone() as u8) | data; writebuf[1] = (command.clone() as u8) | data;
match i2c.write(SH1106_ADDR, &writebuf) { match i2c.write(SH1106_I2C_ADDR, &writebuf) {
_ => () _ => ()
}; };
} }

View File

@@ -86,7 +86,7 @@ impl SpriteAtlas {
let word_width = self.get_str_pixel_width(word); let word_width = self.get_str_pixel_width(word);
if cur_x + word_width as u8 > x + width { if cur_x + word_width as u8 > x + width {
cur_x = 0; cur_x = x;
cur_y += 8; cur_y += 8;
} }