From 357701a68535ee910bdb7d9961618bad161c86d8 Mon Sep 17 00:00:00 2001 From: ede1998 Date: Sun, 23 Aug 2026 13:10:33 +0200 Subject: [PATCH] Add shared reset pin --- src/display/shared_bus.rs | 104 ++++++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/src/display/shared_bus.rs b/src/display/shared_bus.rs index 964c49d..37145bc 100644 --- a/src/display/shared_bus.rs +++ b/src/display/shared_bus.rs @@ -16,19 +16,21 @@ use crate::display::secondary_oled::{SecondaryOledDisplay, init_secondary_oled_o use core::cell::RefCell; use embassy_sync::blocking_mutex::Mutex as BlockingMutex; -#[derive(Clone)] pub struct SharedPin<'a, P> { mutex: &'a BlockingMutex>, } -impl<'a, P> SharedPin<'a, P> { - pub fn new(mutex: &'a BlockingMutex>) -> Self { - Self { mutex } +impl<'a, P> Clone for SharedPin<'a, P> { + fn clone(&self) -> Self { + Self { mutex: self.mutex } } +} - pub fn pre_wrap(pin: P) -> &'static BlockingMutex> { +impl<'a, P> SharedPin<'a, P> { + pub fn new(pin: P) -> Self { let mutexed = BlockingMutex::new(RefCell::new(pin)); - Box::leak(Box::new(mutexed)) + let mutex = Box::leak(Box::new(mutexed)); + Self { mutex } } } @@ -46,11 +48,66 @@ impl<'a, P: EhOutputPin> EhOutputPin for SharedPin<'a, P> { } } +pub struct SharedResetPin<'a, P> { + mutex: &'a BlockingMutex>, +} + +impl<'a, P> SharedResetPin<'a, P> { + pub fn new(pin: P, disabled: bool) -> Self { + let mutexed = BlockingMutex::new(RefCell::new((pin, disabled))); + let mutex = Box::leak(Box::new(mutexed)); + Self { mutex } + } + + pub fn disable(&self, disabled: bool) { + self.mutex.lock(|p| { + p.borrow_mut().1 = disabled; + }); + if disabled { + log::debug!("Reset pin disabled (masked). No more resets now."); + } else { + log::debug!("Reset pin enabled (unmasked). Reset now possible."); + } + } +} + +impl<'a, P> Clone for SharedResetPin<'a, P> { + fn clone(&self) -> Self { + Self { mutex: self.mutex } + } +} + +impl<'a, P: EhOutputPin> ErrorType for SharedResetPin<'a, P> { + type Error = P::Error; +} + +impl<'a, P: EhOutputPin> EhOutputPin for SharedResetPin<'a, P> { + fn set_low(&mut self) -> Result<(), Self::Error> { + self.mutex.lock(|p| { + let mut guard = p.borrow_mut(); + let disabled = guard.1; + let pin = &mut guard.0; + if !disabled { pin.set_low() } else { Ok(()) } + }) + } + + fn set_high(&mut self) -> Result<(), Self::Error> { + self.mutex.lock(|p| { + let mut guard = p.borrow_mut(); + let disabled = guard.1; + let pin = &mut guard.0; + if !disabled { pin.set_high() } else { Ok(()) } + }) + } +} + +type SharedOutput<'a> = SharedPin<'a, Output<'a>>; +type SharedReset<'a> = SharedResetPin<'a, Output<'a>>; pub type DisplaySpiBus = Spi<'static, esp_hal::Blocking>; pub type DualPrimaryDisplay<'a> = - PrimaryLcdDisplay<'a, DisplaySpiBus, SharedPin<'a, Output<'a>>, SharedPin<'a, Output<'a>>>; + PrimaryLcdDisplay<'a, DisplaySpiBus, SharedOutput<'a>, Output<'a>>; pub type DualSecondaryDisplay<'a> = - SecondaryOledDisplay<'a, DisplaySpiBus, SharedPin<'a, Output<'a>>, SharedPin<'a, Output<'a>>>; + SecondaryOledDisplay<'a, DisplaySpiBus, SharedReset<'a>, SharedOutput<'a>>; pub struct DisplayPinConfiguration { pub spi_peripheral: SPI, @@ -94,33 +151,38 @@ where .with_miso(self.miso); let bus_static: &'static AtomicCell<_> = Box::leak(Box::new(AtomicCell::new(spi))); - let reset_primary_static = SharedPin::pre_wrap(Output::new( - self.reset_primary, + let reset_primary = Output::new(self.reset_primary, Level::High, OutputConfig::default()); + let reset_secondary = SharedResetPin::new( + Output::new(self.reset_secondary, Level::High, OutputConfig::default()), + false, + ); + let dc = SharedPin::new(Output::new( + self.dc_pin, Level::High, OutputConfig::default(), )); - let reset_secondary_static = SharedPin::pre_wrap(Output::new( - self.reset_secondary, - Level::High, - OutputConfig::default(), - )); - let dc_static = - SharedPin::pre_wrap(Output::new(self.dc_pin, Level::High, OutputConfig::default())); let mut delay = Delay::new(); let primary = init_primary_lcd_on_bus( bus_static, self.cs_primary, - SharedPin::new(reset_primary_static), - SharedPin::new(dc_static), + reset_primary, + dc.clone(), &mut delay, ); + // TODO + // Build display + // Trigger reset + // disable init + // rest of init code + + let secondary = init_secondary_oled_on_bus( bus_static, self.cs_secondary_1, - SharedPin::new(reset_secondary_static), - SharedPin::new(dc_static), + reset_secondary, + dc, &mut delay, );