diff --git a/examples/Callbacks/Callbacks.ino b/examples/Callbacks/Callbacks.ino index 642223b..90e1901 100644 --- a/examples/Callbacks/Callbacks.ino +++ b/examples/Callbacks/Callbacks.ino @@ -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); } diff --git a/src/MIDI.h b/src/MIDI.h index 5e91f26..56d44bf 100644 --- a/src/MIDI.h +++ b/src/MIDI.h @@ -50,6 +50,7 @@ class MidiInterface public: typedef _Settings Settings; typedef _Platform Platform; + typedef Message 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 MidiMessage; private: Transport& mTransport; diff --git a/src/MIDI.hpp b/src/MIDI.hpp index 7f86ec9..16bdbb0 100644 --- a/src/MIDI.hpp +++ b/src/MIDI.hpp @@ -45,6 +45,7 @@ inline MidiInterface::MidiInterface(Transport& in , mThruActivated(false) , mThruFilterMode(Thru::Full) { + mMessageCallback = 0; mNoteOffCallback = 0; mNoteOnCallback = 0; mAfterTouchPolyCallback = 0; @@ -706,10 +707,7 @@ template bool MidiInterface::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::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::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::isChannelMessage(MidiType inT @{ */ +template void MidiInterface::setHandleMessage(void (*fptr)(MidiMessage)) { mMessageCallback = fptr; } template void MidiInterface::setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity)) { mNoteOffCallback = fptr; } template void MidiInterface::setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity)) { mNoteOnCallback = fptr; } template void MidiInterface::setHandleAfterTouchPoly(void (*fptr)(byte channel, byte note, byte pressure)) { mAfterTouchPolyCallback = fptr; } @@ -1237,6 +1211,8 @@ void MidiInterface::disconnectCallbackFromType(Mi template void MidiInterface::launchCallback() { + if (mMessageCallback != 0) mMessageCallback(mMessage); + // The order is mixed to allow frequent messages to trigger their callback faster. switch (mMessage.type) { diff --git a/src/serialMIDI.h b/src/serialMIDI.h index 42dece5..801978f 100644 --- a/src/serialMIDI.h +++ b/src/serialMIDI.h @@ -52,9 +52,10 @@ private: Then call midi2.begin(), midi2.read() etc.. */ #define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \ - typedef MIDI_NAMESPACE::serialMIDI __amt;\ - __amt serialMidi(SerialPort);\ - MIDI_NAMESPACE::MidiInterface<__amt> Name((__amt&)serialMidi); + typedef MIDI_NAMESPACE::serialMIDI __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.