Updated examples to work on Leo, Uno, Mega & Due.

This commit is contained in:
Francois Best 2014-04-09 20:00:26 +02:00
parent b4b39a9e5f
commit 50d32da96e
3 changed files with 61 additions and 52 deletions

View File

@ -1,17 +1,23 @@
#include <SoftwareSerial.h>
#define MIDI_AUTO_INSTANCIATE 0
#include <MIDI.h> #include <MIDI.h>
// This program demonstrates how to use two serial ports at a time, // This program will measure the time needed to receive, parse and process a
// the hardware serial being used for sending messages to the computer, // NoteOn message.
// while MIDI is handled on a software serial port. // For it to work, please connect RX and TX on the MIDI port:
// This program measures the time spent to receive and parse a message. // 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.h>
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_start = 0;
unsigned long gTime_stop = 0; unsigned long gTime_stop = 0;
unsigned gCounter = 0; unsigned gCounter = 0;
@ -24,39 +30,37 @@ unsigned long gTime_max = 0;
void handleNoteOn(byte inChannel, byte inNote, byte inVelocity) void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
{ {
gTime_stop = micros(); gTime_stop = micros();
const unsigned long diff = gTime_stop - gTime_start; const unsigned long diff = gTime_stop - gTime_start;
gTime_sum += diff; gTime_sum += diff;
if (diff > gTime_max) gTime_max = diff; if (diff > gTime_max) gTime_max = diff;
if (diff < gTime_min) gTime_min = diff; if (diff < gTime_min) gTime_min = diff;
gCounter++; if (gCounter++ >= 1000)
if (gCounter >= 100)
{ {
const unsigned long average = gTime_sum / (float)gCounter; const unsigned long average = gTime_sum / (float)gCounter;
Serial.println("Time to receive NoteOn: "); Serial.println("Time to receive NoteOn: ");
Serial.print("Average: "); Serial.print("Average: ");
Serial.print(average); Serial.print(average);
Serial.println(" microsecs"); Serial.println(" microsecs");
Serial.print("Min: "); Serial.print("Min: ");
Serial.print(gTime_min); Serial.print(gTime_min);
Serial.println(" microsecs"); Serial.println(" microsecs");
Serial.print("Max: "); Serial.print("Max: ");
Serial.print(gTime_max); Serial.print(gTime_max);
Serial.println(" microsecs"); Serial.println(" microsecs");
gCounter = 0; gCounter = 0;
gTime_sum = 0; gTime_sum = 0;
gTime_max = 0; gTime_max = 0;
gTime_min = -1; gTime_min = -1;
softMidi.turnThruOff(); midiBench.turnThruOff();
} }
} }
@ -64,18 +68,18 @@ void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
void setup() void setup()
{ {
softMidi.begin(); midiBench.setHandleNoteOn(handleNoteOn);
midiBench.begin();
Serial.begin(38400);
Serial.print("Arduino Ready");
softMidi.sendNoteOn(69,127,1);
}
while(!Serial);
Serial.begin(115200);
Serial.println("Arduino Ready");
midiBench.sendNoteOn(69,127,1);
}
void loop() void loop()
{ {
gTime_start = micros(); gTime_start = micros();
softMidi.read(); midiBench.read();
} }

View File

@ -2,20 +2,22 @@
// This function will be automatically called when a NoteOn is received. // This function will be automatically called when a NoteOn is received.
// It must be a void-returning function with the correct parameters, // It must be a void-returning function with the correct parameters,
// see documentation here: // see documentation here:
// http://arduinomidilib.sourceforge.net/class_m_i_d_i___class.html // http://arduinomidilib.fortyseveneffects.com/a00022.html
void HandleNoteOn(byte channel, byte pitch, byte velocity) void HandleNoteOn(byte channel, byte pitch, byte velocity)
{ {
// Do whatever you want when you receive a Note On. // Do whatever you want when you receive a Note On.
if (velocity == 0) 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) // Try to keep your callbacks short (no delays ect)
// otherwise it would slow down the loop() and have a bad impact // otherwise it would slow down the loop() and have a bad impact
// on real-time performance. // on real-time performance.
} }
@ -23,22 +25,21 @@ void HandleNoteOn(byte channel, byte pitch, byte velocity)
void setup() void setup()
{ {
// Initiate MIDI communications, listen to all channels // Connect the HandleNoteOn function to the library,
MIDI.begin(MIDI_CHANNEL_OMNI);
// Connect the HandleNoteOn function to the library,
// so it is called upon reception of a NoteOn. // so it is called upon reception of a NoteOn.
MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function
}
// Initiate MIDI communications, listen to all channels
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop() void loop()
{ {
// Call MIDI.read the fastest you can for real-time performance. // Call MIDI.read the fastest you can for real-time performance.
MIDI.read(); 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. // 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. // when the corresponding message has been received.
} }

View File

@ -1,4 +1,3 @@
#include <SoftwareSerial.h>
#include <MIDI.h> #include <MIDI.h>
// This example shows how to create two instances of the library to create a merger. // 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 // A out = A in + B in
// B out = B in + A in // B out = B in + A in
SoftwareSerial softSerial(2,3); #ifdef ARDUINO_SAM_DUE
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiA);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiA); MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midiB);
MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, midiB); #else
#include <SoftwareSerial.h>
SoftwareSerial softSerial(2,3);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, midiA);
MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, midiB);
#endif
void setup() void setup()
{ {
@ -30,7 +34,7 @@ void loop()
midiA.getData2(), midiA.getData2(),
midiA.getChannel()); midiA.getChannel());
} }
if (midiB.read()) if (midiB.read())
{ {
// Thru on B has already pushed the input message to out B. // Thru on B has already pushed the input message to out B.