Skip to content

Commit 8521f44

Browse files
Feature: Midi - make it possible to handle 'SystemRealTime' events synchronously instead of polling (#643)
1 parent edc23bb commit 8521f44

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/hid/midi.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdint.h>
66
#include <stdlib.h>
77
#include <algorithm>
8+
#include <functional>
89
#include "per/uart.h"
910
#include "util/ringbuffer.h"
1011
#include "util/FIFO.h"
@@ -152,6 +153,8 @@ template <typename Transport>
152153
class MidiHandler
153154
{
154155
public:
156+
using MidiEventCallback = std::function<void(MidiEvent&)>;
157+
155158
MidiHandler() {}
156159
~MidiHandler() {}
157160

@@ -177,6 +180,16 @@ class MidiHandler
177180
transport_.StartRx(MidiHandler::ParseCallback, this);
178181
}
179182

183+
/** Starts listening on the selected input mode(s).
184+
* MidiEvent Queue will begin to fill, and can be checked with HasEvents()
185+
* MIDI events of type SystemRealTime will be delivered synchronously by callback.
186+
* All other events are on the queue */
187+
void StartReceiveRt(MidiEventCallback callback_for_rt_messages)
188+
{
189+
realtime_callback_ = callback_for_rt_messages;
190+
transport_.StartRx(MidiHandler::ParseCallback, this);
191+
}
192+
180193
/** Start listening */
181194
void Listen()
182195
{
@@ -222,7 +235,16 @@ class MidiHandler
222235
MidiEvent event;
223236
if(parser_.Parse(byte, &event))
224237
{
238+
if(event.type == MidiMessageType::SystemRealTime)
239+
{
240+
if(realtime_callback_)
241+
{
242+
realtime_callback_(event);
243+
return;
244+
}
245+
}
225246
event_q_.PushBack(event);
247+
return;
226248
}
227249
}
228250

@@ -231,6 +253,7 @@ class MidiHandler
231253
Transport transport_;
232254
MidiParser parser_;
233255
FIFO<MidiEvent, 256> event_q_;
256+
MidiEventCallback realtime_callback_;
234257

235258
static void ParseCallback(uint8_t* data, size_t size, void* context)
236259
{

0 commit comments

Comments
 (0)