feat: Export type definitions when using macros

Types have names prepended by the port name
(defaults to `MIDI`), to allow multi-port applications.

This allows referencing those types elsewhere in the
application, without re-writing the template arguments,
and simplifies referencing the underlying Message type,
for SoftThru `filter`/`map`function definitions.

Proposition & discussion:
https://github.com/FortySevenEffects/arduino_midi_library/pull/232#issuecomment-910355200
This commit is contained in:
Francois Best 2021-09-02 22:49:14 +02:00
parent 2fec41ecfe
commit 7bb96f9780
2 changed files with 16 additions and 12 deletions

View File

@ -1,7 +1,5 @@
#include <MIDI.h> #include <MIDI.h>
using Message = midi::Message<midi::DefaultSettings::SysExMaxSize>;
MIDI_CREATE_DEFAULT_INSTANCE(); MIDI_CREATE_DEFAULT_INSTANCE();
/** /**
@ -17,7 +15,7 @@ MIDI_CREATE_DEFAULT_INSTANCE();
* allowing to use a keyboard to change patches on a MIDI device. * allowing to use a keyboard to change patches on a MIDI device.
*/ */
bool filter(const Message& message) bool filter(const MIDIMessage& message)
{ {
if (message.type == midi::NoteOn) if (message.type == midi::NoteOn)
{ {
@ -27,10 +25,10 @@ bool filter(const Message& message)
return false; return false;
} }
Message map(const Message& message) MIDIMessage map(const MIDIMessage& message)
{ {
// Make a copy of the message // Make a copy of the message
Message output(message); MIDIMessage output(message);
if (message.type == midi::NoteOn) if (message.type == midi::NoteOn)
{ {
output.type = midi::ProgramChange; output.type = midi::ProgramChange;

View File

@ -52,7 +52,7 @@ public:
public: public:
static const bool thruActivated = true; static const bool thruActivated = true;
void begin() void begin()
{ {
// Initialise the Serial port // Initialise the Serial port
@ -103,9 +103,12 @@ END_MIDI_NAMESPACE
Example: MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midi2); Example: MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midi2);
Then call midi2.begin(), midi2.read() etc.. Then call midi2.begin(), midi2.read() etc..
*/ */
#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \ #define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \
MIDI_NAMESPACE::SerialMIDI<Type> serial##Name(SerialPort);\ using Name##SerialTransport = MIDI_NAMESPACE::SerialMIDI<Type>; \
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name); using Name##Interface = MIDI_NAMESPACE::MidiInterface<Name##SerialTransport>; \
using Name##Message = Name##Interface::MidiMessage; \
Name##SerialTransport serial##Name(SerialPort); \
Name##Interface Name((Name##SerialTransport&)serial##Name);
#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) #if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
// Leonardo, Due and other USB boards use Serial1 by default. // Leonardo, Due and other USB boards use Serial1 by default.
@ -125,6 +128,9 @@ END_MIDI_NAMESPACE
@see DefaultSettings @see DefaultSettings
@see MIDI_CREATE_INSTANCE @see MIDI_CREATE_INSTANCE
*/ */
#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \ #define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \
MIDI_NAMESPACE::SerialMIDI<Type> serial##Name(SerialPort);\ using Name##SerialTransport = MIDI_NAMESPACE::SerialMIDI<Type>; \
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>, Settings> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name); using Name##Interface = MIDI_NAMESPACE::MidiInterface<Name##SerialTransport, Settings>; \
using Name##Message = Name##Interface::MidiMessage; \
Name##SerialTransport serial##Name(SerialPort); \
Name##Interface Name((Name##SerialTransport&)serial##Name);