My personal project and infrastructure archive
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nomicon/hardware/devices/euro-yesman-1u/src/seq.rs

87 lines
1.9 KiB

const SEQ_LEN: usize = 4;
/// Represent a single layer on the sequencer
pub struct SeqLayer {
/// List of steps that are either on or off
steps: [bool; SEQ_LEN],
/// The length of the sequence to loop
length: u8,
}
impl Default for SeqLayer {
fn default() -> Self {
Self {
steps: [true; SEQ_LEN],
length: SEQ_LEN as u8,
}
}
}
impl SeqLayer {
fn toggle(&mut self, idx: usize) {
self.steps[idx] = !self.steps[idx];
}
}
/// Indicate which layer is selected
pub enum LayerSelect {
A,
B,
C,
D,
}
impl Default for LayerSelect {
fn default() -> Self {
Self::A
}
}
/// Main sequencer state structure
#[derive(Default)]
pub struct Sequencer {
a: SeqLayer,
b: SeqLayer,
c: SeqLayer,
d: SeqLayer,
active_layer: LayerSelect,
active_step: u8,
}
impl Sequencer {
/// Step to the next step, wrapping around at SEQ_LEN
pub fn step(&mut self) {
self.active_step += 1;
self.active_step %= SEQ_LEN as u8;
}
/// Get the current step for all four layers
pub fn get(&self) -> (bool, bool, bool, bool) {
let i = self.active_step as usize;
(
self.a.steps[i],
self.b.steps[i],
self.c.steps[i],
self.d.steps[i],
)
}
pub fn toggle(&mut self, idx: usize) {
assert!(idx <= 16);
match self.active_layer {
LayerSelect::A => self.a.toggle(idx),
LayerSelect::B => self.b.toggle(idx),
LayerSelect::C => self.c.toggle(idx),
LayerSelect::D => self.d.toggle(idx),
}
}
pub fn layer(&self, sel: LayerSelect) -> &[bool; SEQ_LEN] {
match sel {
LayerSelect::A => &self.a.steps,
LayerSelect::B => &self.b.steps,
LayerSelect::C => &self.c.steps,
LayerSelect::D => &self.d.steps,
}
}
}