Removed auto-instanciation

This commit is contained in:
Francois Best 2014-04-20 17:24:35 +02:00
parent 410e51c91a
commit f88012b38f
5 changed files with 45 additions and 120 deletions

View File

@ -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 BEGIN_MIDI_NAMESPACE
/*! \brief Encode System Exclusive messages. /*! \brief Encode System Exclusive messages.

View File

@ -26,10 +26,6 @@
#include "midi_Settings.h" #include "midi_Settings.h"
#include "midi_Defs.h" #include "midi_Defs.h"
#ifdef FSE_AVR
#include <ak47.h>
#endif
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
BEGIN_MIDI_NAMESPACE BEGIN_MIDI_NAMESPACE
@ -44,8 +40,8 @@ template<class SerialPort, class Settings = DefaultSettings>
class MidiInterface class MidiInterface
{ {
public: public:
MidiInterface(SerialPort& inSerial); inline MidiInterface(SerialPort& inSerial);
~MidiInterface(); inline ~MidiInterface();
public: public:
void begin(Channel inChannel = 1); void begin(Channel inChannel = 1);
@ -53,8 +49,6 @@ public:
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// MIDI Output // MIDI Output
#if MIDI_BUILD_OUTPUT
public: public:
inline void sendNoteOn(DataByte inNoteNumber, inline void sendNoteOn(DataByte inNoteNumber,
DataByte inVelocity, DataByte inVelocity,
@ -100,17 +94,9 @@ public:
DataByte inData2, DataByte inData2,
Channel inChannel); Channel inChannel);
private:
inline StatusByte getStatus(MidiType inType,
Channel inChannel) const;
#endif // MIDI_BUILD_OUTPUT
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// MIDI Input // MIDI Input
#if MIDI_BUILD_INPUT
public: public:
inline bool read(); inline bool read();
inline bool read(Channel inChannel); inline bool read(Channel inChannel);
@ -133,25 +119,10 @@ public:
static inline Channel getChannelFromStatusByte(byte inStatus); static inline Channel getChannelFromStatusByte(byte inStatus);
static inline bool isChannelMessage(MidiType inType); 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 // Input Callbacks
#if MIDI_USE_CALLBACKS
public: public:
inline void setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity)); inline void setHandleNoteOff(void (*fptr)(byte channel, byte note, byte velocity));
inline void setHandleNoteOn(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 (*mActiveSensingCallback)(void);
void (*mSystemResetCallback)(void); void (*mSystemResetCallback)(void);
#endif // MIDI_USE_CALLBACKS
#endif // MIDI_BUILD_INPUT
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// MIDI Soft Thru // MIDI Soft Thru
#if MIDI_BUILD_THRU
public: public:
inline MidiFilterMode getFilterMode() const; inline MidiFilterMode getFilterMode() const;
inline bool getThruState() const; inline bool getThruState() const;
@ -216,11 +182,28 @@ public:
private: private:
void thruFilter(byte inChannel); void thruFilter(byte inChannel);
private:
bool parse();
inline void handleNullVelocityNoteOnAsNoteOff();
inline bool inputFilter(Channel inChannel);
inline void resetInput();
private: private:
bool mThruActivated : 1; bool mThruActivated : 1;
MidiFilterMode mThruFilterMode : 7; 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 #if MIDI_USE_RUNNING_STATUS
@ -241,10 +224,4 @@ END_MIDI_NAMESPACE
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if MIDI_AUTO_INSTANCIATE && defined(ARDUINO)
extern MIDI_NAMESPACE::MidiInterface<MIDI_DEFAULT_SERIAL_CLASS> MIDI;
#endif
// -----------------------------------------------------------------------------
#include "MIDI.hpp" #include "MIDI.hpp"

View File

@ -143,7 +143,7 @@ void MidiInterface<SerialPort, Settings>::send(MidiType inType,
// Then test if channel is valid // Then test if channel is valid
if (inChannel >= MIDI_CHANNEL_OFF || if (inChannel >= MIDI_CHANNEL_OFF ||
inChannel == MIDI_CHANNEL_OMNI || inChannel == MIDI_CHANNEL_OMNI ||
inType < NoteOff) inType < 0x80)
{ {
if (Settings::UseRunningStatus) if (Settings::UseRunningStatus)
{ {
@ -178,12 +178,14 @@ void MidiInterface<SerialPort, Settings>::send(MidiType inType,
// Then send data // Then send data
mSerial.write(inData1); mSerial.write(inData1);
if (inType != ProgramChange && inType != AfterTouchChannel) if (inType != ProgramChange && inType != AfterTouchChannel)
{
mSerial.write(inData2); mSerial.write(inData2);
}
return;
} }
else if (inType >= TuneRequest && inType <= SystemReset) else if (inType >= TuneRequest && inType <= SystemReset)
{
sendRealTime(inType); // System Real-time and 1 byte. sendRealTime(inType); // System Real-time and 1 byte.
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -212,7 +214,7 @@ void MidiInterface<SerialPort, Settings>::sendNoteOn(DataByte inNoteNumber,
Note: you can send NoteOn with zero velocity to make a NoteOff, this is based 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 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: Take a look at the values, names and frequencies of notes here:
http://www.phys.unsw.edu.au/jw/notes.html http://www.phys.unsw.edu.au/jw/notes.html
*/ */
@ -492,24 +494,22 @@ inline bool MidiInterface<SerialPort, Settings>::read(Channel inChannel)
if (inChannel >= MIDI_CHANNEL_OFF) if (inChannel >= MIDI_CHANNEL_OFF)
return false; // MIDI Input disabled. return false; // MIDI Input disabled.
if (parse()) if (!parse())
return false;
handleNullVelocityNoteOnAsNoteOff();
const bool channelMatch = inputFilter(inChannel);
if (MIDI_USE_CALLBACKS && channelMatch)
{ {
handleNullVelocityNoteOnAsNoteOff(); launchCallback();
if (inputFilter(inChannel))
{
#if (MIDI_BUILD_OUTPUT && MIDI_BUILD_THRU)
thruFilter(inChannel);
#endif
#if MIDI_USE_CALLBACKS
launchCallback();
#endif
return true;
}
} }
return false; #if MIDI_BUILD_THRU
thruFilter(inChannel);
#endif
return channelMatch;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -802,7 +802,6 @@ inline void MidiInterface<SerialPort, Settings>::handleNullVelocityNoteOnAsNoteO
{ {
mMessage.type = NoteOff; mMessage.type = NoteOff;
} }
#endif
} }
// Private method: check if the received message is on the listened channel // Private method: check if the received message is on the listened channel
@ -953,10 +952,10 @@ MidiType MidiInterface<SerialPort, Settings>::getTypeFromStatusByte(byte inStatu
if (inStatus < 0xf0) if (inStatus < 0xf0)
{ {
// Channel message, remove channel nibble. // 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 /*! \brief Returns channel in the range 1-16

View File

@ -24,6 +24,7 @@
#pragma once #pragma once
#include "midi_Namespace.h" #include "midi_Namespace.h"
#include "midi_Settings.h"
#include <inttypes.h> #include <inttypes.h>
BEGIN_MIDI_NAMESPACE BEGIN_MIDI_NAMESPACE
@ -39,8 +40,7 @@ BEGIN_MIDI_NAMESPACE
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Type definitions // Type definitions
typedef uint8_t byte; typedef uint8_t byte;
typedef byte StatusByte; typedef byte StatusByte;
typedef byte DataByte; typedef byte DataByte;
typedef byte Channel; typedef byte Channel;
@ -163,7 +163,6 @@ enum MidiControlChangeNumber
*/ */
struct Message struct Message
{ {
/*! The MIDI channel on which the message was recieved. /*! The MIDI channel on which the message was recieved.
\n Value goes from 1 to 16. \n Value goes from 1 to 16.
*/ */
@ -196,13 +195,14 @@ struct Message
validity means the message respects the MIDI norm. validity means the message respects the MIDI norm.
*/ */
bool valid; bool valid;
}; };
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
/*! \brief Create an instance of the library attached to a serial port. /*! \brief Create an instance of the library attached to a serial port.
You can use HardwareSerial or SoftwareSerial for the 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) \ #define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \
midi::MidiInterface<Type> Name((Type&)SerialPort); midi::MidiInterface<Type> Name((Type&)SerialPort);

View File

@ -44,36 +44,6 @@
#define MIDI_USE_CALLBACKS 1 #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 // Misc. options