From 50d32da96e646ef9010b95f35026d2805cbd729d Mon Sep 17 00:00:00 2001 From: Francois Best Date: Wed, 9 Apr 2014 20:00:26 +0200 Subject: [PATCH] Updated examples to work on Leo, Uno, Mega & Due. --- res/Examples/MIDI_Bench/MIDI_Bench.ino | 66 ++++++++++--------- .../MIDI_Callbacks/MIDI_Callbacks.ino | 31 ++++----- .../MIDI_DualMerger/MIDI_DualMerger.ino | 16 +++-- 3 files changed, 61 insertions(+), 52 deletions(-) diff --git a/res/Examples/MIDI_Bench/MIDI_Bench.ino b/res/Examples/MIDI_Bench/MIDI_Bench.ino index 0b3061b..a97eaaf 100644 --- a/res/Examples/MIDI_Bench/MIDI_Bench.ino +++ b/res/Examples/MIDI_Bench/MIDI_Bench.ino @@ -1,17 +1,23 @@ -#include -#define MIDI_AUTO_INSTANCIATE 0 #include -// This program demonstrates how to use two serial ports at a time, -// the hardware serial being used for sending messages to the computer, -// while MIDI is handled on a software serial port. -// This program measures the time spent to receive and parse a message. +// This program will measure the time needed to receive, parse and process a +// NoteOn message. +// For it to work, please connect RX and TX on the MIDI port: +// Due, Leonardo and other USB-native Arduinos: Serial1 +// All other Arduinos: Connect pins 2 and 3. +// The program will then wait for 100 loops and print the results. + +#if defined(ARDUINO_SAM_DUE) || defined(USBCON) + // Print through USB and bench with Hardware serial + MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midiBench); +#else + #include + SoftwareSerial midiSerial(2,3); + MIDI_CREATE_INSTANCE(SoftwareSerial, midiSerial, midiBench); +#endif // ----------------------------------------------------------------------------- -SoftwareSerial midiSerial(2,3); -MIDI_CREATE_INSTANCE(SoftwareSerial, midiSerial, softMidi); - unsigned long gTime_start = 0; unsigned long gTime_stop = 0; unsigned gCounter = 0; @@ -24,39 +30,37 @@ unsigned long gTime_max = 0; void handleNoteOn(byte inChannel, byte inNote, byte inVelocity) { gTime_stop = micros(); - + const unsigned long diff = gTime_stop - gTime_start; gTime_sum += diff; - + if (diff > gTime_max) gTime_max = diff; if (diff < gTime_min) gTime_min = diff; - - gCounter++; - - if (gCounter >= 100) + + if (gCounter++ >= 1000) { const unsigned long average = gTime_sum / (float)gCounter; - + Serial.println("Time to receive NoteOn: "); - + Serial.print("Average: "); Serial.print(average); Serial.println(" microsecs"); - + Serial.print("Min: "); Serial.print(gTime_min); Serial.println(" microsecs"); - + Serial.print("Max: "); Serial.print(gTime_max); Serial.println(" microsecs"); - + gCounter = 0; gTime_sum = 0; gTime_max = 0; gTime_min = -1; - - softMidi.turnThruOff(); + + midiBench.turnThruOff(); } } @@ -64,18 +68,18 @@ void handleNoteOn(byte inChannel, byte inNote, byte inVelocity) void setup() { - softMidi.begin(); - - Serial.begin(38400); - Serial.print("Arduino Ready"); - - softMidi.sendNoteOn(69,127,1); -} + midiBench.setHandleNoteOn(handleNoteOn); + midiBench.begin(); + while(!Serial); + Serial.begin(115200); + Serial.println("Arduino Ready"); + + midiBench.sendNoteOn(69,127,1); +} void loop() { gTime_start = micros(); - softMidi.read(); + midiBench.read(); } - diff --git a/res/Examples/MIDI_Callbacks/MIDI_Callbacks.ino b/res/Examples/MIDI_Callbacks/MIDI_Callbacks.ino index 475c177..b8043e7 100644 --- a/res/Examples/MIDI_Callbacks/MIDI_Callbacks.ino +++ b/res/Examples/MIDI_Callbacks/MIDI_Callbacks.ino @@ -2,20 +2,22 @@ // This function will be automatically called when a NoteOn is received. // It must be a void-returning function with the correct parameters, -// see documentation here: -// http://arduinomidilib.sourceforge.net/class_m_i_d_i___class.html +// see documentation here: +// http://arduinomidilib.fortyseveneffects.com/a00022.html void HandleNoteOn(byte channel, byte pitch, byte velocity) { // Do whatever you want when you receive a Note On. - + if (velocity == 0) { - // This acts like a NoteOff. + // This acts like a NoteOff. You can ask the library to call the NoteOff + // callback when receiving null-velocity NoteOn messages. + // See MIDI_HANDLE_NULL_VELOCITY_NOTE_ON_AS_NOTE_OFF in midi_Settings.h } - - // Try to keep your callbacks short (no delays ect) - // otherwise it would slow down the loop() and have a bad impact + + // Try to keep your callbacks short (no delays ect) + // otherwise it would slow down the loop() and have a bad impact // on real-time performance. } @@ -23,22 +25,21 @@ void HandleNoteOn(byte channel, byte pitch, byte velocity) void setup() { - // Initiate MIDI communications, listen to all channels - MIDI.begin(MIDI_CHANNEL_OMNI); - - // Connect the HandleNoteOn function to the library, + // Connect the HandleNoteOn function to the library, // so it is called upon reception of a NoteOn. MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function -} + // Initiate MIDI communications, listen to all channels + MIDI.begin(MIDI_CHANNEL_OMNI); +} void loop() { // Call MIDI.read the fastest you can for real-time performance. MIDI.read(); - - // There is no need to check if there are messages incoming + + // There is no need to check if there are messages incoming // if they are bound to a Callback function. - // The attached method will be called automatically + // The attached method will be called automatically // when the corresponding message has been received. } diff --git a/res/Examples/MIDI_DualMerger/MIDI_DualMerger.ino b/res/Examples/MIDI_DualMerger/MIDI_DualMerger.ino index edff690..740fa5b 100644 --- a/res/Examples/MIDI_DualMerger/MIDI_DualMerger.ino +++ b/res/Examples/MIDI_DualMerger/MIDI_DualMerger.ino @@ -1,4 +1,3 @@ -#include #include // This example shows how to create two instances of the library to create a merger. @@ -7,10 +6,15 @@ // A out = A in + B in // B out = B in + A in -SoftwareSerial softSerial(2,3); - -MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiA); -MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, midiB); +#ifdef ARDUINO_SAM_DUE + MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiA); + MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midiB); +#else + #include + SoftwareSerial softSerial(2,3); + MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiA); + MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, midiB); +#endif void setup() { @@ -30,7 +34,7 @@ void loop() midiA.getData2(), midiA.getChannel()); } - + if (midiB.read()) { // Thru on B has already pushed the input message to out B.