diff --git a/src/firmware/application/protocol/midi/MIDI.cpp b/src/firmware/application/protocol/midi/MIDI.cpp index 62203bc2c..9fcb6fff4 100644 --- a/src/firmware/application/protocol/midi/MIDI.cpp +++ b/src/firmware/application/protocol/midi/MIDI.cpp @@ -17,6 +17,7 @@ limitations under the License. */ #include "MIDI.h" +#include "core/Timing.h" #include "application/system/Config.h" #include "application/util/conversion/Conversion.h" #include "application/messaging/Messaging.h" @@ -82,6 +83,16 @@ protocol::MIDI::MIDI(HWAUSB& hwaUSB, } break; + case messaging::systemMessage_t::MIDI_BPM_CHANGE: + { + if (isSettingEnabled(setting_t::DIN_ENABLED)) + { + core::mcu::timers::setPeriod(_clockTimerIndex, BPM.bpmToUsec(BPM.value())); + core::mcu::timers::start(_clockTimerIndex); + } + } + break; + default: break; } @@ -172,6 +183,24 @@ bool protocol::MIDI::setupDINMIDI() _dinMIDI.setNoteOffMode(isSettingEnabled(setting_t::STANDARD_NOTE_OFF) ? noteOffType_t::STANDARD_NOTE_OFF : noteOffType_t::NOTE_ON_ZERO_VEL); _hwaDIN.setLoopback(isDinLoopbackRequired()); + if (!_clockTimerAllocated) + { + if (core::mcu::timers::allocate(_clockTimerIndex, [this]() + { + _dinMIDI.sendRealTime(messageType_t::SYS_REAL_TIME_CLOCK); + })) + { + _clockTimerAllocated = true; + + core::mcu::timers::setPeriod(_clockTimerIndex, BPM.bpmToUsec(BPM.value())); + + if (isSettingEnabled(setting_t::SEND_MIDI_CLOCK_DIN)) + { + core::mcu::timers::start(_clockTimerIndex); + } + } + } + return true; } @@ -1074,6 +1103,13 @@ std::optional protocol::MIDI::sysConfigSet(sys::Config::Section::global } break; + case setting_t::SEND_MIDI_CLOCK_DIN: + { + value ? core::mcu::timers::start(_clockTimerIndex) : core::mcu::timers::stop(_clockTimerIndex); + result = sys::Config::status_t::ACK; + } + break; + default: { result = sys::Config::status_t::ACK; diff --git a/src/firmware/application/protocol/midi/MIDI.h b/src/firmware/application/protocol/midi/MIDI.h index ac1f8032b..eb476baa0 100644 --- a/src/firmware/application/protocol/midi/MIDI.h +++ b/src/firmware/application/protocol/midi/MIDI.h @@ -137,5 +137,7 @@ namespace protocol MIDIlib::BLEMIDI _bleMIDI = MIDIlib::BLEMIDI(_hwaBLE); Database& _database; std::array _midiInterface; + bool _clockTimerAllocated = false; + size_t _clockTimerIndex = 0; }; } // namespace protocol \ No newline at end of file