Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Midi - make it possible to handle 'SystemRealTime' events synchronously #643

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/hid/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include "per/uart.h"
#include "util/ringbuffer.h"
#include "util/FIFO.h"
Expand Down Expand Up @@ -152,6 +153,8 @@ template <typename Transport>
class MidiHandler
{
public:
using MidiEventCallback = std::function<void(MidiEvent&)>;

MidiHandler() {}
~MidiHandler() {}

Expand All @@ -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()
{
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -231,6 +253,7 @@ class MidiHandler
Transport transport_;
MidiParser parser_;
FIFO<MidiEvent, 256> event_q_;
MidiEventCallback realtime_callback_;

static void ParseCallback(uint8_t* data, size_t size, void* context)
{
Expand Down