From 4c1161c1fe583aa949fbd3a93145059439922eab Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Mon, 5 Jan 2026 08:37:04 -0500 Subject: [PATCH] Vertical flip function added --- src/main.rs | 4 +++- src/sh1106.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index bcac557..176e1d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,9 +65,11 @@ fn main() -> ! { let mut led_pin = pins.gpio25.into_push_pull_output(); led_pin.set_high().unwrap(); - let sh1106_dev = sh1106_pico_rs::sh1106::SH1106Dev::new(&mut delay, &mut i2c); + let mut sh1106_dev = sh1106_pico_rs::sh1106::SH1106Dev::new(&mut delay, &mut i2c); println!("SH1106 initialized."); + sh1106_dev.set_vertical_flip(&mut i2c, true); + let mut gfx_buf = sh1106_pico_rs::graphics::GraphicsBuf::new(); loop { diff --git a/src/sh1106.rs b/src/sh1106.rs index 5fa2bca..b0b3482 100644 --- a/src/sh1106.rs +++ b/src/sh1106.rs @@ -23,6 +23,7 @@ pub const SH1106_I2C_ADDR: u8 = 0x78u8 >> 1; pub struct SH1106Dev { ready: bool, + vertical_flip: bool, } impl SH1106Dev { @@ -38,7 +39,25 @@ impl SH1106Dev { delay.delay_ms(500); send_command(&mut i2c, &SH1106Cmd::DisplayOnOff, 0x01); - SH1106Dev { ready: true } + SH1106Dev { + ready: true, + vertical_flip: false, + } + } + + pub fn set_vertical_flip(&mut self, i2c: &mut dyn I2c, flipped: bool) { + if !self.ready { + panic!("Attempted to use SSH1106 before initialized."); + } + + self.vertical_flip = flipped; + + let cmd = match flipped { + true => SH1106Cmd::ColDirectionRev, + false => SH1106Cmd::ColDirectionNorm, + }; + + send_command(i2c, &cmd, 0); } pub fn blit_framebuffer(&self, i2c: &mut dyn I2c, gfx_buf: &GraphicsBuf) { @@ -51,12 +70,12 @@ impl SH1106Dev { for r in 0..8 { set_page(i2c, r); set_col(i2c, 2); + // for each cel of 8x8 pixels for c in 0..16 { let mut cel = [0u8; 8]; // so our x,y here would be r * 128 + c * 8 - // start assembling the 8x8 cel for x in 0..8 { let mut col_byte = 0u8; @@ -65,7 +84,11 @@ impl SH1106Dev { for y in 0..8 { let pix_x = (c as usize) * 8 + x; let pix_y = (r as usize) * 8 + y; - col_byte |= buf[pix_y * 128 + pix_x] << y; + let byte_index = match self.vertical_flip { + true => pix_y * 128 + (127 - pix_x), + false => pix_y * 128 + pix_x, + }; + col_byte |= buf[byte_index] << y; } cel[x] = col_byte;