Own the sequence buffer

This approach owns the sequence buffers which, while introducing an extra move, it eliminates the need to guard the lifetime of the sequence buffer. Given ownership, the buffer will be retained until the PWM sequence task is stopped.
This commit is contained in:
huntc
2022-01-29 15:26:31 +11:00
parent 9ac52a768b
commit 482389a691
4 changed files with 108 additions and 68 deletions

View File

@ -30,19 +30,6 @@ const RES: u16 = 0x8000;
// line is assumed to be P1_05.
#[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
// Declare the bits of 24 bits
let mut color_seq_words = [
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // G
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
T1H, T1H, T1H, T1H, T1H, T1H, T1H, T1H, // B
];
let color_seq = Sequence::new(&mut color_seq_words, SequenceConfig::default());
let mut reset_seq_words = [RES; 1];
let mut reset_seq_config = SequenceConfig::default();
reset_seq_config.end_delay = 799; // 50us (20 ticks * 40) - 1 tick because we've already got one RES;
let reset_seq = Sequence::new(&mut reset_seq_words, reset_seq_config);
let mut config = Config::default();
config.sequence_load = SequenceLoad::Common;
config.prescaler = Prescaler::Div1;
@ -51,7 +38,21 @@ async fn main(_spawner: Spawner, p: Peripherals) {
p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config,
));
unwrap!(pwm.start(color_seq, Some(reset_seq), SequenceMode::Times(2)));
// Declare the bits of 24 bits
let color_seq = Sequence::new(
[
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // G
T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
T1H, T1H, T1H, T1H, T1H, T1H, T1H, T1H, // B
],
SequenceConfig::default(),
);
let mut reset_seq_config = SequenceConfig::default();
reset_seq_config.end_delay = 799; // 50us (20 ticks * 40) - 1 tick because we've already got one RES;
let reset_seq = Sequence::new([RES], reset_seq_config);
unwrap!(pwm.start(color_seq, reset_seq, SequenceMode::Times(2)));
Timer::after(Duration::from_millis(1000)).await;
@ -59,9 +60,9 @@ async fn main(_spawner: Spawner, p: Peripherals) {
let mut bit_value = T0H;
loop {
if let (Some(color_seq), Some(reset_seq)) = pwm.stop() {
if let (Some(mut color_seq), Some(reset_seq)) = pwm.stop() {
color_seq.words[color_bit] = bit_value;
unwrap!(pwm.start(color_seq, Some(reset_seq), SequenceMode::Times(2)));
unwrap!(pwm.start(color_seq, reset_seq, SequenceMode::Times(2)));
}
Timer::after(Duration::from_millis(50)).await;