diff --git a/tprais_semestralka1/src/bin/main.rs b/tprais_semestralka1/src/bin/main.rs index e924a11..5d63a68 100644 --- a/tprais_semestralka1/src/bin/main.rs +++ b/tprais_semestralka1/src/bin/main.rs @@ -9,7 +9,7 @@ // TODO WARNING core 1 should be logic, core 0 wifi, its flipped now use embassy_executor::Spawner; -use embassy_futures::select::{select, select3, Either, Either3}; +use embassy_futures::select::{select, Either}; use embassy_net::{Runner, StackResources}; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::signal::Signal; @@ -23,7 +23,6 @@ use esp_hal::{ clock::CpuClock, gpio::InputConfig, gpio::{Input, Pull}, - i2c::master::{Config as I2cConfig, I2c}, rng::Rng, system::{CpuControl, Stack}, timer::timg::TimerGroup, @@ -46,7 +45,6 @@ use projekt_final::mqtt::client::{ }; extern crate alloc; -use alloc::format; static APP_CORE_STACK: StaticCell> = StaticCell::new(); static EXECUTOR_CORE1: StaticCell = StaticCell::new(); @@ -95,20 +93,20 @@ async fn main(spawner: Spawner) -> ! { let seed = (rng.random() as u64) << 32 | rng.random() as u64; - // Start core 1 for WiFi and MQTT (network stack created there) + let config = InputConfig::default().with_pull(Pull::Down); + let button_select = Input::new(peripherals.GPIO32, config); + let button_next = Input::new(peripherals.GPIO35, config); + + // Core1 is now general stuff and core 0 is for wifi and mqtt let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL); let _guard = cpu_control .start_app_core(APP_CORE_STACK.init(Stack::new()), move || { let executor = EXECUTOR_CORE1.init(esp_hal_embassy::Executor::new()); executor.run(|spawner| { - spawner - .spawn(core1_network_task( - spawner, - controller, - wifi_interface, - seed, - )) - .ok(); + // Replace with the sensor reading + // spawner + // .spawn(button_detection_task(button_select, button_next)) + // .ok(); }); }) .unwrap(); @@ -117,13 +115,10 @@ async fn main(spawner: Spawner) -> ! { NETWORK_READY.wait().await; info!("Network ready, starting core 0 tasks"); - let config = InputConfig::default().with_pull(Pull::Down); - let button_select = Input::new(peripherals.GPIO32, config); - let button_next = Input::new(peripherals.GPIO35, config); + // Start core 0 for WiFi and MQTT (network stack created there) spawner - .spawn(button_detection_task(button_select, button_next)) + .spawn(network_task(spawner, controller, wifi_interface, seed)) .unwrap(); - mqtt_subscribe("esp32/read").await; mqtt_publish("esp32/imu", b"online", QualityOfService::QoS1, false).await; @@ -133,52 +128,22 @@ async fn main(spawner: Spawner) -> ! { let mut last_mqtt_publish = Instant::now(); let mqtt_publish_interval = Duration::from_secs(3); - // loop { - // match select3( - // mqtt_rx.receive(), - // // imu_rx.receive(), - // Timer::after(Duration::from_secs(5)), - // ) - // .await - // { - // Either3::First(msg) => { - // mqtt_msg_count += 1; - // handle_mqtt_message(msg).await; - // display::api::set_mqtt_status(true, mqtt_msg_count).await; - // } - // Either3::Second(mut reading) => { - // // Drain zabezpečuje, že 'reading' je najčerstvejšia možná hodnota - // let mut drained = 0; - // while let Ok(next) = imu_rx.try_receive() { - // reading = next; - // drained += 1; - // } - - // imu_reading_count += 1; - // display::api::show_imu(reading); - - // // 3. Nahraďte pôvodnú podmienku týmto časovým zámkom - // if last_mqtt_publish.elapsed() >= mqtt_publish_interval { - // let payload = client::encode_imu_json(&reading); - // client::mqtt_set_imu_payload(payload); - // last_mqtt_publish = Instant::now(); - // } - // } - // Either3::Third(_) => { - // crate::mpu::api::IMU_CHANNEL.clear(); - // info!( - // "IMU heartbeat: force-cleared queue, {} readings total, {} mqtt drops", - // imu_reading_count, mqtt_publish_drops - // ); - // } - // } - // } - loop {} + loop { + match select(mqtt_rx.receive(), Timer::after(Duration::from_secs(5))).await { + Either::First(msg) => { + mqtt_msg_count += 1; + handle_mqtt_message(msg).await; + } + Either::Second(_) => { + info!("Ahoj",); + } + } + } } -// Runs on core 1 - creates and owns the network stack +// Runs on core0 - creates and owns the network stack #[embassy_executor::task] -async fn core1_network_task( +async fn network_task( spawner: Spawner, controller: WifiController<'static>, wifi_interface: WifiDevice<'static>, @@ -211,7 +176,7 @@ async fn core1_network_task( Timer::after(Duration::from_millis(500)).await; } - // Signal core 0 that network is ready + // Signal core1 that network is ready NETWORK_READY.signal(()); spawner.spawn(mqtt_task(stack)).ok(); @@ -259,26 +224,3 @@ async fn connection_task(mut controller: WifiController<'static>) { async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) { runner.run().await } - -#[embassy_executor::task] -async fn button_detection_task(mut select_btn: Input<'static>, mut next_btn: Input<'static>) { - loop { - // match select( - // select_btn.wait_for_rising_edge(), - // next_btn.wait_for_rising_edge(), - // ) - // .await - // { - // Either::First(_) => { - // info!("Detection: GPIO 32 (Select) triggered!"); - // display::api::push_key(Key::enter()).await; - // } - // Either::Second(_) => { - // info!("Detection: GPIO 35 (Next) triggered!"); - // display::api::push_key(Key::tab()).await - // } - // } - // Debounce: prevent mechanical bouncing from double-triggering - embassy_time::Timer::after(embassy_time::Duration::from_millis(200)).await; - } -}