// src/uart/usart1.rs
use defmt::info;
use embassy_executor::Spawner;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::pipe::Pipe;
use embassy_time::Duration;

use crate::uart::safety::{preflight_and_suggest_yield_period, RX_PIPE_CAP, TX_PIPE_CAP};
use crate::uart::driver::{spawn_for, UartHandle};

// Static pipes and buffers
static UART1_TX_PIPE: Pipe<CriticalSectionRawMutex, TX_PIPE_CAP> = Pipe::new();
static UART1_RX_PIPE: Pipe<CriticalSectionRawMutex, RX_PIPE_CAP> = Pipe::new();

pub fn setup_and_spawn(
    spawner: &Spawner,
    uart: embassy_stm32::usart::BufferedUart<'static>,
    baudrate: u32,
) -> UartHandle {
    let yield_period: Duration = preflight_and_suggest_yield_period(baudrate);
    info!("USART1 initialized");
    spawn_for(spawner, uart, &UART1_TX_PIPE, &UART1_RX_PIPE, yield_period)
}
