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;
@ -31,9 +37,7 @@ void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
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;
@ -56,7 +60,7 @@ void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
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); while(!Serial);
Serial.print("Arduino Ready"); Serial.begin(115200);
Serial.println("Arduino Ready");
softMidi.sendNoteOn(69,127,1); midiBench.sendNoteOn(69,127,1);
} }
void loop() void loop()
{ {
gTime_start = micros(); gTime_start = micros();
softMidi.read(); midiBench.read();
} }

View File

@ -3,7 +3,7 @@
// 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)
{ {
@ -11,7 +11,9 @@ void HandleNoteOn(byte channel, byte pitch, byte velocity)
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)
@ -23,14 +25,13 @@ void HandleNoteOn(byte channel, byte pitch, byte velocity)
void setup() 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. // 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()
{ {

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()
{ {