From e916b2da81b8ab805d70a9a1f50a639d2809c3f6 Mon Sep 17 00:00:00 2001 From: heck Date: Mon, 23 Sep 2024 02:29:44 +0200 Subject: [PATCH] Feature: Midi - make it possible to handle 'SystemRealTime' events synchronously instead of polling --- src/hid/midi.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/hid/midi.h b/src/hid/midi.h index b212b3b3a..792e363b6 100644 --- a/src/hid/midi.h +++ b/src/hid/midi.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "per/uart.h" #include "util/ringbuffer.h" #include "util/FIFO.h" @@ -152,6 +153,8 @@ template class MidiHandler { public: + using MidiEventCallback = std::function; + MidiHandler() {} ~MidiHandler() {} @@ -177,6 +180,16 @@ class MidiHandler transport_.StartRx(MidiHandler::ParseCallback, this); } + /** Starts listening on the selected input mode(s). + * MidiEvent Queue will begin to fill, and can be checked with HasEvents() + * MIDI events of type SystemRealTime will be delivered synchronously by callback. + * All other events are on the queue */ + void StartReceiveRt(MidiEventCallback callback_for_rt_messages) + { + realtime_callback_ = callback_for_rt_messages; + transport_.StartRx(MidiHandler::ParseCallback, this); + } + /** Start listening */ void Listen() { @@ -222,7 +235,16 @@ class MidiHandler MidiEvent event; if(parser_.Parse(byte, &event)) { + if(event.type == MidiMessageType::SystemRealTime) + { + if(realtime_callback_) + { + realtime_callback_(event); + return; + } + } event_q_.PushBack(event); + return; } } @@ -231,6 +253,7 @@ class MidiHandler Transport transport_; MidiParser parser_; FIFO event_q_; + MidiEventCallback realtime_callback_; static void ParseCallback(uint8_t* data, size_t size, void* context) {