Skip to content

Commit

Permalink
move option mutation into timer IRQ
Browse files Browse the repository at this point in the history
  • Loading branch information
vk2seb committed Nov 4, 2023
1 parent 0c0479e commit 366cf1e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 60 deletions.
97 changes: 37 additions & 60 deletions firmware/litex-fw/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn timer0_handler(state: &Mutex<RefCell<State>>, opts: &Mutex<RefCell<opt::Optio

critical_section::with(|cs| {
let state = &mut state.borrow_ref_mut(cs);
let opts = &opts.borrow_ref(cs);
let opts = &mut opts.borrow_ref_mut(cs);
state.tick(opts, uptime_ms);
});
}
Expand Down Expand Up @@ -238,31 +238,49 @@ impl State {
}
}

fn tick(&mut self, opts: &opt::Options, uptime_ms: u32) {
fn tick(&mut self, opts: &mut opt::Options, uptime_ms: u32) {

let peripherals = unsafe { pac::Peripherals::steal() };

self.breathe.tick();

self.encoder.update_ticks(uptime_ms);

unsafe {
let peripherals = pac::Peripherals::steal();

let shifter = get_shifters(&peripherals);
if self.encoder.pending_short_press() {
opts.toggle_modify();
}

let lpf = get_lpfs(&peripherals);
if self.encoder.pending_long_press() {
unsafe { reset_soc(&peripherals.CTRL); }
}

while let Ok(event) = self.midi_in.read() {
self.voice_manager.event(event, uptime_ms);
let encoder_ticks = self.encoder.pending_ticks();
if encoder_ticks > 0 {
for _ in 0..encoder_ticks {
opts.tick_up();
}
}
if encoder_ticks < 0 {
for _ in 0..(-encoder_ticks) {
opts.tick_down();
}
}

self.voice_manager.tick(uptime_ms, opts);
let shifter = get_shifters(&peripherals);

for n_voice in 0..N_VOICES {
let voice = &self.voice_manager.voices[n_voice];
shifter[n_voice].set_pitch(voice.pitch);
lpf[n_voice].set_cutoff((voice.amplitude * 8000f32) as i16);
lpf[n_voice].set_resonance(opts.resonance.value);
}
let lpf = get_lpfs(&peripherals);

while let Ok(event) = self.midi_in.read() {
self.voice_manager.event(event, uptime_ms);
}

self.voice_manager.tick(uptime_ms, opts);

for n_voice in 0..N_VOICES {
let voice = &self.voice_manager.voices[n_voice];
shifter[n_voice].set_pitch(voice.pitch);
lpf[n_voice].set_cutoff((voice.amplitude * 8000f32) as i16);
lpf[n_voice].set_resonance(opts.resonance.value);
}
}
}
Expand Down Expand Up @@ -579,10 +597,6 @@ fn main() -> ! {
let mut cycle_cnt = timer.uptime();
let mut td_us: Option<u32> = None;

let mut cur_opt: usize = 0;

let mut modif: bool = false;

timer.set_periodic_event(5); // 5ms tick

let mut spi_dma = SpiDma::new(peripherals.SPI_DMA, pac::OLED_SPI::PTR);
Expand Down Expand Up @@ -647,58 +661,21 @@ fn main() -> ! {
}
}

let (enc_short_press, enc_long_press, encoder_ticks) =
critical_section::with(|cs| {
let enc = &mut state.borrow_ref_mut(cs).encoder;
(enc.pending_short_press(),
enc.pending_long_press(),
enc.pending_ticks())
});

if enc_short_press {
modif = !modif;
}

if enc_long_press {
unsafe { reset_soc(&peripherals.CTRL); }
}


{

let opts_ro = critical_section::with(|cs| {
let opts = &mut opts.borrow_ref_mut(cs);
let opts_view = opts.view_mut();
if modif {
if encoder_ticks > 0 {
opts_view[cur_opt].tick_up();
}
if encoder_ticks < 0 {
opts_view[cur_opt].tick_down();
}
}
opts.clone()
opts.borrow_ref(cs).clone()
});

// Should always succeed if the above CS runs.
let opts_view = opts_ro.view();

if !modif {
let sel_opt = (cur_opt as i32) + encoder_ticks;
if sel_opt >= opts_view.len() as i32 {
cur_opt = opts_view.len()-1;
}
if sel_opt < 0 {
cur_opt = 0;
}
}

let vy: usize = 205;
for (n, opt) in opts_view.iter().enumerate() {
let mut font = font_small_grey;
if cur_opt == n {
if opts_ro.selected == n {
font = font_small_white;
if modif {
if opts_ro.modify {
Text::with_alignment(
"-",
Point::new(62, (vy+10*n) as i32),
Expand Down
26 changes: 26 additions & 0 deletions firmware/litex-fw/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub struct Option<T> {

#[derive(Clone)]
pub struct Options {
pub modify: bool,
pub selected: usize,
pub attack_ms: Option<u32>,
pub decay_ms: Option<u32>,
pub release_ms: Option<u32>,
Expand All @@ -30,6 +32,8 @@ pub struct Options {
impl Options {
pub fn new() -> Options {
Options {
modify: false,
selected: 0,
delay_len: Option {
name: "delayln".into(),
value: 511,
Expand Down Expand Up @@ -68,6 +72,28 @@ impl Options {
}
}

pub fn toggle_modify(&mut self) {
self.modify = !self.modify;
}

pub fn tick_up(&mut self) {
let selected = self.selected;
if self.modify {
self.view_mut()[selected].tick_up();
} else if selected < self.view().len()-1 {
self.selected = selected + 1;
}
}

pub fn tick_down(&mut self) {
let selected = self.selected;
if self.modify {
self.view_mut()[selected].tick_down();
} else if selected != 0 {
self.selected = selected - 1;
}
}

#[allow(dead_code)]
pub fn view(&self) -> [& dyn OptionTrait; 5] {
[
Expand Down

0 comments on commit 366cf1e

Please sign in to comment.