From 2a83844ab56a6dde8adac93b771862df90d122c4 Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Fri, 11 Sep 2026 11:33:49 -0400 Subject: [PATCH] Allow SH1106Dev to fail on new, giving a device that simply ignores commands and issues warnings. --- src/sh1106.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/sh1106.rs b/src/sh1106.rs index 4aab35a..f0876c7 100644 --- a/src/sh1106.rs +++ b/src/sh1106.rs @@ -29,6 +29,7 @@ pub struct SH1106Dev { impl SH1106Dev { pub fn new(delay: &mut Delay, mut i2c: &mut dyn I2c) -> Self { let mut rx_buff = [0u8; 1]; + let mut fails = 0; loop { let res = match i2c.read(SH1106_I2C_ADDR, &mut rx_buff) { @@ -43,6 +44,15 @@ impl SH1106Dev { break; } + fails += 1; + + if fails > 10 { + warn!("Could not find SH1106. Ignoring."); + return Self { + ready: false, + vertical_flip: false, + }; + } println!("Waiting for sh1106 oled init..."); delay.delay_ms(500); @@ -68,7 +78,8 @@ impl SH1106Dev { pub fn set_vertical_flip(&mut self, i2c: &mut dyn I2c, flipped: bool) { if !self.ready { - panic!("Attempted to use SSH1106 before initialized."); + warn!("SSH1106 not initialized, ignoring cmd."); + return; } self.vertical_flip = flipped; @@ -83,7 +94,8 @@ impl SH1106Dev { pub fn blit_framebuffer(&self, i2c: &mut dyn I2c, gfx_buf: &GraphicsBuf) { if !self.ready { - panic!("Attempted to use SSH1106 before initialized."); + warn!("SSH1106 not initialized, ignoring cmd."); + return; } let buf = gfx_buf.get_px_buffer();