add peripherals module which has shared state of all peripherals

This commit was merged in pull request #47.
This commit is contained in:
2026-08-29 17:57:08 +00:00
parent 282bcd163c
commit a31c818b37
18 changed files with 207 additions and 99 deletions
+54 -33
View File
@@ -7,6 +7,7 @@
)]
#![deny(clippy::large_stack_frames)]
use alloc::boxed::Box;
use creaturedex::background_tasks;
use creaturedex::drivers::i2c_bus::I2cBusPinConfiguration;
use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
@@ -15,11 +16,14 @@ use creaturedex::drivers::spi_bus;
use creaturedex::drivers::tertiary_lcd::{
BOX, EMPTY, EXAMPLE, HEART, TertiaryDisplay, TertiaryDisplayPinConfiguration,
};
use creaturedex::navigation::navigation::Mutex;
use creaturedex::navigation::inputs::Inputs;
use creaturedex::navigation::navigation::{self};
use creaturedex::navigation::outputs::Outputs;
use creaturedex::storage::cardstore::CardStore;
use creaturedex::storage::store::Store;
use creaturedex::peripherals::Peripherals;
use creaturedex::peripherals::storage::cardstore::CardStore;
use creaturedex::peripherals::storage::flash_store::FlashStore;
use creaturedex::peripherals::storage::store::Store;
use embassy_executor::Spawner;
use embedded_graphics::pixelcolor::BinaryColor;
use embedded_graphics::{
@@ -58,41 +62,44 @@ async fn main(spawner: Spawner) {
creaturedex::logging::init();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
let esp_peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: HEAP_SIZE);
let sw_interrupt =
esp_hal::interrupt::software::SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_hal::interrupt::software::SoftwareInterruptControl::new(esp_peripherals.SW_INTERRUPT);
let timg0 = TimerGroup::new(esp_peripherals.TIMG0);
esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0);
let (primary, mut secondaries) = retry(
5,
spi_bus::DisplayPinConfiguration {
spi_peripheral: peripherals.SPI2,
sck: peripherals.GPIO36,
mosi: peripherals.GPIO35,
miso: peripherals.GPIO37,
cs_primary: peripherals.GPIO11,
cs_secondary_1: peripherals.GPIO13,
cs_secondary_2: peripherals.GPIO14,
cs_secondary_3: peripherals.GPIO21,
reset_primary: peripherals.GPIO10,
reset_secondary: peripherals.GPIO47,
dc_pin: peripherals.GPIO12,
spi_peripheral: esp_peripherals.SPI2,
sck: esp_peripherals.GPIO36,
mosi: esp_peripherals.GPIO35,
miso: esp_peripherals.GPIO37,
cs_primary: esp_peripherals.GPIO11,
cs_secondary_1: esp_peripherals.GPIO13,
cs_secondary_2: esp_peripherals.GPIO14,
cs_secondary_3: esp_peripherals.GPIO21,
reset_primary: esp_peripherals.GPIO10,
reset_secondary: esp_peripherals.GPIO47,
dc_pin: esp_peripherals.GPIO12,
},
spi_bus::DisplayPinConfiguration::build,
);
// Initialize the shared I2C bus using the refactored i2c_bus module.
let shared_i2c = {
use alloc::boxed::Box;
let bus = retry(5, I2cBusPinConfiguration {
i2c_peripheral: peripherals.I2C0,
scl: peripherals.GPIO18,
sda: peripherals.GPIO17,
}, I2cBusPinConfiguration::build);
let bus = retry(
5,
I2cBusPinConfiguration {
i2c_peripheral: esp_peripherals.I2C0,
scl: esp_peripherals.GPIO18,
sda: esp_peripherals.GPIO17,
},
I2cBusPinConfiguration::build,
);
Box::leak(Box::new(bus))
};
@@ -113,12 +120,26 @@ async fn main(spawner: Spawner) {
);
}
let store = Store::new(peripherals.FLASH).expect("Could not initialize Flash Store");
let cardstore = CardStore::new(store).expect("Could not initialize Card Store");
let flash_store = Box::leak(Box::new(
FlashStore::new(esp_peripherals.FLASH).expect("Could not initialize Flash Store"),
));
let rtc = Rtc::new(peripherals.LPWR);
let card_store = CardStore::new(flash_store)
.await
.expect("Could not initialize Card Store");
let store = Store {
flash_store,
card_store,
};
let rtc = Rtc::new(esp_peripherals.LPWR);
//setup_wifi(peripherals.WIFI, peripherals.FLASH, &spawner).await;
let peripherals = Box::leak(Box::new(Peripherals {
store: Mutex::new(store),
rtc,
}));
let mut nfc_driver = NfcPn532Driver::new(AtomicDevice::new(shared_i2c));
if nfc_driver.configure_sam().await.is_ok() {
log::info!("NFC SAM configuration successful on the shared I2C bus");
@@ -128,12 +149,12 @@ async fn main(spawner: Spawner) {
let button_config = InputConfig::default().with_pull(Pull::Up);
let inputs = Inputs {
up: Input::new(peripherals.GPIO38, button_config),
down: Input::new(peripherals.GPIO39, button_config),
left: Input::new(peripherals.GPIO40, button_config),
right: Input::new(peripherals.GPIO41, button_config),
back: Input::new(peripherals.GPIO8, button_config),
ok: Input::new(peripherals.GPIO0, button_config),
up: Input::new(esp_peripherals.GPIO38, button_config),
down: Input::new(esp_peripherals.GPIO39, button_config),
left: Input::new(esp_peripherals.GPIO40, button_config),
right: Input::new(esp_peripherals.GPIO41, button_config),
back: Input::new(esp_peripherals.GPIO8, button_config),
ok: Input::new(esp_peripherals.GPIO0, button_config),
};
display_shit(&mut secondaries[0], "foo 1");
@@ -153,9 +174,9 @@ async fn main(spawner: Spawner) {
};
log::info!("Setup complete, entering main loop");
spawner.spawn(navigation::run(inputs, outputs).expect("run task failed"));
spawner.spawn(navigation::run(inputs, outputs, peripherals).expect("run task failed"));
spawner.spawn(
background_tasks::nfc_scanner(nfc_driver, cardstore, rtc).expect("nfc scanner task failed"),
background_tasks::nfc_scanner(nfc_driver, peripherals).expect("nfc scanner task failed"),
);
core::future::pending::<()>().await
}