Vertical flip function added

This commit is contained in:
JP Stringham
2026-01-05 08:37:04 -05:00
parent 4ed15fae42
commit 4c1161c1fe
2 changed files with 29 additions and 4 deletions

View File

@@ -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 {

View File

@@ -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<Error = Error>, 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<Error = Error>, 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;