Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CUSTOM serial and MIDI creation MACRO's #286

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/platformio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
- RPN_NRPN
- SimpleSynth
- CustomBaudRate
- CustomMIDIsettings
- CustomMIDIandSerial
board:
- uno
- due
Expand Down
8 changes: 3 additions & 5 deletions examples/CustomBaudRate/CustomBaudRate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

// Override the default MIDI baudrate to
// a decoding program such as Hairless MIDI (set baudrate to 115200)
struct CustomBaudRateSettings : public MIDI_NAMESPACE::DefaultSerialSettings {
struct CustomSerialSettings : public MIDI_NAMESPACE::DefaultSerialSettings {
static const long BaudRate = 115200;
};

#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
// Leonardo, Due and other USB boards use Serial1 by default.
MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings> serialMIDI(Serial1);
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>> MIDI((MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>&)serialMIDI);
MIDI_CREATE_CUSTOMSERIAL_INSTANCE(HardwareSerial, Serial1, MIDI, CustomSerialSettings)
#else
MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings> serialMIDI(Serial);
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>> MIDI((MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>&)serialMIDI);
MIDI_CREATE_CUSTOMSERIAL_INSTANCE(HardwareSerial, Serial, MIDI, CustomSerialSettings)
#endif

void setup() {
Expand Down
36 changes: 36 additions & 0 deletions examples/CustomMIDIandSerial/CustomMIDIandSerial.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <MIDI.h>

// Disable the default Use1ByteParsing
struct CustomMIDISettings : public MIDI_NAMESPACE::DefaultSettings {
static const bool Use1ByteParsing = false;
};

// Override the default MIDI baudrate to
// a decoding program such as Hairless MIDI (set baudrate to 115200)
struct CustomSerialSettings : public MIDI_NAMESPACE::DefaultSerialSettings {
static const long BaudRate = 115200;
};


#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
// Leonardo, Due and other USB boards use Serial1 by default.
MIDI_CREATE_SPECIAL_INSTANCE(HardwareSerial, Serial1, MIDI, CustomMIDISettings, CustomSerialSettings)
#else
MIDI_CREATE_SPECIAL_INSTANCE(HardwareSerial, Serial, MIDI, CustomMIDISettings, CustomSerialSettings)
#endif

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
if (MIDI.read()) // If we have received a message
{
digitalWrite(LED_BUILTIN, HIGH);
MIDI.sendNoteOn(42, 127, 1); // Send a Note (pitch 42, velo 127 on channel 1)
delay(1000); // Wait for a second
MIDI.sendNoteOff(42, 0, 1); // Stop the note
digitalWrite(LED_BUILTIN, LOW);
}
}
29 changes: 29 additions & 0 deletions examples/CustomMIDIsettings/CustomMIDIsettings.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <MIDI.h>

// Disable the default Use1ByteParsing
struct CustomMIDISettings : public MIDI_NAMESPACE::DefaultSettings {
static const bool Use1ByteParsing = false;
};

#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
// Leonardo, Due and other USB boards use Serial1 by default.
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial1, MIDI, CustomMIDISettings)
#else
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, CustomMIDISettings)
#endif

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
if (MIDI.read()) // If we have received a message
{
digitalWrite(LED_BUILTIN, HIGH);
MIDI.sendNoteOn(42, 127, 1); // Send a Note (pitch 42, velo 127 on channel 1)
delay(1000); // Wait for a second
MIDI.sendNoteOff(42, 0, 1); // Stop the note
digitalWrite(LED_BUILTIN, LOW);
}
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ MIDI KEYWORD1
MIDI.h KEYWORD1
MidiInterface KEYWORD1
DefaultSettings KEYWORD1
DefaultSerialSettings KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down
18 changes: 16 additions & 2 deletions src/serialMIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ class SerialMIDI

END_MIDI_NAMESPACE

/*! \brief Create an instance of the library attached to a serial port
with overwritten MIDI & Serial Settings
*/
#define MIDI_CREATE_SPECIAL_INSTANCE(Type, SerialPort, Name, CustomMIDISettings, CustomSerialSettings) \
MIDI_NAMESPACE::SerialMIDI<Type, CustomSerialSettings> serial##Name(SerialPort); \
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type, CustomSerialSettings>, CustomMIDISettings> Name((MIDI_NAMESPACE::SerialMIDI<Type, CustomSerialSettings>&)serial##Name);

/*! \brief Create an instance of the library attached to a serial port
with overwritten Serial Settings
*/
#define MIDI_CREATE_CUSTOMSERIAL_INSTANCE(Type, SerialPort, Name, CustomSerialSettings) \
MIDI_NAMESPACE::SerialMIDI<Type, CustomSerialSettings> serial##Name(SerialPort); \
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type, CustomSerialSettings>> Name((MIDI_NAMESPACE::SerialMIDI<Type, CustomSerialSettings>&)serial##Name);

/*! \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);
Expand Down Expand Up @@ -125,6 +139,6 @@ END_MIDI_NAMESPACE
@see DefaultSettings
@see MIDI_CREATE_INSTANCE
*/
#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \
#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, CustomMIDISettings) \
MIDI_NAMESPACE::SerialMIDI<Type> serial##Name(SerialPort);\
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>, Settings> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>, CustomMIDISettings> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);