// src/software_uart/gpio_dma_uart_tx.rs
use embassy_futures::yield_now;
use crate::software_uart::uart_emulation::encode_uart_byte_cfg;
use crate::config::UART_CFG;

pub async fn encode_uart_frames<'a>(
    pin_bit: u8,
    bytes: &[u8],
    out_buf: &'a mut [u32],
) -> usize {
    let mut offset = 0;
    for &b in bytes {
        let mut frame = [0u32; 12];
        let used = encode_uart_byte_cfg(pin_bit, b, &UART_CFG, &mut frame);

        if offset + used <= out_buf.len() {
            out_buf[offset..offset + used].copy_from_slice(&frame[..used]);
            offset += used;
        } else {
            break;
        }

        // cooperative async yield
        yield_now().await;
    }
    offset
}

