From 5dc2cc4eb55d2349414af2fb80298705f3f7aa6d Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Mon, 11 Sep 2023 11:18:48 +0200 Subject: [PATCH] korg-kaoss-dj: Send MIDI reset messages on attach/detach --- Cargo.toml | 2 +- src/devices/korg_kaoss_dj/output.rs | 9 ++++++++- src/midi/mod.rs | 6 ++++++ src/output/blinking_led_task.rs | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0994d9f..350783f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/devices/korg_kaoss_dj/output.rs b/src/devices/korg_kaoss_dj/output.rs index 9137b26..d173669 100644 --- a/src/devices/korg_kaoss_dj/output.rs +++ b/src/devices/korg_kaoss_dj/output.rs @@ -203,7 +203,9 @@ fn on_attach(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 @@ -222,10 +224,15 @@ fn on_attach(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(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)?; } diff --git a/src/midi/mod.rs b/src/midi/mod.rs index 1e473ba..afb5ace 100644 --- a/src/midi/mod.rs +++ b/src/midi/mod.rs @@ -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 { @@ -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; diff --git a/src/output/blinking_led_task.rs b/src/output/blinking_led_task.rs index 069b326..aaf2910 100644 --- a/src/output/blinking_led_task.rs +++ b/src/output/blinking_led_task.rs @@ -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);