added callback for Message

before the specific callback are called, a generic mMessage callback can be fired to inform the user that 'a' message has been received.
This commit is contained in:
lathoub 2020-03-09 09:40:44 +01:00
parent 7bb64074e1
commit 8905d36c0e
4 changed files with 19 additions and 34 deletions

View File

@ -24,6 +24,11 @@ void handleNoteOff(byte channel, byte pitch, byte velocity)
// Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
}
void handleMessage(__mismt::MidiMessage message)
{
// Do something when the message.
}
// -----------------------------------------------------------------------------
void setup()
@ -35,6 +40,8 @@ void setup()
// Do the same for NoteOffs
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.setHandleMessage(handleMessage);
// Initiate MIDI communications, listen to all channels
MIDI.begin(MIDI_CHANNEL_OMNI);
}

View File

@ -50,6 +50,7 @@ class MidiInterface
public:
typedef _Settings Settings;
typedef _Platform Platform;
typedef Message<Settings::SysExMaxSize> MidiMessage;
public:
inline MidiInterface(Transport&);
@ -164,6 +165,7 @@ public:
// Input Callbacks
public:
inline void setHandleMessage(void (*fptr)(MidiMessage));
inline void setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity));
inline void setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity));
inline void setHandleAfterTouchPoly(void (*fptr)(byte channel, byte note, byte pressure));
@ -188,6 +190,7 @@ public:
private:
void launchCallback();
void (*mMessageCallback)(MidiMessage message);
void (*mNoteOffCallback)(byte channel, byte note, byte velocity);
void (*mNoteOnCallback)(byte channel, byte note, byte velocity);
void (*mAfterTouchPolyCallback)(byte channel, byte note, byte velocity);
@ -227,8 +230,6 @@ private:
inline bool inputFilter(Channel inChannel);
inline void resetInput();
private:
typedef Message<Settings::SysExMaxSize> MidiMessage;
private:
Transport& mTransport;

View File

@ -45,6 +45,7 @@ inline MidiInterface<Transport, Settings, Platform>::MidiInterface(Transport& in
, mThruActivated(false)
, mThruFilterMode(Thru::Full)
{
mMessageCallback = 0;
mNoteOffCallback = 0;
mNoteOnCallback = 0;
mAfterTouchPolyCallback = 0;
@ -706,10 +707,7 @@ template<class Transport, class Settings, class Platform>
bool MidiInterface<Transport, Settings, Platform>::parse()
{
if (mTransport.available() == 0)
{
// No data available.
return false;
}
return false; // No data available.
// Parsing algorithm:
// Get a byte from the serial buffer.
@ -724,19 +722,7 @@ bool MidiInterface<Transport, Settings, Platform>::parse()
// Ignore Undefined
if (extracted == 0xf9 || extracted == 0xfd)
{
return (Settings::Use1ByteParsing) ? false : parse();
/*
if (Settings::Use1ByteParsing)
{
return false;
}
else
{
return parse();
}
*/
}
if (mPendingMessageIndex == 0)
{
@ -978,20 +964,7 @@ bool MidiInterface<Transport, Settings, Platform>::parse()
// Then update the index of the pending message.
mPendingMessageIndex++;
return (Settings::Use1ByteParsing) ? false : parse();
/* if (Settings::Use1ByteParsing)
{
// Message is not complete.
return false;
}
else
{
// Call the parser recursively to parse the rest of the message.
return parse();
}
*/
}
}
}
@ -1178,6 +1151,7 @@ bool MidiInterface<Transport, Settings, Platform>::isChannelMessage(MidiType inT
@{
*/
template<class Transport, class Settings, class Platform> void MidiInterface<Transport, Settings, Platform>::setHandleMessage(void (*fptr)(MidiMessage)) { mMessageCallback = fptr; }
template<class Transport, class Settings, class Platform> void MidiInterface<Transport, Settings, Platform>::setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity)) { mNoteOffCallback = fptr; }
template<class Transport, class Settings, class Platform> void MidiInterface<Transport, Settings, Platform>::setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity)) { mNoteOnCallback = fptr; }
template<class Transport, class Settings, class Platform> void MidiInterface<Transport, Settings, Platform>::setHandleAfterTouchPoly(void (*fptr)(byte channel, byte note, byte pressure)) { mAfterTouchPolyCallback = fptr; }
@ -1237,6 +1211,8 @@ void MidiInterface<Transport, Settings, Platform>::disconnectCallbackFromType(Mi
template<class Transport, class Settings, class Platform>
void MidiInterface<Transport, Settings, Platform>::launchCallback()
{
if (mMessageCallback != 0) mMessageCallback(mMessage);
// The order is mixed to allow frequent messages to trigger their callback faster.
switch (mMessage.type)
{

View File

@ -52,9 +52,10 @@ private:
Then call midi2.begin(), midi2.read() etc..
*/
#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \
typedef MIDI_NAMESPACE::serialMIDI<Type> __amt;\
__amt serialMidi(SerialPort);\
MIDI_NAMESPACE::MidiInterface<__amt> Name((__amt&)serialMidi);
typedef MIDI_NAMESPACE::serialMIDI<Type> __smt;\
typedef MIDI_NAMESPACE::MidiInterface<__smt> __mismt;\
__smt serialMidi(SerialPort);\
__mismt Name((__smt&)serialMidi);
#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
// Leonardo, Due and other USB boards use Serial1 by default.