Skip to content

Commit 219340e

Browse files
author
Francois Best
committed
Fix SysEx size callback bug, using template settings instead of preprocessor macros.
1 parent 1ff874c commit 219340e

File tree

10 files changed

+228
-193
lines changed

10 files changed

+228
-193
lines changed

res/Examples/MIDI_Basic_IO/MIDI_Basic_IO.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include <MIDI.h>
22

33
// Simple tutorial on how to receive and send MIDI messages.
4-
// Here, when receiving any message on channel 4, the Arduino
4+
// Here, when receiving any message on channel 4, the Arduino
55
// will blink a led and play back a note for 1 second.
66

7+
MIDI_CREATE_DEFAULT_INSTANCE();
8+
79
#define LED 13 // LED pin on Arduino Uno
810

911
void setup()

res/Examples/MIDI_Callbacks/MIDI_Callbacks.ino

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
#include <MIDI.h>
22

3+
MIDI_CREATE_DEFAULT_INSTANCE();
4+
5+
// -----------------------------------------------------------------------------
6+
37
// This function will be automatically called when a NoteOn is received.
48
// It must be a void-returning function with the correct parameters,
59
// see documentation here:
610
// http://arduinomidilib.fortyseveneffects.com/a00022.html
711

8-
void HandleNoteOn(byte channel, byte pitch, byte velocity)
12+
void handleNoteOn(byte channel, byte pitch, byte velocity)
913
{
10-
// Do whatever you want when you receive a Note On.
11-
12-
if (velocity == 0)
13-
{
14-
// This acts like a NoteOff. You can ask the library to call the NoteOff
15-
// callback when receiving null-velocity NoteOn messages.
16-
// See MIDI_HANDLE_NULL_VELOCITY_NOTE_ON_AS_NOTE_OFF in midi_Settings.h
17-
}
14+
// Do whatever you want when a note is pressed.
1815

1916
// Try to keep your callbacks short (no delays ect)
2017
// otherwise it would slow down the loop() and have a bad impact
2118
// on real-time performance.
2219
}
2320

21+
void handleNoteOff(byte channel, byte pitch, byte velocity)
22+
{
23+
// Do something when the note is released.
24+
// Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
25+
}
26+
2427
// -----------------------------------------------------------------------------
2528

