DMX_LEN generic

This commit is contained in:
Jörn-Michael Miehe 2024-03-21 23:31:59 +00:00
parent c27c980097
commit dd8dfeacd6
2 changed files with 14 additions and 12 deletions

View file

@ -2,18 +2,20 @@ use cortex_m::singleton;
use stm32f1xx_hal::{dma, pac, prelude::*, rcc, serial}; use stm32f1xx_hal::{dma, pac, prelude::*, rcc, serial};
type _TxDma = dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>; type _TxDma = dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>;
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>;
pub struct DMXIdle { pub struct DMXIdle<const DMX_LEN: usize> {
tx: Option<_TxDma>, tx: Option<_TxDma>,
txbuffer: Option<&'static mut [u8; 512]>, txbuffer: Option<_DMXUniverse<DMX_LEN>>,
} }
pub enum DMX { pub enum DMX<const DMX_LEN: usize = 512> {
Idle(DMXIdle), Idle(DMXIdle<DMX_LEN>),
Busy(Option<dma::Transfer<dma::R, &'static mut [u8; 512], _TxDma>>), Busy(Option<_DMXTransfer<DMX_LEN>>),
} }
impl DMX { impl<const DMX_LEN: usize> DMX<DMX_LEN> {
pub fn new<PINS>( pub fn new<PINS>(
mut serial: serial::Serial<pac::USART1, PINS>, mut serial: serial::Serial<pac::USART1, PINS>,
channel: dma::dma1::C4, channel: dma::dma1::C4,
@ -30,7 +32,7 @@ impl DMX {
}) })
} }
pub fn send(&mut self, data: &[u8; 512]) { pub fn send(&mut self, data: &[u8; DMX_LEN]) {
if let Self::Busy(_) = self { if let Self::Busy(_) = self {
self.wait(); self.wait();
} }
@ -39,10 +41,10 @@ impl DMX {
panic!("Broken DMX State!") panic!("Broken DMX State!")
}; };
let txbuffer = idle let txbuffer = idle.txbuffer.take().unwrap_or_else(|| {
.txbuffer let foo = singleton!(: [u8; 512] = [0u8; 512]).unwrap();
.take() (&mut foo[..DMX_LEN]).try_into().unwrap()
.unwrap_or_else(|| singleton!(: [u8; 512] = [0; 512]).unwrap()); });
let tx = idle.tx.take().unwrap(); let tx = idle.tx.take().unwrap();
txbuffer.copy_from_slice(data); txbuffer.copy_from_slice(data);

View file

@ -29,7 +29,7 @@ mod app {
#[local] #[local]
struct Local { struct Local {
dmx: DMX, dmx: DMX<512>,
led: gpio::gpioc::PC13<gpio::Output>, led: gpio::gpioc::PC13<gpio::Output>,
} }