Add shared reset pin

This commit is contained in:
2026-08-23 13:10:33 +02:00
parent 537692d348
commit 357701a685
+83 -21
View File
@@ -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<CriticalSectionRawMutex, RefCell<P>>,
}
impl<'a, P> SharedPin<'a, P> {
pub fn new(mutex: &'a BlockingMutex<CriticalSectionRawMutex, RefCell<P>>) -> 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<CriticalSectionRawMutex, RefCell<P>> {
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<CriticalSectionRawMutex, RefCell<(P, bool)>>,
}
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<SPI, SCK, MOSI, MISO, CS0, CS1, CS2, CS3, RES1, RES2, DC> {
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,
);