Skip to content

Commit a9e8e02

Browse files
committed
Adding an injectMessageByteCallback. This allows us to inject arbitrary data into the top of the MIDI stream.
1 parent 28d014e commit a9e8e02

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/MIDI.h

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class MidiInterface
178178
inline void setHandleStop(void (*fptr)(void));
179179
inline void setHandleActiveSensing(void (*fptr)(void));
180180
inline void setHandleSystemReset(void (*fptr)(void));
181+
inline void setHandleInjectMessageByte(byte (*fptr)(void));
181182

182183
inline void disconnectCallbackFromType(MidiType inType);
183184

@@ -202,6 +203,7 @@ class MidiInterface
202203
void (*mStopCallback)(void);
203204
void (*mActiveSensingCallback)(void);
204205
void (*mSystemResetCallback)(void);
206+
byte (*mInjectMessageByteCallback)(void);
205207

206208
// -------------------------------------------------------------------------
207209
// MIDI Soft Thru

src/MIDI.hpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ inline MidiInterface<SerialPort, Settings>::MidiInterface(SerialPort& inSerial)
6262
mStopCallback = 0;
6363
mActiveSensingCallback = 0;
6464
mSystemResetCallback = 0;
65+
mInjectMessageByteCallback = 0;
6566
}
6667

6768
/*! \brief Destructor for MidiInterface.
@@ -676,9 +677,20 @@ inline bool MidiInterface<SerialPort, Settings>::read(Channel inChannel)
676677
template<class SerialPort, class Settings>
677678
bool MidiInterface<SerialPort, Settings>::parse()
678679
{
679-
if (mSerial.available() == 0)
680-
// No data available.
680+
byte extracted = 255;
681+
682+
if (mInjectMessageByteCallback != 0) {
683+
extracted = mInjectMessageByteCallback();
684+
// extracted will remain 255 if no data is enqueued
685+
}
686+
687+
if (extracted == 255 && mSerial.available() != 0) {
688+
extracted = mSerial.read();
689+
690+
} else if (extracted == 255) {
691+
// No data available and no callback.
681692
return false;
693+
}
682694

683695
// Parsing algorithm:
684696
// Get a byte from the serial buffer.
@@ -689,8 +701,6 @@ bool MidiInterface<SerialPort, Settings>::parse()
689701
// Else, add the extracted byte to the pending message, and check validity.
690702
// When the message is done, store it.
691703

692-
const byte extracted = mSerial.read();
693-
694704
// Ignore Undefined
695705
if (extracted == 0xf9 || extracted == 0xfd)
696706
{
@@ -1165,6 +1175,7 @@ template<class SerialPort, class Settings> void MidiInterface<SerialPort, Settin
11651175
template<class SerialPort, class Settings> void MidiInterface<SerialPort, Settings>::setHandleStop(void (*fptr)(void)) { mStopCallback = fptr; }
11661176
template<class SerialPort, class Settings> void MidiInterface<SerialPort, Settings>::setHandleActiveSensing(void (*fptr)(void)) { mActiveSensingCallback = fptr; }
11671177
template<class SerialPort, class Settings> void MidiInterface<SerialPort, Settings>::setHandleSystemReset(void (*fptr)(void)) { mSystemResetCallback = fptr; }
1178+
template<class SerialPort, class Settings> void MidiInterface<SerialPort, Settings>::setHandleInjectMessageByte(byte (*fptr)(void)) { mInjectMessageByteCallback = fptr; }
11681179

11691180
/*! \brief Detach an external function from the given type.
11701181

0 commit comments

Comments
 (0)