From ce0949ad4fe1560ea702ff4b7b0487f31f293ace Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Fri, 18 Oct 2024 22:24:41 -0400 Subject: [PATCH] revert some changes - scan frequency slightly reduced in hopes of not missing ticks - pedal normally closed by default again --- src/bin/piano_firmware.rs | 2 +- src/matrix.rs | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/bin/piano_firmware.rs b/src/bin/piano_firmware.rs index 605d350..a36072b 100644 --- a/src/bin/piano_firmware.rs +++ b/src/bin/piano_firmware.rs @@ -489,7 +489,7 @@ async fn main(_spawner: Spawner) { .spawn(matrix::pedal( midi::Controller::SustainPedal, p.PIN_8.into(), - true, + matrix::NormalState::NC, )) .unwrap(); } diff --git a/src/matrix.rs b/src/matrix.rs index 8cfaf25..61916e7 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -7,16 +7,25 @@ use core::cmp::min; use embassy_rp::gpio; use embassy_time::{Duration, Instant, Ticker}; +pub enum NormalState { + /// Normal open + NO, + /// Normal closed + NC, +} + /// Task to handle pedals in MIDI /// /// `norm_open` represents a normally open switch #[embassy_executor::task] -pub async fn pedal(pedal: midi::Controller, pin: gpio::AnyPin, norm_open: bool) { +pub async fn pedal(pedal: midi::Controller, pin: gpio::AnyPin, norm_state: NormalState) { let mut inp = gpio::Input::new(pin, gpio::Pull::Up); let chan = midi::MidiChannel::new(0); loop { - let on_val = if norm_open { 64 } else { 0 }; - let off_val = if norm_open { 0 } else { 64 }; + let (off_val, on_val) = match norm_state { + NormalState::NO => (0, 64), + NormalState::NC => (64, 0), + }; inp.wait_for_low().await; chan.controller(pedal, on_val).await; defmt::debug!("{} set to {}", pedal, on_val); @@ -62,7 +71,7 @@ impl KeyMatrix { // scan frequency // this might(?) panic if the scan takes longer than the tick - let mut ticker = Ticker::every(Duration::from_micros(3600)); + let mut ticker = Ticker::every(Duration::from_micros(4000)); let chan = midi::MidiChannel::new(0); const MAX_NOTES: usize = 128; @@ -120,7 +129,12 @@ impl KeyMatrix { } else { (127 - min(dur, 240) / 4 - 60) as u8 }; - defmt::debug!("{} velocity {} from dur {}ms", note, velocity, dur); + defmt::debug!( + "{} velocity {} from dur {}ms", + note, + velocity, + dur + ); note_on[note as usize] = true; chan.note_on(note, velocity).await; }