still cleaning up

This commit is contained in:
Priec
2026-05-03 20:07:33 +02:00
parent b542fb02a2
commit d8b33e50a2

View File

@@ -9,7 +9,7 @@
// TODO WARNING core 1 should be logic, core 0 wifi, its flipped now // TODO WARNING core 1 should be logic, core 0 wifi, its flipped now
use embassy_executor::Spawner; 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_net::{Runner, StackResources};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::signal::Signal; use embassy_sync::signal::Signal;
@@ -23,7 +23,6 @@ use esp_hal::{
clock::CpuClock, clock::CpuClock,
gpio::InputConfig, gpio::InputConfig,
gpio::{Input, Pull}, gpio::{Input, Pull},
i2c::master::{Config as I2cConfig, I2c},
rng::Rng, rng::Rng,
system::{CpuControl, Stack}, system::{CpuControl, Stack},
timer::timg::TimerGroup, timer::timg::TimerGroup,
@@ -46,7 +45,6 @@ use projekt_final::mqtt::client::{
}; };
extern crate alloc; extern crate alloc;
use alloc::format;
static APP_CORE_STACK: StaticCell<Stack<8192>> = StaticCell::new(); static APP_CORE_STACK: StaticCell<Stack<8192>> = StaticCell::new();
static EXECUTOR_CORE1: StaticCell<esp_hal_embassy::Executor> = StaticCell::new(); static EXECUTOR_CORE1: StaticCell<esp_hal_embassy::Executor> = StaticCell::new();
@@ -95,20 +93,20 @@ async fn main(spawner: Spawner) -> ! {
let seed = (rng.random() as u64) << 32 | rng.random() as u64; 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 mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);
let _guard = cpu_control let _guard = cpu_control
.start_app_core(APP_CORE_STACK.init(Stack::new()), move || { .start_app_core(APP_CORE_STACK.init(Stack::new()), move || {
let executor = EXECUTOR_CORE1.init(esp_hal_embassy::Executor::new()); let executor = EXECUTOR_CORE1.init(esp_hal_embassy::Executor::new());
executor.run(|spawner| { executor.run(|spawner| {
spawner // Replace with the sensor reading
.spawn(core1_network_task( // spawner
spawner, // .spawn(button_detection_task(button_select, button_next))
controller, // .ok();
wifi_interface,
seed,
))
.ok();
}); });
}) })
.unwrap(); .unwrap();
@@ -117,13 +115,10 @@ async fn main(spawner: Spawner) -> ! {
NETWORK_READY.wait().await; NETWORK_READY.wait().await;
info!("Network ready, starting core 0 tasks"); info!("Network ready, starting core 0 tasks");
let config = InputConfig::default().with_pull(Pull::Down); // Start core 0 for WiFi and MQTT (network stack created there)
let button_select = Input::new(peripherals.GPIO32, config);
let button_next = Input::new(peripherals.GPIO35, config);
spawner spawner
.spawn(button_detection_task(button_select, button_next)) .spawn(network_task(spawner, controller, wifi_interface, seed))
.unwrap(); .unwrap();
mqtt_subscribe("esp32/read").await; mqtt_subscribe("esp32/read").await;
mqtt_publish("esp32/imu", b"online", QualityOfService::QoS1, false).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 mut last_mqtt_publish = Instant::now();
let mqtt_publish_interval = Duration::from_secs(3); let mqtt_publish_interval = Duration::from_secs(3);
// loop { loop {
// match select3( match select(mqtt_rx.receive(), Timer::after(Duration::from_secs(5))).await {
// mqtt_rx.receive(), Either::First(msg) => {
// // imu_rx.receive(), mqtt_msg_count += 1;
// Timer::after(Duration::from_secs(5)), handle_mqtt_message(msg).await;
// ) }
// .await Either::Second(_) => {
// { info!("Ahoj",);
// 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 {}
} }
// Runs on core 1 - creates and owns the network stack // Runs on core0 - creates and owns the network stack
#[embassy_executor::task] #[embassy_executor::task]
async fn core1_network_task( async fn network_task(
spawner: Spawner, spawner: Spawner,
controller: WifiController<'static>, controller: WifiController<'static>,
wifi_interface: WifiDevice<'static>, wifi_interface: WifiDevice<'static>,
@@ -211,7 +176,7 @@ async fn core1_network_task(
Timer::after(Duration::from_millis(500)).await; Timer::after(Duration::from_millis(500)).await;
} }
// Signal core 0 that network is ready // Signal core1 that network is ready
NETWORK_READY.signal(()); NETWORK_READY.signal(());
spawner.spawn(mqtt_task(stack)).ok(); 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>>) { async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) {
runner.run().await 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;
}
}