2629
void setup()
2730
{
28-
// Connect the HandleNoteOn function to the library,
31+
// Connect the handleNoteOn function to the library,
2932
// so it is called upon reception of a NoteOn.
30-
MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function
33+
MIDI.setHandleNoteOn(handleNoteOn); // Put only the name of the function
34+
35+
// Do the same for NoteOffs
36+
MIDI.setHandleNoteOff(handleNoteOff);
3137

3238
// Initiate MIDI communications, listen to all channels
3339
MIDI.begin(MIDI_CHANNEL_OMNI);

res/Examples/MIDI_Input/MIDI_Input.ino

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include <MIDI.h>
22

3+
MIDI_CREATE_DEFAULT_INSTANCE();
4+
5+
// -----------------------------------------------------------------------------
6+
37
// This example shows the old way of checking for input messages.
48
// It's simpler to use the callbacks now, check out the dedicated example.
59

@@ -8,7 +12,7 @@
812
// -----------------------------------------------------------------------------
913

1014
void BlinkLed(byte num) // Basic blink function
11-
{
15+
{
1216
for (byte i=0;i<num;i++)
1317
{
1418
digitalWrite(LED,HIGH);
@@ -29,12 +33,12 @@ void setup()
2933
void loop()
3034
{
3135
if (MIDI.read()) // Is there a MIDI message incoming ?
32-
{
36+
{
3337
switch(MIDI.getType()) // Get the type of the message we caught
34-
{
38+
{
3539
case midi::ProgramChange: // If it is a Program Change,
36-
BlinkLed(MIDI.getData1()); // blink the LED a number of times
37-
// correponding to the program number
40+
BlinkLed(MIDI.getData1()); // blink the LED a number of times
41+
// correponding to the program number
3842
// (0 to 127, it can last a while..)
3943
break;
4044
// See the online reference for other message types

res/Examples/MIDI_SimpleSynth/MIDI_SimpleSynth.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "noteList.h"
33
#include "pitches.h"
44

5+
MIDI_CREATE_DEFAULT_INSTANCE();
6+
57
#ifdef ARDUINO_SAM_DUE // Due has no tone function (yet), overriden to prevent build errors.
68
#define tone(...)
79
#define noTone(...)
@@ -50,7 +52,7 @@ void handleNotesChanged(bool isFirstNote = false)
5052
if (midiNotes.getLast(currentNote))
5153
{
5254
tone(sAudioOutPin, sNotePitches[currentNote]);
53-
55+
5456
if (isFirstNote)
5557
{
5658
handleGateChanged(true);

res/validator/validate.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import shutil
66
import subprocess
7+
import argparse
78
from pprint import pprint
89
from midi import *
910
from tester import *
@@ -138,28 +139,42 @@ def validate(self):
138139
# ------------------------------------------------------------------------------
139140

140141
def main():
141-
midiInterface = MidiInterface()
142-
tester = Tester(midiInterface)
143-
midiInterface.listenerCallback = tester.handleMidiInput
144-
145-
tester.checkThru([Midi.NoteOn, 64, 80])
146-
tester.checkThru([Midi.AfterTouchChannel, 1])
147-
tester.checkThru([2])
148-
tester.checkThru([3])
149-
tester.checkThru([Midi.NoteOn, 64, 0])
150-
tester.checkThru([65, 127])
151-
tester.checkThru([65, 0])
152-
tester.checkThru([66, 127])
153-
tester.checkThru([66, 0])
154-
155-
#lib = ArduinoMidiLibrary()
156-
#lib.install()
157-
#if lib.validate():
158-
# print('Validation passed')
159-
#else:
160-
# print('Validation failed')
161-
162142

143+
info = "Validator script for the Arduino MIDI Library."
144+
arg_parser = argparse.ArgumentParser(description = info)
145+
146+
arg_parser.add_argument('--compile', '-c',
147+
action="store_true",
148+
help="Test compilation of the example sketches")
149+
150+
arg_parser.add_argument('--runtime', '-r',
151+
action="store_true",
152+
help="Test runtime")
153+
154+
args = arg_parser.parse_args()
155+
156+
if args.compile:
157+
lib = ArduinoMidiLibrary()
158+
lib.install()
159+
if lib.validate():
160+
print('Compilation test passed')
161+
else:
162+
print('Compilation test failed')
163+
164+
if args.runtime:
165+
midiInterface = MidiInterface()
166+
tester = Tester(midiInterface)
167+
midiInterface.listenerCallback = tester.handleMidiInput
168+
169+
tester.checkThru([Midi.NoteOn, 64, 80])
170+
tester.checkThru([Midi.AfterTouchChannel, 1])
171+
tester.checkThru([2])
172+
tester.checkThru([3])
173+
tester.checkThru([Midi.NoteOn, 64, 0])
174+
tester.checkThru([65, 127])
175+
tester.checkThru([65, 0])
176+
tester.checkThru([66, 127])
177+
tester.checkThru([66, 0])
163178

164179
# ------------------------------------------------------------------------------
165180

src/MIDI.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
#pragma once
2525

26-
#include "midi_Settings.h"
2726
#include "midi_Defs.h"
27+
#include "midi_Settings.h"
28+
#include "midi_Message.h"
2829

2930
// -----------------------------------------------------------------------------
3031

@@ -131,7 +132,7 @@ class MidiInterface
131132
inline void setHandleProgramChange(void (*fptr)(byte channel, byte number));
132133
inline void setHandleAfterTouchChannel(void (*fptr)(byte channel, byte pressure));
133134
inline void setHandlePitchBend(void (*fptr)(byte channel, int bend));
134-
inline void setHandleSystemExclusive(void (*fptr)(byte * array, byte size));
135+
inline void setHandleSystemExclusive(void (*fptr)(byte * array, unsigned size));
135136
inline void setHandleTimeCodeQuarterFrame(void (*fptr)(byte data));
136137
inline void setHandleSongPosition(void (*fptr)(unsigned beats));
137138
inline void setHandleSongSelect(void (*fptr)(byte songnumber));
@@ -155,7 +156,7 @@ class MidiInterface
155156
void (*mProgramChangeCallback)(byte channel, byte);
156157
void (*mAfterTouchChannelCallback)(byte channel, byte);
157158
void (*mPitchBendCallback)(byte channel, int);
158-
void (*mSystemExclusiveCallback)(byte * array, byte size);
159+
void (*mSystemExclusiveCallback)(byte * array, unsigned size);
159160
void (*mTimeCodeQuarterFrameCallback)(byte data);
160161
void (*mSongPositionCallback)(unsigned beats);
161162
void (*mSongSelectCallback)(byte songnumber);
@@ -192,25 +193,22 @@ class MidiInterface
192193
bool mThruActivated : 1;
193194
MidiFilterMode mThruFilterMode : 7;
194195

196+
private:
197+
typedef Message<Settings::SysExMaxSize> MidiMessage;
198+
195199
private:
196200
StatusByte mRunningStatus_RX;
201+
StatusByte mRunningStatus_TX;
197202
Channel mInputChannel;
198203
byte mPendingMessage[3];
199204
unsigned mPendingMessageExpectedLenght;
200205
unsigned mPendingMessageIndex;
201-
Message mMessage;
206+
MidiMessage mMessage;
202207

203208
private:
204209
inline StatusByte getStatus(MidiType inType,
205210
Channel inChannel) const;
206211

207-
208-
209-
#if MIDI_USE_RUNNING_STATUS
210-
private:
211-
StatusByte mRunningStatus_TX;
212-
#endif
213-
214212
private:
215213
SerialPort& mSerial;
216214
};

0 commit comments

Comments
 (0)