Skip to content

Commit

Permalink
feat: Export type definitions when using macros
Browse files Browse the repository at this point in the history
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:
#232 (comment)
  • Loading branch information
franky47 committed Oct 8, 2022
1 parent c2a838d commit f42c344
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 3 additions & 5 deletions examples/ThruFilterMap/ThruFilterMap.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <MIDI.h>

using Message = midi::Message<midi::DefaultSettings::SysExMaxSize>;

MIDI_CREATE_DEFAULT_INSTANCE();

/**
Expand All @@ -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)
{
Expand All @@ -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;
Expand Down
20 changes: 13 additions & 7 deletions src/serialMIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SerialMIDI

public:
static const bool thruActivated = true;

void begin()
{
// Initialise the Serial port
Expand Down Expand Up @@ -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<Type> serial##Name(SerialPort);\
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);
#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \
using Name##SerialTransport = MIDI_NAMESPACE::SerialMIDI<Type>; \
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__)
// Leonardo, Due and other USB boards use Serial1 by default.
Expand All @@ -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<Type> serial##Name(SerialPort);\
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>, Settings> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);
#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \
using Name##SerialTransport = MIDI_NAMESPACE::SerialMIDI<Type>; \
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);

0 comments on commit f42c344

Please sign in to comment.