Introduced the SingleSequencer and a more complex Sequencer

This commit is contained in:
huntc
2022-02-04 19:11:15 +11:00
parent 9e36ede363
commit 965a5f2c3f
5 changed files with 134 additions and 68 deletions

View File

@ -314,26 +314,58 @@ impl<'s> Sequence<'s> {
}
}
/// A single sequence that can be started and stopped.
/// Takes at one sequence along with its configuration.
#[non_exhaustive]
pub struct SingleSequencer<'d, 's, T: Instance> {
pub sequencer: Sequencer<'d, 's, T>,
}
impl<'d, 's, T: Instance> SingleSequencer<'d, 's, T> {
/// Create a new sequencer
pub fn new(pwm: &'s mut SequencePwm<'d, T>, sequence: Sequence<'s>) -> Self {
Self {
sequencer: Sequencer::new(pwm, sequence, None),
}
}
/// Start or restart playback.
#[inline(always)]
pub fn start(&self, times: SingleSequenceMode) -> Result<(), Error> {
let (start_seq, times) = match times {
SingleSequenceMode::Times(n) if n == 1 => (StartSequence::One, SequenceMode::Loop(1)),
SingleSequenceMode::Times(n) if n & 1 == 1 => {
(StartSequence::One, SequenceMode::Loop((n / 2) + 1))
}
SingleSequenceMode::Times(n) => (StartSequence::Zero, SequenceMode::Loop(n / 2)),
SingleSequenceMode::Infinite => (StartSequence::Zero, SequenceMode::Infinite),
};
self.sequencer.start(start_seq, times)
}
}
/// A composition of sequences that can be started and stopped.
/// Takes at least one sequence along with its configuration.
/// Optionally takes a second sequence and its configuration.
/// In the case where no second sequence is provided then the first sequence
/// is used.
#[non_exhaustive]
pub struct Sequences<'d, 's, T: Instance> {
pub pwm: &'s mut SequencePwm<'d, T>,
pub struct Sequencer<'d, 's, T: Instance> {
_pwm: &'s mut SequencePwm<'d, T>,
sequence0: Sequence<'s>,
sequence1: Option<Sequence<'s>>,
}
impl<'d, 's, T: Instance> Sequences<'d, 's, T> {
impl<'d, 's, T: Instance> Sequencer<'d, 's, T> {
/// Create a new double sequence. In the absence of sequence 1, sequence 0
/// will be used twice in the one loop.
pub fn new(
pwm: &'s mut SequencePwm<'d, T>,
sequence0: Sequence<'s>,
sequence1: Option<Sequence<'s>>,
) -> Self {
Sequences {
pwm,
Sequencer {
_pwm: pwm,
sequence0,
sequence1,
}
@ -341,7 +373,7 @@ impl<'d, 's, T: Instance> Sequences<'d, 's, T> {
/// Start or restart playback. The sequence mode applies to both sequences combined as one.
#[inline(always)]
pub fn start(&self, times: SequenceMode) -> Result<(), Error> {
pub fn start(&self, start_seq: StartSequence, times: SequenceMode) -> Result<(), Error> {
let sequence0 = &self.sequence0;
let alt_sequence = self.sequence1.as_ref().unwrap_or(&self.sequence0);
@ -352,7 +384,7 @@ impl<'d, 's, T: Instance> Sequences<'d, 's, T> {
return Err(Error::SequenceTooLong);
}
if let SequenceMode::Times(0) = times {
if let SequenceMode::Loop(0) = times {
return Err(Error::SequenceTimesAtLeastOne);
}
@ -391,41 +423,27 @@ impl<'d, 's, T: Instance> Sequences<'d, 's, T> {
// defensive before seqstart
compiler_fence(Ordering::SeqCst);
let seqstart_index = if start_seq == StartSequence::One {
1
} else {
0
};
match times {
// just the one time, no loop count
SequenceMode::Times(1) => {
r.loop_.write(|w| w.cnt().disabled());
// tasks_seqstart() doesn't exist in all svds so write its bit instead
r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) });
}
// loop count is how many times to play BOTH sequences
// 2 total (1 x 2)
// 3 total, (2 x 2) - 1
SequenceMode::Times(n) => {
let odd = n & 1 == 1;
let times = if odd { (n / 2) + 1 } else { n / 2 };
r.loop_.write(|w| unsafe { w.cnt().bits(times) });
// we can subtract 1 by starting at seq1 instead of seq0
if odd {
// tasks_seqstart() doesn't exist in all svds so write its bit instead
r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) });
} else {
// tasks_seqstart() doesn't exist in all svds so write its bit instead
r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) });
}
SequenceMode::Loop(n) => {
r.loop_.write(|w| unsafe { w.cnt().bits(n) });
}
// to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again
SequenceMode::Infinite => {
r.loop_.write(|w| unsafe { w.cnt().bits(0x1) });
r.shorts.write(|w| w.loopsdone_seqstart0().enabled());
// tasks_seqstart() doesn't exist in all svds so write its bit instead
r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) });
}
}
// tasks_seqstart() doesn't exist in all svds so write its bit instead
r.tasks_seqstart[seqstart_index].write(|w| unsafe { w.bits(0x01) });
Ok(())
}
@ -447,22 +465,35 @@ impl<'d, 's, T: Instance> Sequences<'d, 's, T> {
}
}
impl<'d, 's, T: Instance> Drop for Sequences<'d, 's, T> {
impl<'d, 's, T: Instance> Drop for Sequencer<'d, 's, T> {
fn drop(&mut self) {
let _ = self.stop();
}
}
/// How many times to run the sequence
/// How many times to run a single sequence
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum SingleSequenceMode {
/// Run a single sequence n Times total.
Times(u16),
/// Repeat until `stop` is called.
Infinite,
}
/// Which sequence to start a loop with
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum StartSequence {
/// Start with Sequence 0
Zero,
/// Start with Sequence 1
One,
}
/// How many loops to run two sequences
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum SequenceMode {
/// Run sequence n Times total.
/// 1 = Run sequence 0 once
/// 2 = Run sequence 0 and then sequence 1
/// 3 = Run sequence 1, sequence 0, sequence 1 and then sequence 0
/// 4 = Run sequence 0, sequence 1, sequence 0 and then sequence 1
/// ...and so on.
Times(u16),
/// Run two sequences n loops i.e. (n * (seq0 + seq1.unwrap_or(seq0)))
Loop(u16),
/// Repeat until `stop` is called.
Infinite,
}