Now permits sequences to be mutated subsequently
This commit is contained in:
		@@ -31,6 +31,8 @@ pub struct SequencePwm<'d, T: Instance> {
 | 
				
			|||||||
    ch1: Option<AnyPin>,
 | 
					    ch1: Option<AnyPin>,
 | 
				
			||||||
    ch2: Option<AnyPin>,
 | 
					    ch2: Option<AnyPin>,
 | 
				
			||||||
    ch3: Option<AnyPin>,
 | 
					    ch3: Option<AnyPin>,
 | 
				
			||||||
 | 
					    sequence0: Option<Sequence<'d>>,
 | 
				
			||||||
 | 
					    sequence1: Option<Sequence<'d>>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 | 
					#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 | 
				
			||||||
@@ -125,11 +127,13 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
 | 
				
			|||||||
            ch1: ch1.degrade_optional(),
 | 
					            ch1: ch1.degrade_optional(),
 | 
				
			||||||
            ch2: ch2.degrade_optional(),
 | 
					            ch2: ch2.degrade_optional(),
 | 
				
			||||||
            ch3: ch3.degrade_optional(),
 | 
					            ch3: ch3.degrade_optional(),
 | 
				
			||||||
 | 
					            sequence0: None,
 | 
				
			||||||
 | 
					            sequence1: None,
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Start or restart playback. Takes at least one sequence along with its
 | 
					    /// Start or restart playback. Takes at least one sequence along with its
 | 
				
			||||||
    /// configuration. Optionally takes a second sequence and/or its configuration.
 | 
					    /// configuration. Optionally takes a second sequence and its configuration.
 | 
				
			||||||
    /// In the case where no second sequence is provided then the first sequence
 | 
					    /// In the case where no second sequence is provided then the first sequence
 | 
				
			||||||
    /// is used. The sequence mode applies to both sequences combined as one.
 | 
					    /// is used. The sequence mode applies to both sequences combined as one.
 | 
				
			||||||
    #[inline(always)]
 | 
					    #[inline(always)]
 | 
				
			||||||
