implicit wait()

This commit is contained in:
Jörn-Michael Miehe 2024-03-21 22:50:21 +00:00
parent 91139c792c
commit c27c980097
2 changed files with 28 additions and 20 deletions

View file

@ -31,27 +31,33 @@ impl DMX {
} }
pub fn send(&mut self, data: &[u8; 512]) { pub fn send(&mut self, data: &[u8; 512]) {
if let Self::Idle(idle) = self { if let Self::Busy(_) = self {
let txbuffer = idle self.wait();
.txbuffer
.take()
.unwrap_or_else(|| singleton!(: [u8; 512] = [0; 512]).unwrap());
let tx = idle.tx.take().unwrap();
txbuffer.copy_from_slice(data);
*self = Self::Busy(Some(tx.write(txbuffer)));
} }
let Self::Idle(idle) = self else {
panic!("Broken DMX State!")
};
let txbuffer = idle
.txbuffer
.take()
.unwrap_or_else(|| singleton!(: [u8; 512] = [0; 512]).unwrap());
let tx = idle.tx.take().unwrap();
txbuffer.copy_from_slice(data);
*self = Self::Busy(Some(tx.write(txbuffer)));
} }
pub fn wait(&mut self) { pub fn wait(&mut self) {
if let Self::Busy(xfer) = self { let Self::Busy(xfer) = self else { return };
let xfer = xfer.take().unwrap();
let (txbuffer, tx) = xfer.wait();
*self = Self::Idle(DMXIdle { let xfer = xfer.take().unwrap();
tx: Some(tx), let (txbuffer, tx) = xfer.wait();
txbuffer: Some(txbuffer),
}); *self = Self::Idle(DMXIdle {
} tx: Some(tx),
txbuffer: Some(txbuffer),
});
} }
} }

View file

@ -9,7 +9,7 @@ mod dmx;
// extern crate panic_halt; // extern crate panic_halt;
extern crate panic_semihosting; extern crate panic_semihosting;
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [PVD])] #[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [SPI1, SPI2, SPI3])]
mod app { mod app {
use cortex_m::singleton; use cortex_m::singleton;
use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer}; use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer};
@ -87,7 +87,9 @@ mod app {
// Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
// in order to configure the port. For pins 0-7, crl should be passed instead. // in order to configure the port. For pins 0-7, crl should be passed instead.
led: gpioc.pc13.into_push_pull_output(&mut gpioc.crh), led: gpioc
.pc13
.into_push_pull_output_with_state(&mut gpioc.crh, gpio::PinState::High),
}, },
init::Monotonics(mono), init::Monotonics(mono),
) )
@ -107,7 +109,7 @@ mod app {
cx.local.led.toggle(); cx.local.led.toggle();
cx.shared.delay_us.lock(|d| d.delay(1.secs())); cx.shared.delay_us.lock(|d| d.delay(1.secs()));
cx.local.dmx.wait(); // cx.local.dmx.wait();
foo::spawn().unwrap(); foo::spawn().unwrap();
} }
} }