implement basic embassy framework

This commit is contained in:
2026-08-14 19:14:44 +00:00
parent 7ce3a60a08
commit 88dce365ad
8 changed files with 71 additions and 63 deletions
+3 -3
View File
@@ -9,7 +9,7 @@ name = "creaturedex"
path = "./src/bin/main.rs"
[dependencies]
esp-hal = { version = "~1.1.0", features = ["esp32s3", "log-04", "unstable"] }
esp-hal = { version = "~1.1.0", features = ["esp32s3", "log-04", "unstable"] } #, "embassy", "embassy-time-timg0", "embassy-executor-thread"] }
esp-rtos = { version = "0.3.0", features = [
"embassy",
@@ -24,7 +24,8 @@ log = "0.4.27"
critical-section = "1.2.0"
embedded-io = "0.7.1"
embassy-executor = { version = "0.10.0", default-features = false }
embassy-executor = { version = "0.10.0", features = [
] }
embassy-time = { version = "0.5.1", default-features = false }
esp-alloc = "0.10.0"
esp-hal-wifimanager = { git = "https://github.com/InvisibleUSA/esp-hal-wifimanager", default-features = false, features = [
@@ -44,7 +45,6 @@ ndef = "0.5.0"
# For fine tuning these settings, please refer to https://doc.rust-lang.org/cargo/reference/profiles.html
[profile.dev]
# The default debug profile is too slow and too big for resource-constrained devices.
+24 -34
View File
@@ -11,7 +11,8 @@ use creaturedex::card::mock::{MOCK_PAYLOAD, MOCK_PAYLOAD2};
use creaturedex::display::primary_lcd::init_primary_lcd;
use creaturedex::display::views::card_view::render_nfc_payload;
use creaturedex::drivers::nfc_pn532::NfcPn532Driver;
use creaturedex::navigation::input::init_navigation_button;
use creaturedex::navigation::inputs::Inputs;
use creaturedex::navigation::navigation;
use creaturedex::navigation::state::NavigationState;
use creaturedex::network::wifi::setup_wifi;
use embassy_executor::Spawner;
@@ -19,9 +20,10 @@ use embassy_time::Timer;
use embedded_graphics::draw_target::DrawTarget;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::RgbColor;
use esp_hal::time::Duration;
use esp_hal::clock::CpuClock;
use esp_hal::delay::Delay;
use esp_hal::gpio::{Input, InputConfig, Pull};
use esp_hal::time::Duration;
use esp_hal::timer::timg::TimerGroup;
use log::error;
@@ -33,6 +35,8 @@ fn panic(panic_info: &core::panic::PanicInfo) -> ! {
extern crate alloc;
const HEAP_SIZE: usize = 73744;
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
@@ -48,26 +52,15 @@ async fn main(spawner: Spawner) {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
// The following GPIO pins are in use by some feature of the module and should not be used.
let _ = peripherals.GPIO27;
let _ = peripherals.GPIO28;
let _ = peripherals.GPIO29;
let _ = peripherals.GPIO30;
let _ = peripherals.GPIO31;
let _ = peripherals.GPIO32;
let _ = peripherals.GPIO33;
let _ = peripherals.GPIO34;
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: HEAP_SIZE);
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 73744);
let mut delay = Delay::new();
let button1 = init_navigation_button(peripherals.GPIO38);
let timg0 = TimerGroup::new(peripherals.TIMG0);
let sw_interrupt =
esp_hal::interrupt::software::SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0);
log::info!("Setup complete, entering main loop");
let mut display = init_primary_lcd(
peripherals.SPI2,
peripherals.GPIO36,
@@ -76,7 +69,6 @@ async fn main(spawner: Spawner) {
peripherals.GPIO11,
peripherals.GPIO10,
peripherals.GPIO12,
&mut delay,
);
setup_wifi(peripherals.WIFI, peripherals.FLASH, &spawner).await;
@@ -85,7 +77,8 @@ async fn main(spawner: Spawner) {
let mut nav_state = NavigationState::new();
let mut nfc_driver = NfcPn532Driver::new(peripherals.I2C0, peripherals.GPIO17, peripherals.GPIO18);
let mut nfc_driver =
NfcPn532Driver::new(peripherals.I2C0, peripherals.GPIO17, peripherals.GPIO18);
let timeout = Duration::from_millis(1000);
if nfc_driver.configure_sam(timeout).is_ok() {
log::info!("NFC SAM configuration successful");
@@ -93,22 +86,19 @@ async fn main(spawner: Spawner) {
log::error!("NFC SAM configuration failed");
}
loop {
if let Ok(card_data) = nfc_driver.read_card_data() {
log::info!("NFC Card Read: {:?}", card_data);
}
log::info!("Setup complete, entering main loop");
if button1.is_low() {
log::info!("Button pressed!");
display.clear(Rgb565::BLACK).unwrap();
let payload = if nav_state.toggle_payload() {
&MOCK_PAYLOAD
} else {
&MOCK_PAYLOAD2
let mut nav_state = NavigationState::new();
let button_config = InputConfig::default().with_pull(Pull::Up);
let mut inputs = Inputs {
up: Input::new(peripherals.GPIO38, button_config.clone()),
down: Input::new(peripherals.GPIO0, button_config.clone()),
left: Input::new(peripherals.GPIO1, button_config.clone()),
right: Input::new(peripherals.GPIO2, button_config.clone()),
back: Input::new(peripherals.GPIO3, button_config.clone()),
ok: Input::new(peripherals.GPIO4, button_config.clone()),
};
render_nfc_payload(&mut display, payload);
}
Timer::after_millis(10).await;
}
spawner.spawn(navigation::run(inputs).expect("run task failed"));
}
+9 -8
View File
@@ -6,15 +6,15 @@ use embedded_hal_bus::spi::ExclusiveDevice;
use esp_hal::delay::Delay;
use esp_hal::gpio::{Level, Output, OutputConfig};
use esp_hal::peripherals::{GPIO10, GPIO11, GPIO12, GPIO35, GPIO36, GPIO37, SPI2};
use esp_hal::spi::{master::{Config, Spi}, Mode};
use esp_hal::spi::{
Mode,
master::{Config, Spi},
};
use esp_hal::time::Rate;
use ili9341::{DisplaySize240x320, Ili9341, Orientation};
pub type PrimaryLcdDisplay<'a> = Ili9341<
SPIInterface<
ExclusiveDevice<Spi<'a, esp_hal::Blocking>, Output<'a>, Delay>,
Output<'a>,
>,
SPIInterface<ExclusiveDevice<Spi<'a, esp_hal::Blocking>, Output<'a>, Delay>, Output<'a>>,
Output<'a>,
>;
@@ -26,7 +26,6 @@ pub fn init_primary_lcd(
cs: GPIO11<'static>,
reset: GPIO10<'static>,
dc: GPIO12<'static>,
delay: &mut Delay,
) -> PrimaryLcdDisplay<'static> {
let freq = Rate::from_khz(60_000);
log::info!("Display freq: {freq}");
@@ -40,8 +39,10 @@ pub fn init_primary_lcd(
.with_mosi(mosi)
.with_miso(miso);
let mut delay = Delay::new();
let cs_main = Output::new(cs, Level::High, OutputConfig::default());
let spi_dev = ExclusiveDevice::new(spi_bus, cs_main, *delay).unwrap();
let spi_dev = ExclusiveDevice::new(spi_bus, cs_main, delay).unwrap();
let reset_main = Output::new(reset, Level::High, OutputConfig::default());
let spi_dc = Output::new(dc, Level::High, OutputConfig::default());
let iface = SPIInterface::new(spi_dev, spi_dc);
@@ -49,7 +50,7 @@ pub fn init_primary_lcd(
let mut display = Ili9341::new(
iface,
reset_main,
delay,
&mut delay,
Orientation::Portrait,
DisplaySize240x320,
)
+3 -1
View File
@@ -1,2 +1,4 @@
pub mod input;
pub mod inputs;
pub mod main_menu;
pub mod navigation;
pub mod state;
-15
View File
@@ -1,15 +0,0 @@
use esp_hal::gpio::{Input, InputConfig, Pull};
use esp_hal::peripherals::GPIO38;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NavCommand {
NextCard,
PrevCard,
Select,
ScanTag,
}
pub fn init_navigation_button(pin: GPIO38<'static>) -> Input<'static> {
let button_config = InputConfig::default().with_pull(Pull::Up);
Input::new(pin, button_config)
}
+19
View File
@@ -0,0 +1,19 @@
use esp_hal::gpio::{Input, InputConfig, Pull};
use esp_hal::peripherals::Peripherals;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NavCommand {
NextCard,
PrevCard,
Select,
ScanTag,
}
pub struct Inputs {
pub up: Input<'static>,
pub down: Input<'static>,
pub left: Input<'static>,
pub right: Input<'static>,
pub back: Input<'static>,
pub ok: Input<'static>,
}
+3
View File
@@ -0,0 +1,3 @@
use crate::navigation::inputs::Inputs;
pub async fn show(_inputs: &mut Inputs) {}
+8
View File
@@ -0,0 +1,8 @@
use crate::navigation::inputs::Inputs;
#[embassy_executor::task]
pub async fn run(mut inputs: Inputs) {
loop {
crate::navigation::main_menu::show(&mut inputs).await
}
}