Recover from init fail

This commit was merged in pull request #46.
This commit is contained in:
2026-08-29 18:17:25 +02:00
parent 92c20d61cb
commit 282bcd163c
4 changed files with 58 additions and 36 deletions
+10 -10
View File
@@ -1,4 +1,3 @@
use alloc::boxed::Box;
use embedded_hal_bus::i2c::AtomicDevice;
use embedded_hal_bus::util::AtomicCell;
use esp_hal::gpio::interconnect::PeripheralInput;
@@ -21,18 +20,19 @@ where
SCL: PeripheralOutput<'a> + PeripheralInput<'a>,
SDA: PeripheralOutput<'a> + PeripheralInput<'a>,
{
pub fn build(self) -> I2cBus<'a> {
pub fn build(self) -> Result<AtomicCell<InnerI2cBus<'a>>, Self> {
let config = Config::default().with_frequency(esp_hal::time::Rate::from_khz(400));
let i2c = match I2c::new(self.i2c_peripheral, config) {
Ok(bus) => bus.with_sda(self.sda).with_scl(self.scl),
match I2c::new(self.i2c_peripheral, config) {
Ok(bus) => {
let i2c = bus.with_sda(self.sda).with_scl(self.scl);
log::info!("Shared I2C bus ready on GPIO17 (SDA) and GPIO18 (SCL)");
Ok(AtomicCell::new(i2c))
}
Err(e) => {
log::error!("Shared I2C bus initialization failed for GPIO17/GPIO18: {e:?}");
panic!("Shared I2C bus initialization failed");
unimplemented!("Partial move makes it impossible to retry.");
// Err(self)
}
};
let shared_i2c = Box::leak(Box::new(AtomicCell::new(i2c)));
log::info!("Shared I2C bus ready on GPIO17 (SDA) and GPIO18 (SCL)");
shared_i2c
}
}
}
+2 -2
View File
@@ -135,7 +135,7 @@ where
RES2: OutputPin + 'static,
DC: OutputPin + 'static,
{
pub fn build(self) -> (PrimaryDisplay<'static>, [SecondaryDisplay<'static>; 3]) {
pub fn build(self) -> Result<(PrimaryDisplay<'static>, [SecondaryDisplay<'static>; 3]), Self> {
let spi = Spi::new(
self.spi_peripheral,
SpiConfig::default()
@@ -178,7 +178,7 @@ where
}
.build();
(primary, secondaries)
Ok((primary, secondaries))
}
}
+5 -5
View File
@@ -15,19 +15,20 @@ pub struct TertiaryDisplayPinConfiguration<'a> {
}
impl<'a> TertiaryDisplayPinConfiguration<'a> {
pub fn build(self) -> TertiaryDisplay<'a> {
pub fn build(self) -> Result<TertiaryDisplay<'a>, Self> {
let device = AtomicDevice::new(self.i2c);
let mut lcd = CharacterDisplayPCF8574T::new(device, LcdDisplayType::Lcd16x2, Delay::new());
match lcd.init() {
Ok(()) => {
log::info!("I2C tertiary LCD initialized on PCF8574T at address 0x27");
TertiaryDisplay {
Ok(TertiaryDisplay {
display: lcd,
charset: create_custom_char_set!(),
}
})
}
Err(e) => {
panic!("I2C tertiary LCD init failed on PCF8574T at address 0x27: {e}");
log::error!("I2C tertiary LCD init failed on PCF8574T at address 0x27: {e}");
Err(self)
}
}
}
@@ -407,4 +408,3 @@ pub const BOX: CharMap = [
];
pub const EXAMPLE: CustomCharset = create_custom_char_set!(HEART, BOX, EMPTY);