Skip to content

Commit

Permalink
korg-kaoss-dj: Send MIDI reset messages on attach/detach
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Sep 11, 2023
1 parent 3a6cd56 commit 5dc2cc4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ futures = "0.3.28"
is_sorted = "0.1.1"
log = "0.4.20"
strum = { version = "0.25.0", features = ["derive"] }
thiserror = "1.0.47"
thiserror = "1.0.48"

# Optional dependencies
discro = { version = "0.15.0", optional = true }
Expand Down
9 changes: 8 additions & 1 deletion src/devices/korg_kaoss_dj/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ fn on_attach<C: MidiOutputConnection>(midi_output_connection: &mut C) -> OutputR
const MIDI_STATUS_SYSEX: &[u8] = &[
0xf0, 0x42, 0x40, 0x00, 0x01, 0x28, 0x00, 0x1f, 0x70, 0x01, 0xf7,
];
midi_output_connection.send_midi_output(MIDI_STATUS_SYSEX)?;
// First send a MIDI system reset message
midi_output_connection.send_midi_system_reset()?;
// Turn on all knob LEDs
for led in MainLed::iter() {
let output = if led.is_knob() {
LedOutput::On
Expand All @@ -222,10 +224,15 @@ fn on_attach<C: MidiOutputConnection>(midi_output_connection: &mut C) -> OutputR
send_led_output(midi_output_connection, Led::Deck(deck, led), output)?;
}
}
// Finally query the initial position of all knobs and faders
midi_output_connection.send_midi_output(MIDI_STATUS_SYSEX)?;
Ok(())
}

fn on_detach<C: MidiOutputConnection>(midi_output_connection: &mut C) -> OutputResult<()> {
// First send a MIDI system reset message
midi_output_connection.send_midi_system_reset()?;
// Turn off all LEDs
for led in MainLed::iter() {
send_led_output(midi_output_connection, led.into(), LedOutput::Off)?;
}
Expand Down
6 changes: 6 additions & 0 deletions src/midi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::{
#[cfg(feature = "midir")]
pub(crate) mod midir;

const MIDI_OUTPUT_SYSTEM_RESET: &[u8] = &[0xff];

/// MIDI-related, extended [`DeviceDescriptor`]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MidiDeviceDescriptor {
Expand Down Expand Up @@ -124,6 +126,10 @@ where

pub trait MidiOutputConnection {
fn send_midi_output(&mut self, output: &[u8]) -> OutputResult<()>;

fn send_midi_system_reset(&mut self) -> OutputResult<()> {
self.send_midi_output(MIDI_OUTPUT_SYSTEM_RESET)
}
}

pub type BoxedMidiOutputConnection = Box<dyn MidiOutputConnection + Send + 'static>;
Expand Down
1 change: 1 addition & 0 deletions src/output/blinking_led_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn blinking_led_task(
let mut ticker = BlinkingLedTicker::default();
let mut interval = tokio::time::interval(period);
loop {
// The first tick arrives immediately
interval.tick().await;
let output = ticker.tick();
publisher.write(output);
Expand Down

0 comments on commit 5dc2cc4

Please sign in to comment.