From f88012b38fb3179339ee2fc5a3a35b59145ed891 Mon Sep 17 00:00:00 2001 From: Francois Best Date: Sun, 20 Apr 2014 17:24:35 +0200 Subject: [PATCH] Removed auto-instanciation --- src/MIDI.cpp | 21 --------------- src/MIDI.h | 63 ++++++++++++++------------------------------- src/MIDI.hpp | 43 +++++++++++++++---------------- src/midi_Defs.h | 8 +++--- src/midi_Settings.h | 30 --------------------- 5 files changed, 45 insertions(+), 120 deletions(-) diff --git a/src/MIDI.cpp b/src/MIDI.cpp index b8f1c0c..fa356ce 100644 --- a/src/MIDI.cpp +++ b/src/MIDI.cpp @@ -25,27 +25,6 @@ // ----------------------------------------------------------------------------- -#if !(MIDI_BUILD_INPUT) && !(MIDI_BUILD_OUTPUT) -# error To use MIDI, you need to enable at least input or output. -#endif - -#if MIDI_BUILD_THRU && !(MIDI_BUILD_OUTPUT) -# error For thru to work, you need to enable output. -#endif -#if MIDI_BUILD_THRU && !(MIDI_BUILD_INPUT) -# error For thru to work, you need to enable input. -#endif - -// ----------------------------------------------------------------------------- - -#if MIDI_AUTO_INSTANCIATE && defined(ARDUINO) - MIDI_CREATE_INSTANCE(MIDI_DEFAULT_SERIAL_CLASS, - MIDI_DEFAULT_SERIAL_PORT, - MIDI); -#endif - -// ----------------------------------------------------------------------------- - BEGIN_MIDI_NAMESPACE /*! \brief Encode System Exclusive messages. diff --git a/src/MIDI.h b/src/MIDI.h index 6f21ec6..dc6baed 100644 --- a/src/MIDI.h +++ b/src/MIDI.h @@ -26,10 +26,6 @@ #include "midi_Settings.h" #include "midi_Defs.h" -#ifdef FSE_AVR -#include -#endif - // ----------------------------------------------------------------------------- BEGIN_MIDI_NAMESPACE @@ -44,8 +40,8 @@ template class MidiInterface { public: - MidiInterface(SerialPort& inSerial); - ~MidiInterface(); + inline MidiInterface(SerialPort& inSerial); + inline ~MidiInterface(); public: void begin(Channel inChannel = 1); @@ -53,8 +49,6 @@ public: // ------------------------------------------------------------------------- // MIDI Output -#if MIDI_BUILD_OUTPUT - public: inline void sendNoteOn(DataByte inNoteNumber, DataByte inVelocity, @@ -100,17 +94,9 @@ public: DataByte inData2, Channel inChannel); -private: - inline StatusByte getStatus(MidiType inType, - Channel inChannel) const; - -#endif // MIDI_BUILD_OUTPUT - // ------------------------------------------------------------------------- // MIDI Input -#if MIDI_BUILD_INPUT - public: inline bool read(); inline bool read(Channel inChannel); @@ -133,25 +119,10 @@ public: static inline Channel getChannelFromStatusByte(byte inStatus); static inline bool isChannelMessage(MidiType inType); -private: - bool parse(); - inline void handleNullVelocityNoteOnAsNoteOff(); - inline bool inputFilter(Channel inChannel); - inline void resetInput(); - -private: - StatusByte mRunningStatus_RX; - Channel mInputChannel; - byte mPendingMessage[3]; - unsigned mPendingMessageExpectedLenght; - unsigned mPendingMessageIndex; - Message mMessage; // ------------------------------------------------------------------------- // Input Callbacks -#if MIDI_USE_CALLBACKS - public: inline void setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity)); inline void setHandleNoteOn(void (*fptr)(byte channel, byte note, byte velocity)); @@ -196,14 +167,9 @@ private: void (*mActiveSensingCallback)(void); void (*mSystemResetCallback)(void); -#endif // MIDI_USE_CALLBACKS -#endif // MIDI_BUILD_INPUT - // ------------------------------------------------------------------------- // MIDI Soft Thru -#if MIDI_BUILD_THRU - public: inline MidiFilterMode getFilterMode() const; inline bool getThruState() const; @@ -216,11 +182,28 @@ public: private: void thruFilter(byte inChannel); +private: + bool parse(); + inline void handleNullVelocityNoteOnAsNoteOff(); + inline bool inputFilter(Channel inChannel); + inline void resetInput(); + private: bool mThruActivated : 1; MidiFilterMode mThruFilterMode : 7; -#endif // MIDI_BUILD_THRU +private: + StatusByte mRunningStatus_RX; + Channel mInputChannel; + byte mPendingMessage[3]; + unsigned mPendingMessageExpectedLenght; + unsigned mPendingMessageIndex; + Message mMessage; + +private: + inline StatusByte getStatus(MidiType inType, + Channel inChannel) const; + #if MIDI_USE_RUNNING_STATUS @@ -241,10 +224,4 @@ END_MIDI_NAMESPACE // ----------------------------------------------------------------------------- -#if MIDI_AUTO_INSTANCIATE && defined(ARDUINO) - extern MIDI_NAMESPACE::MidiInterface MIDI; -#endif - -// ----------------------------------------------------------------------------- - #include "MIDI.hpp" diff --git a/src/MIDI.hpp b/src/MIDI.hpp index b6df785..2ffd715 100644 --- a/src/MIDI.hpp +++ b/src/MIDI.hpp @@ -143,7 +143,7 @@ void MidiInterface::send(MidiType inType, // Then test if channel is valid if (inChannel >= MIDI_CHANNEL_OFF || inChannel == MIDI_CHANNEL_OMNI || - inType < NoteOff) + inType < 0x80) { if (Settings::UseRunningStatus) { @@ -178,12 +178,14 @@ void MidiInterface::send(MidiType inType, // Then send data mSerial.write(inData1); if (inType != ProgramChange && inType != AfterTouchChannel) + { mSerial.write(inData2); - - return; + } } else if (inType >= TuneRequest && inType <= SystemReset) + { sendRealTime(inType); // System Real-time and 1 byte. + } } // ----------------------------------------------------------------------------- @@ -212,7 +214,7 @@ void MidiInterface::sendNoteOn(DataByte inNoteNumber, Note: you can send NoteOn with zero velocity to make a NoteOff, this is based on the Running Status principle, to avoid sending status messages and thus - sending only NoteOn data. This method will always send a real NoteOff message. + sending only NoteOn data. sendNoteOff will always send a real NoteOff message. Take a look at the values, names and frequencies of notes here: http://www.phys.unsw.edu.au/jw/notes.html */ @@ -492,24 +494,22 @@ inline bool MidiInterface::read(Channel inChannel) if (inChannel >= MIDI_CHANNEL_OFF) return false; // MIDI Input disabled. - if (parse()) + if (!parse()) + return false; + + handleNullVelocityNoteOnAsNoteOff(); + const bool channelMatch = inputFilter(inChannel); + + if (MIDI_USE_CALLBACKS && channelMatch) { - handleNullVelocityNoteOnAsNoteOff(); - if (inputFilter(inChannel)) - { - -#if (MIDI_BUILD_OUTPUT && MIDI_BUILD_THRU) - thruFilter(inChannel); -#endif - -#if MIDI_USE_CALLBACKS - launchCallback(); -#endif - return true; - } + launchCallback(); } - return false; +#if MIDI_BUILD_THRU + thruFilter(inChannel); +#endif + + return channelMatch; } // ----------------------------------------------------------------------------- @@ -802,7 +802,6 @@ inline void MidiInterface::handleNullVelocityNoteOnAsNoteO { mMessage.type = NoteOff; } - #endif } // Private method: check if the received message is on the listened channel @@ -953,10 +952,10 @@ MidiType MidiInterface::getTypeFromStatusByte(byte inStatu if (inStatus < 0xf0) { // Channel message, remove channel nibble. - return (MidiType)(inStatus & 0xf0); + return MidiType(inStatus & 0xf0); } - return (MidiType)inStatus; + return MidiType(inStatus); } /*! \brief Returns channel in the range 1-16 diff --git a/src/midi_Defs.h b/src/midi_Defs.h index dc2b29a..84315be 100644 --- a/src/midi_Defs.h +++ b/src/midi_Defs.h @@ -24,6 +24,7 @@ #pragma once #include "midi_Namespace.h" +#include "midi_Settings.h" #include BEGIN_MIDI_NAMESPACE @@ -39,8 +40,7 @@ BEGIN_MIDI_NAMESPACE // ----------------------------------------------------------------------------- // Type definitions -typedef uint8_t byte; - +typedef uint8_t byte; typedef byte StatusByte; typedef byte DataByte; typedef byte Channel; @@ -163,7 +163,6 @@ enum MidiControlChangeNumber */ struct Message { - /*! The MIDI channel on which the message was recieved. \n Value goes from 1 to 16. */ @@ -196,13 +195,14 @@ struct Message validity means the message respects the MIDI norm. */ bool valid; - }; // ----------------------------------------------------------------------------- /*! \brief Create an instance of the library attached to a serial port. You can use HardwareSerial or SoftwareSerial for the serial port. + Example: MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midi2); + Then call midi2.begin(), midi2.read() etc.. */ #define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \ midi::MidiInterface Name((Type&)SerialPort); diff --git a/src/midi_Settings.h b/src/midi_Settings.h index f2ea338..85270e8 100644 --- a/src/midi_Settings.h +++ b/src/midi_Settings.h @@ -44,36 +44,6 @@ #define MIDI_USE_CALLBACKS 1 -// ----------------------------------------------------------------------------- - -// Create a MIDI object automatically on the port defined with MIDI_SERIAL_PORT. -#ifndef MIDI_AUTO_INSTANCIATE -# ifdef ARDUINO -# define MIDI_AUTO_INSTANCIATE 1 -# else -# define MIDI_AUTO_INSTANCIATE 0 ///< @see MIDI_CREATE_INSTANCE -# endif -#endif - -// ----------------------------------------------------------------------------- -// Default serial port configuration (if MIDI_AUTO_INSTANCIATE is set) - -// Set the default port to use for MIDI. -#if MIDI_AUTO_INSTANCIATE -# ifdef ARDUINO -# include "Arduino.h" -# ifdef USBCON -# define MIDI_DEFAULT_SERIAL_PORT Serial1 // For Leonardo -# else -# define MIDI_DEFAULT_SERIAL_PORT Serial // For other Arduinos -# endif -# define MIDI_DEFAULT_SERIAL_CLASS HardwareSerial -# include "HardwareSerial.h" -# else -# error Auto-instanciation disabled. Use MIDI_CREATE_INSTANCE macro. -# endif -#endif - // ----------------------------------------------------------------------------- // Misc. options