diff --git a/Cargo.toml b/Cargo.toml index 2ecb546..7596f76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [ @@ -40,7 +41,6 @@ embedded-graphics = "0.8.2" embedded-graphics-core = "0.4.1" base64 = { version = "0.22", default-features = false, features = ["alloc"] } - # 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. @@ -55,4 +55,4 @@ opt-level = 's' [features] default = [] -wokwi = [] \ No newline at end of file +wokwi = [] diff --git a/src/bin/main.rs b/src/bin/main.rs index 8a9feec..7cb5ab5 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -7,19 +7,15 @@ )] #![deny(clippy::large_stack_frames)] -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::navigation::input::init_navigation_button; +use creaturedex::navigation::navigation; +use creaturedex::navigation::navigation::inputs::Inputs; use creaturedex::navigation::state::NavigationState; -use creaturedex::network::wifi::setup_wifi; -use embassy_executor::Spawner; -use embassy_time::Timer; -use embedded_graphics::draw_target::DrawTarget; -use embedded_graphics::pixelcolor::Rgb565; -use embedded_graphics::prelude::RgbColor; + +use esp_hal::clock::ClockControl; use esp_hal::clock::CpuClock; -use esp_hal::delay::Delay; +use esp_hal::peripherals::Peripherals; +use esp_hal::system::SystemControl; use esp_hal::timer::timg::TimerGroup; use log::error; @@ -31,6 +27,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: esp_bootloader_esp_idf::esp_app_desc!(); @@ -39,13 +37,28 @@ esp_bootloader_esp_idf::esp_app_desc!(); clippy::large_stack_frames, reason = "it's not unusual to allocate larger buffers etc. in main" )] -#[esp_rtos::main] -async fn main(spawner: Spawner) { - esp_println::logger::init_logger_from_env(); +fn init() -> Peripherals { + esp_println::logger::init_logger_from_env(); let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); let peripherals = esp_hal::init(config); + esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: HEAP_SIZE); + + let system = SystemControl::new(peripherals.SYSTEM); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + let timg0 = TimerGroup::new_async(peripherals.TIMG0, &clocks).timer0; + embassy::init(&clocks, timg0); + + log::info!("Setup complete, entering main loop"); + + peripherals +} + +#[embassy_executor::main(executor = "esp_rtos::embassy::Executor")] +async fn main(spawner: embassy_executor::Spawner) { + let peripherals = init(); + // The following GPIO pins are in use by some feature of the module and should not be used. let _ = peripherals.GPIO27; let _ = peripherals.GPIO28; @@ -56,15 +69,7 @@ async fn main(spawner: Spawner) { let _ = peripherals.GPIO33; let _ = peripherals.GPIO34; - 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); - esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0); + let mut nav_state = NavigationState::new(); let mut display = init_primary_lcd( peripherals.SPI2, @@ -74,27 +79,8 @@ async fn main(spawner: Spawner) { peripherals.GPIO11, peripherals.GPIO10, peripherals.GPIO12, - &mut delay, ); - // setup_wifi(peripherals.WIFI, peripherals.FLASH, &spawner).await; - - log::info!("Setup complete, entering main loop"); - - let mut nav_state = NavigationState::new(); - - 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 - }; - - render_nfc_payload(&mut display, payload); - } - Timer::after_millis(10).await; - } + let mut inputs = Inputs::new(peripherals); + spawner.spawn(navigation::run(inputs).await.expect("run task failed")); } diff --git a/src/display/primary_lcd.rs b/src/display/primary_lcd.rs index efebe50..65e727d 100644 --- a/src/display/primary_lcd.rs +++ b/src/display/primary_lcd.rs @@ -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, Output<'a>, Delay>, - Output<'a>, - >, + SPIInterface, 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, ) diff --git a/src/navigation.rs b/src/navigation.rs index 6856496..daeb191 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -1,2 +1,4 @@ -pub mod input; +pub mod inputs; +pub mod main_menu; +pub mod navigation; pub mod state; diff --git a/src/navigation/input.rs b/src/navigation/input.rs deleted file mode 100644 index 07f5426..0000000 --- a/src/navigation/input.rs +++ /dev/null @@ -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) -} diff --git a/src/navigation/inputs.rs b/src/navigation/inputs.rs new file mode 100644 index 0000000..4a802f3 --- /dev/null +++ b/src/navigation/inputs.rs @@ -0,0 +1,33 @@ +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 { + up: Input<'static>, + down: Input<'static>, + left: Input<'static>, + right: Input<'static>, + back: Input<'static>, + ok: Input<'static>, +} + +impl Inputs { + pub fn new(peripherals: Peripherals) -> Self { + let button_config = InputConfig::default().with_pull(Pull::Up); + Self { + 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()), + } + } +} diff --git a/src/navigation/main_menu.rs b/src/navigation/main_menu.rs new file mode 100644 index 0000000..f92903d --- /dev/null +++ b/src/navigation/main_menu.rs @@ -0,0 +1,3 @@ +use crate::navigation::inputs::Inputs; + +pub async fn show(_inputs: &mut Inputs) {} diff --git a/src/navigation/navigation.rs b/src/navigation/navigation.rs new file mode 100644 index 0000000..a83cebf --- /dev/null +++ b/src/navigation/navigation.rs @@ -0,0 +1,9 @@ +use crate::navigation::inputs::Inputs; + +#[embassy_executor::task] +//pub async fn run() -> Result { +pub async fn run(mut inputs: Inputs) { + loop { + crate::navigation::main_menu::show(&mut inputs).await + } +}