strip options

This commit is contained in:
Jörn-Michael Miehe 2024-03-24 23:06:54 +00:00
parent e9b63dca16
commit cd8b254375

View file

@ -5,21 +5,76 @@ type DMXUniverse<const DMX_LEN: usize> = &'static mut [u8; DMX_LEN];
type DMXTransfer<const DMX_LEN: usize> = dma::Transfer<dma::R, DMXUniverse<DMX_LEN>, TxDma>;
struct TxDMAIdle<const DMX_LEN: usize> {
dma: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>,
buffer: Option<DMXUniverse<DMX_LEN>>,
tx: TxDma,
buffer: DMXUniverse<DMX_LEN>,
}
struct TxDMABusy<const DMX_LEN: usize> {
transfer: Option<DMXTransfer<DMX_LEN>>,
transfer: DMXTransfer<DMX_LEN>,
}
enum TxDMA<const DMX_LEN: usize> {
Idle(TxDMAIdle<DMX_LEN>),
Busy(TxDMABusy<DMX_LEN>),
Idle(Option<TxDMAIdle<DMX_LEN>>),
Busy(Option<TxDMABusy<DMX_LEN>>),
}
impl<const DMX_LEN: usize> TxDMA<DMX_LEN> {
fn new(tx: TxDma, buffer: DMXUniverse<DMX_LEN>) -> Self {
TxDMA::Idle(Some(TxDMAIdle { tx, buffer }))
}
fn start_sending(&mut self, tx_universe: &[u8]) {
let TxDMA::Idle(idle) = self else {
return;
};
let Some(TxDMAIdle { tx, buffer }) = idle.take() else {
panic!();
};
buffer.copy_from_slice(tx_universe);
*self = TxDMA::Busy(Some(TxDMABusy {
transfer: tx.write(buffer),
}));
}
fn wait(&mut self) {
let TxDMA::Busy(busy) = self else {
return;
};
let Some(TxDMABusy { transfer }) = busy.take() else {
panic!();
};
let (buffer, tx) = transfer.wait();
*self = TxDMA::Idle(Some(TxDMAIdle { tx, buffer }));
}
fn is_idle(&mut self) -> bool {
match self {
TxDMA::Idle(_) => true,
TxDMA::Busy(busy) => {
let Some(TxDMABusy { transfer }) = busy.take() else {
panic!();
};
let is_done = transfer.is_done();
busy.replace(TxDMABusy { transfer });
if is_done {
self.wait();
}
is_done
}
}
}
}
pub struct DMX<const DMX_LEN: usize> {
tx_universe: Option<DMXUniverse<DMX_LEN>>,
tx_universe: DMXUniverse<DMX_LEN>,
sender: TxDMA<DMX_LEN>,
}
@ -69,11 +124,20 @@ impl<const DMX_LEN: usize> DMX<DMX_LEN> {
);
Self {
tx_universe: Some(tx_universe),
sender: TxDMA::Idle(TxDMAIdle {
dma: Some(serial.tx.with_dma(dma1.4)),
buffer: Some(tx_buffer),
}),
tx_universe,
sender: TxDMA::new(serial.tx.with_dma(dma1.4), tx_buffer),
}
}
pub fn start_tx(&mut self) {
self.sender.start_sending(self.tx_universe);
}
pub fn wait_tx(&mut self) {
self.sender.wait();
}
pub fn tx_is_idle(&mut self) -> bool {
return self.sender.is_idle();
}
}