Skip to content

Commit

Permalink
system: enable timers for sending midi clock via din
Browse files Browse the repository at this point in the history
  • Loading branch information
paradajz committed Oct 16, 2023
1 parent 5e2bdc2 commit 5686fe9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/firmware/application/protocol/midi/MIDI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1074,6 +1103,13 @@ std::optional<uint8_t> 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;
Expand Down
2 changes: 2 additions & 0 deletions src/firmware/application/protocol/midi/MIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,7 @@ namespace protocol
MIDIlib::BLEMIDI _bleMIDI = MIDIlib::BLEMIDI(_hwaBLE);
Database& _database;
std::array<MIDIlib::Base*, INTERFACE_AMOUNT> _midiInterface;
bool _clockTimerAllocated = false;
size_t _clockTimerIndex = 0;
};
} // namespace protocol

0 comments on commit 5686fe9

Please sign in to comment.