@@ -152,7 +156,7 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
 | 
				
			|||||||
            return Err(Error::SequenceTimesAtLeastOne);
 | 
					            return Err(Error::SequenceTimesAtLeastOne);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.stop();
 | 
					        let _ = self.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let r = T::regs();
 | 
					        let r = T::regs();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -222,6 +226,9 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.sequence0 = Some(sequence0);
 | 
				
			||||||
 | 
					        self.sequence1 = sequence1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -326,9 +333,10 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Stop playback. Disables the peripheral. Does NOT clear the last duty
 | 
					    /// Stop playback. Disables the peripheral. Does NOT clear the last duty
 | 
				
			||||||
    /// cycle from the pin.
 | 
					    /// cycle from the pin. Returns any sequences previously provided to
 | 
				
			||||||
 | 
					    /// `start` so that they may be further mutated.
 | 
				
			||||||
    #[inline(always)]
 | 
					    #[inline(always)]
 | 
				
			||||||
    pub fn stop(&self) {
 | 
					    pub fn stop(&mut self) -> (Option<Sequence<'d>>, Option<Sequence<'d>>) {
 | 
				
			||||||
        let r = T::regs();
 | 
					        let r = T::regs();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        r.shorts.reset();
 | 
					        r.shorts.reset();
 | 
				
			||||||
@@ -339,6 +347,8 @@ impl<'d, T: Instance> SequencePwm<'d, T> {
 | 
				
			|||||||
        r.tasks_stop.write(|w| unsafe { w.bits(0x01) });
 | 
					        r.tasks_stop.write(|w| unsafe { w.bits(0x01) });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        r.enable.write(|w| w.enable().disabled());
 | 
					        r.enable.write(|w| w.enable().disabled());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        (self.sequence0.take(), self.sequence1.take())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -346,7 +356,7 @@ impl<'a, T: Instance> Drop for SequencePwm<'a, T> {
 | 
				
			|||||||
    fn drop(&mut self) {
 | 
					    fn drop(&mut self) {
 | 
				
			||||||
        let r = T::regs();
 | 
					        let r = T::regs();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.stop();
 | 
					        let _ = self.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if let Some(pin) = &self.ch0 {
 | 
					        if let Some(pin) = &self.ch0 {
 | 
				
			||||||
            pin.set_low();
 | 
					            pin.set_low();
 | 
				
			||||||
@@ -415,13 +425,13 @@ impl Default for SequenceConfig {
 | 
				
			|||||||
#[non_exhaustive]
 | 
					#[non_exhaustive]
 | 
				
			||||||
pub struct Sequence<'d> {
 | 
					pub struct Sequence<'d> {
 | 
				
			||||||
    /// The words comprising the sequence. Must not exceed 32767 words.
 | 
					    /// The words comprising the sequence. Must not exceed 32767 words.
 | 
				
			||||||
    pub words: &'d [u16],
 | 
					    pub words: &'d mut [u16],
 | 
				
			||||||
    /// Configuration associated with the sequence.
 | 
					    /// Configuration associated with the sequence.
 | 
				
			||||||
    pub config: SequenceConfig,
 | 
					    pub config: SequenceConfig,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl<'d> Sequence<'d> {
 | 
					impl<'d> Sequence<'d> {
 | 
				
			||||||
    pub fn new(words: &'d [u16], config: SequenceConfig) -> Self {
 | 
					    pub fn new(words: &'d mut [u16], config: SequenceConfig) -> Self {
 | 
				
			||||||
        Self { words, config }
 | 
					        Self { words, config }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,8 +13,8 @@ use embassy_nrf::Peripherals;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[embassy::main]
 | 
					#[embassy::main]
 | 
				
			||||||
async fn main(_spawner: Spawner, p: Peripherals) {
 | 
					async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			||||||
    let seq_words_1: [u16; 5] = [1000, 250, 100, 50, 0];
 | 
					    let mut seq_words_1: [u16; 5] = [1000, 250, 100, 50, 0];
 | 
				
			||||||
    let seq_words_2: [u16; 5] = [0, 50, 100, 250, 1000];
 | 
					    let mut seq_words_2: [u16; 5] = [0, 50, 100, 250, 1000];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut config = Config::default();
 | 
					    let mut config = Config::default();
 | 
				
			||||||
    config.prescaler = Prescaler::Div128;
 | 
					    config.prescaler = Prescaler::Div128;
 | 
				
			||||||
@@ -30,7 +30,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			|||||||
        p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
 | 
					        p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config,
 | 
				
			||||||
    ));
 | 
					    ));
 | 
				
			||||||
    let _ = pwm.start(
 | 
					    let _ = pwm.start(
 | 
				
			||||||
        Sequence::new(&seq_words_1, seq_config.clone()),
 | 
					        Sequence::new(&mut seq_words_1, seq_config.clone()),
 | 
				
			||||||
        None,
 | 
					        None,
 | 
				
			||||||
        SequenceMode::Infinite,
 | 
					        SequenceMode::Infinite,
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
@@ -41,7 +41,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			|||||||
    info!("pwm starting with another sequence!");
 | 
					    info!("pwm starting with another sequence!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let _ = pwm.start(
 | 
					    let _ = pwm.start(
 | 
				
			||||||
        Sequence::new(&seq_words_2, seq_config),
 | 
					        Sequence::new(&mut seq_words_2, seq_config),
 | 
				
			||||||
        None,
 | 
					        None,
 | 
				
			||||||
        SequenceMode::Infinite,
 | 
					        SequenceMode::Infinite,
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,7 +16,7 @@ use embassy_nrf::Peripherals;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[embassy::main]
 | 
					#[embassy::main]
 | 
				
			||||||
async fn main(_spawner: Spawner, p: Peripherals) {
 | 
					async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			||||||
    let seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
 | 
					    let mut seq_words: [u16; 5] = [1000, 250, 100, 50, 0];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut config = Config::default();
 | 
					    let mut config = Config::default();
 | 
				
			||||||
    config.prescaler = Prescaler::Div128;
 | 
					    config.prescaler = Prescaler::Div128;
 | 
				
			||||||
@@ -32,7 +32,7 @@ async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			|||||||
    ));
 | 
					    ));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let _ = pwm.start(
 | 
					    let _ = pwm.start(
 | 
				
			||||||
        Sequence::new(&seq_words, seq_config),
 | 
					        Sequence::new(&mut seq_words, seq_config),
 | 
				
			||||||
        None,
 | 
					        None,
 | 
				
			||||||
        SequenceMode::Infinite,
 | 
					        SequenceMode::Infinite,
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,7 +15,9 @@ use embassy_nrf::Peripherals;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// WS2812B LED light demonstration. Drives just one light.
 | 
					// WS2812B LED light demonstration. Drives just one light.
 | 
				
			||||||
// The following reference on WS2812B may be of use:
 | 
					// The following reference on WS2812B may be of use:
 | 
				
			||||||
// https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf
 | 
					// https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf.
 | 
				
			||||||
 | 
					// This demo lights up a single LED in blue. It then proceeds
 | 
				
			||||||
 | 
					// to pulsate the LED rapidly.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// In the following declarations, setting the high bit tells the PWM
 | 
					// In the following declarations, setting the high bit tells the PWM
 | 
				
			||||||
// to reverse polarity, which is what the WS2812B expects.
 | 
					// to reverse polarity, which is what the WS2812B expects.
 | 
				
			||||||
@@ -29,17 +31,17 @@ const RES: u16 = 0x8000;
 | 
				
			|||||||
#[embassy::main]
 | 
					#[embassy::main]
 | 
				
			||||||
async fn main(_spawner: Spawner, p: Peripherals) {
 | 
					async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			||||||
    // Declare the bits of 24 bits
 | 
					    // Declare the bits of 24 bits
 | 
				
			||||||
    let blue_seq_words = [
 | 
					    let mut color_seq_words = [
 | 
				
			||||||
        T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // G
 | 
					        T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // G
 | 
				
			||||||
        T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
 | 
					        T0H, T0H, T0H, T0H, T0H, T0H, T0H, T0H, // R
 | 
				
			||||||
        T1H, T1H, T1H, T1H, T1H, T1H, T1H, T1H, // B
 | 
					        T1H, T1H, T1H, T1H, T1H, T1H, T1H, T1H, // B
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
    let blue_seq = Sequence::new(&blue_seq_words, SequenceConfig::default());
 | 
					    let color_seq = Sequence::new(&mut color_seq_words, SequenceConfig::default());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let reset_seq_words = [RES; 1];
 | 
					    let mut reset_seq_words = [RES; 1];
 | 
				
			||||||
    let mut reset_seq_config = 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;
 | 
					    reset_seq_config.end_delay = 799; // 50us (20 ticks * 40) - 1 tick because we've already got one RES;
 | 
				
			||||||
    let reset_seq = Sequence::new(&reset_seq_words, reset_seq_config);
 | 
					    let reset_seq = Sequence::new(&mut reset_seq_words, reset_seq_config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut config = Config::default();
 | 
					    let mut config = Config::default();
 | 
				
			||||||
    config.sequence_load = SequenceLoad::Common;
 | 
					    config.sequence_load = SequenceLoad::Common;
 | 
				
			||||||
@@ -49,8 +51,33 @@ async fn main(_spawner: Spawner, p: Peripherals) {
 | 
				
			|||||||
        p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config,
 | 
					        p.PWM0, p.P1_05, NoPin, NoPin, NoPin, config,
 | 
				
			||||||
    ));
 | 
					    ));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    unwrap!(pwm.start(blue_seq, Some(reset_seq), SequenceMode::Times(2)));
 | 
					    unwrap!(pwm.start(color_seq, Some(reset_seq), SequenceMode::Times(2)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Timer::after(Duration::from_millis(20000)).await;
 | 
					    Timer::after(Duration::from_millis(1000)).await;
 | 
				
			||||||
    info!("Program stopped");
 | 
					
 | 
				
			||||||
 | 
					    let mut color_bit = 16;
 | 
				
			||||||
 | 
					    let mut bit_value = T0H;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    loop {
 | 
				
			||||||
 | 
					        if let (Some(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)));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Timer::after(Duration::from_millis(50)).await;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if bit_value == T0H {
 | 
				
			||||||
 | 
					            if color_bit == 20 {
 | 
				
			||||||
 | 
					                bit_value = T1H;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                color_bit += 1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            if color_bit == 16 {
 | 
				
			||||||
 | 
					                bit_value = T0H;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                color_bit -= 1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user