From 7bb96f978043809ab190f5a4b12c29bafb93de2e Mon Sep 17 00:00:00 2001 From: Francois Best Date: Thu, 2 Sep 2021 22:49:14 +0200 Subject: [PATCH] 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 --- examples/ThruFilterMap/ThruFilterMap.ino | 8 +++----- src/serialMIDI.h | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/examples/ThruFilterMap/ThruFilterMap.ino b/examples/ThruFilterMap/ThruFilterMap.ino index be04679..40c2f56 100644 --- a/examples/ThruFilterMap/ThruFilterMap.ino +++ b/examples/ThruFilterMap/ThruFilterMap.ino @@ -1,7 +1,5 @@ #include -using Message = midi::Message; - MIDI_CREATE_DEFAULT_INSTANCE(); /** @@ -17,7 +15,7 @@ MIDI_CREATE_DEFAULT_INSTANCE(); * 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) { @@ -27,10 +25,10 @@ bool filter(const Message& message) return false; } -Message map(const Message& message) +MIDIMessage map(const MIDIMessage& message) { // Make a copy of the message - Message output(message); + MIDIMessage output(message); if (message.type == midi::NoteOn) { output.type = midi::ProgramChange; diff --git a/src/serialMIDI.h b/src/serialMIDI.h index e69e9b2..f783439 100644 --- a/src/serialMIDI.h +++ b/src/serialMIDI.h @@ -52,7 +52,7 @@ public: public: static const bool thruActivated = true; - + void begin() { // Initialise the Serial port @@ -103,9 +103,12 @@ END_MIDI_NAMESPACE Example: MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midi2); Then call midi2.begin(), midi2.read() etc.. */ -#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \ - MIDI_NAMESPACE::SerialMIDI serial##Name(SerialPort);\ - MIDI_NAMESPACE::MidiInterface> Name((MIDI_NAMESPACE::SerialMIDI&)serial##Name); +#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \ + using Name##SerialTransport = MIDI_NAMESPACE::SerialMIDI; \ + using Name##Interface = MIDI_NAMESPACE::MidiInterface; \ + 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__) // Leonardo, Due and other USB boards use Serial1 by default. @@ -125,6 +128,9 @@ END_MIDI_NAMESPACE @see DefaultSettings @see MIDI_CREATE_INSTANCE */ -#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \ - MIDI_NAMESPACE::SerialMIDI serial##Name(SerialPort);\ - MIDI_NAMESPACE::MidiInterface, Settings> Name((MIDI_NAMESPACE::SerialMIDI&)serial##Name); +#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \ + using Name##SerialTransport = MIDI_NAMESPACE::SerialMIDI; \ + using Name##Interface = MIDI_NAMESPACE::MidiInterface; \ + using Name##Message = Name##Interface::MidiMessage; \ + Name##SerialTransport serial##Name(SerialPort); \ + Name##Interface Name((Name##SerialTransport&)serial##Name);