Skip to content

Commit 50e33c8

Browse files
authored
Merge pull request #247 from FortySevenEffects/chaining
chaining for all commands, callbacks and setters
2 parents c792292 + 417beca commit 50e33c8

File tree

4 files changed

+205
-121
lines changed

4 files changed

+205
-121
lines changed

.github/workflows/platformio.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- Basic_IO
1818
- Bench
1919
- Callbacks
20+
- Chaining
2021
- DualMerger
2122
- ErrorCallback
2223
- Input

examples/Chaining/Chaining.ino

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <MIDI.h>
2+
3+
MIDI_CREATE_DEFAULT_INSTANCE();
4+
5+
void setup()
6+
{
7+
pinMode(2, INPUT);
8+
9+
MIDI // chaining MIDI commands - order is from top to bottom (turnThruOff,... begin)
10+
.turnThruOff()
11+
// using a lamdba function for this callbacks
12+
.setHandleNoteOn([](byte channel, byte note, byte velocity)
13+
{
14+
// Do whatever you want when a note is pressed.
15+
16+
// Try to keep your callbacks short (no delays ect)
17+
// otherwise it would slow down the loop() and have a bad impact
18+
// on real-time performance.
19+
})
20+
.setHandleNoteOff([](byte channel, byte note, byte velocity)
21+
{
22+
// Do something when the note is released.
23+
// Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
24+
})
25+
.begin(MIDI_CHANNEL_OMNI); // Initiate MIDI communications, listen to all channels
26+
}
27+
28+
void loop()
29+
{
30+
// Call MIDI.read the fastest you can for real-time performance.
31+
MIDI.read();
32+
33+
if (digitalRead(2))
34+
MIDI // chained sendNoteOn commands
35+
.sendNoteOn(42, 127, 1)
36+
.sendNoteOn(40, 54, 1);
37+
}

src/MIDI.h

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -62,94 +62,94 @@ class MidiInterface
6262
inline ~MidiInterface();
6363

6464
public:
65-
void begin(Channel inChannel = 1);
65+
MidiInterface& begin(Channel inChannel = 1);
6666

6767
// -------------------------------------------------------------------------
6868
// MIDI Output
6969

7070
public:
71-
inline void sendNoteOn(DataByte inNoteNumber,
71+
inline MidiInterface& sendNoteOn(DataByte inNoteNumber,
7272
DataByte inVelocity,
7373
Channel inChannel);
7474

75-
inline void sendNoteOff(DataByte inNoteNumber,
75+
inline MidiInterface& sendNoteOff(DataByte inNoteNumber,
7676
DataByte inVelocity,
7777
Channel inChannel);
7878

79-
inline void sendProgramChange(DataByte inProgramNumber,
79+
inline MidiInterface& sendProgramChange(DataByte inProgramNumber,
8080
Channel inChannel);
8181

82-
inline void sendControlChange(DataByte inControlNumber,
82+
inline MidiInterface& sendControlChange(DataByte inControlNumber,
8383
DataByte inControlValue,
8484
Channel inChannel);
8585

86-
inline void sendPitchBend(int inPitchValue, Channel inChannel);
87-
inline void sendPitchBend(double inPitchValue, Channel inChannel);
86+
inline MidiInterface& sendPitchBend(int inPitchValue, Channel inChannel);
87+
inline MidiInterface& sendPitchBend(double inPitchValue, Channel inChannel);
8888

89-
inline void sendPolyPressure(DataByte inNoteNumber,
89+
inline MidiInterface& sendPolyPressure(DataByte inNoteNumber,
9090
DataByte inPressure,
9191
Channel inChannel) __attribute__ ((deprecated));
9292

93-
inline void sendAfterTouch(DataByte inPressure,
93+
inline MidiInterface& sendAfterTouch(DataByte inPressure,
9494
Channel inChannel);
95-
inline void sendAfterTouch(DataByte inNoteNumber,
95+
inline MidiInterface& sendAfterTouch(DataByte inNoteNumber,
9696
DataByte inPressure,
9797
Channel inChannel);
9898

99-
inline void sendSysEx(unsigned inLength,
99+
inline MidiInterface& sendSysEx(unsigned inLength,
100100
const byte* inArray,
101101
bool inArrayContainsBoundaries = false);
102102

103-
inline void sendTimeCodeQuarterFrame(DataByte inTypeNibble,
103+
inline MidiInterface& sendTimeCodeQuarterFrame(DataByte inTypeNibble,
104104
DataByte inValuesNibble);
105-
inline void sendTimeCodeQuarterFrame(DataByte inData);
105+
inline MidiInterface& sendTimeCodeQuarterFrame(DataByte inData);
106106

107-
inline void sendSongPosition(unsigned inBeats);
108-
inline void sendSongSelect(DataByte inSongNumber);
109-
inline void sendTuneRequest();
107+
inline MidiInterface& sendSongPosition(unsigned inBeats);
108+
inline MidiInterface& sendSongSelect(DataByte inSongNumber);
109+
inline MidiInterface& sendTuneRequest();
110110

111-
inline void sendCommon(MidiType inType, unsigned = 0);
111+
inline MidiInterface& sendCommon(MidiType inType, unsigned = 0);
112112

113-
inline void sendClock() { sendRealTime(Clock); };
114-
inline void sendStart() { sendRealTime(Start); };
115-
inline void sendStop() { sendRealTime(Stop); };
116-
inline void sendTick() { sendRealTime(Tick); };
117-
inline void sendContinue() { sendRealTime(Continue); };
118-
inline void sendActiveSensing() { sendRealTime(ActiveSensing); };
119-
inline void sendSystemReset() { sendRealTime(SystemReset); };
113+
inline MidiInterface& sendClock() { sendRealTime(Clock); };
114+
inline MidiInterface& sendStart() { sendRealTime(Start); };
115+
inline MidiInterface& sendStop() { sendRealTime(Stop); };
116+
inline MidiInterface& sendTick() { sendRealTime(Tick); };
117+
inline MidiInterface& sendContinue() { sendRealTime(Continue); };
118+
inline MidiInterface& sendActiveSensing() { sendRealTime(ActiveSensing); };
119+
inline MidiInterface& sendSystemReset() { sendRealTime(SystemReset); };
120120

121-
inline void sendRealTime(MidiType inType);
121+
inline MidiInterface& sendRealTime(MidiType inType);
122122

123-
inline void beginRpn(unsigned inNumber,
123+
inline MidiInterface& beginRpn(unsigned inNumber,
124124
Channel inChannel);
125-
inline void sendRpnValue(unsigned inValue,
125+
inline MidiInterface& sendRpnValue(unsigned inValue,
126126
Channel inChannel);
127-
inline void sendRpnValue(byte inMsb,
127+
inline MidiInterface& sendRpnValue(byte inMsb,
128128
byte inLsb,
129129
Channel inChannel);
130-
inline void sendRpnIncrement(byte inAmount,
130+
inline MidiInterface& sendRpnIncrement(byte inAmount,
131131
Channel inChannel);
132-
inline void sendRpnDecrement(byte inAmount,
132+
inline MidiInterface& sendRpnDecrement(byte inAmount,
133133
Channel inChannel);
134-
inline void endRpn(Channel inChannel);
134+
inline MidiInterface& endRpn(Channel inChannel);
135135

136-
inline void beginNrpn(unsigned inNumber,
136+
inline MidiInterface& beginNrpn(unsigned inNumber,
137137
Channel inChannel);
138-
inline void sendNrpnValue(unsigned inValue,
138+
inline MidiInterface& sendNrpnValue(unsigned inValue,
139139
Channel inChannel);
140-
inline void sendNrpnValue(byte inMsb,
140+
inline MidiInterface& sendNrpnValue(byte inMsb,
141141
byte inLsb,
142142
Channel inChannel);
143-
inline void sendNrpnIncrement(byte inAmount,
143+
inline MidiInterface& sendNrpnIncrement(byte inAmount,
144144
Channel inChannel);
145-
inline void sendNrpnDecrement(byte inAmount,
145+
inline MidiInterface& sendNrpnDecrement(byte inAmount,
146146
Channel inChannel);
147-
inline void endNrpn(Channel inChannel);
147+
inline MidiInterface& endNrpn(Channel inChannel);
148148

149-
inline void send(const MidiMessage&);
149+
inline MidiInterface& send(const MidiMessage&);
150150

151151
public:
152-
void send(MidiType inType,
152+
MidiInterface& send(MidiType inType,
153153
DataByte inData1,
154154
DataByte inData2,
155155
Channel inChannel);
@@ -172,7 +172,7 @@ class MidiInterface
172172

173173
public:
174174
inline Channel getInputChannel() const;
175-
inline void setInputChannel(Channel inChannel);
175+
inline MidiInterface& setInputChannel(Channel inChannel);
176176

177177
public:
178178
static inline MidiType getTypeFromStatusByte(byte inStatus);
@@ -183,29 +183,29 @@ class MidiInterface
183183
// Input Callbacks
184184

185185
public:
186-
inline void setHandleMessage(void (*fptr)(const MidiMessage&)) { mMessageCallback = fptr; };
187-
inline void setHandleError(ErrorCallback fptr) { mErrorCallback = fptr; }
188-
inline void setHandleNoteOff(NoteOffCallback fptr) { mNoteOffCallback = fptr; }
189-
inline void setHandleNoteOn(NoteOnCallback fptr) { mNoteOnCallback = fptr; }
190-
inline void setHandleAfterTouchPoly(AfterTouchPolyCallback fptr) { mAfterTouchPolyCallback = fptr; }
191-
inline void setHandleControlChange(ControlChangeCallback fptr) { mControlChangeCallback = fptr; }
192-
inline void setHandleProgramChange(ProgramChangeCallback fptr) { mProgramChangeCallback = fptr; }
193-
inline void setHandleAfterTouchChannel(AfterTouchChannelCallback fptr) { mAfterTouchChannelCallback = fptr; }
194-
inline void setHandlePitchBend(PitchBendCallback fptr) { mPitchBendCallback = fptr; }
195-
inline void setHandleSystemExclusive(SystemExclusiveCallback fptr) { mSystemExclusiveCallback = fptr; }
196-
inline void setHandleTimeCodeQuarterFrame(TimeCodeQuarterFrameCallback fptr) { mTimeCodeQuarterFrameCallback = fptr; }
197-
inline void setHandleSongPosition(SongPositionCallback fptr) { mSongPositionCallback = fptr; }
198-
inline void setHandleSongSelect(SongSelectCallback fptr) { mSongSelectCallback = fptr; }
199-
inline void setHandleTuneRequest(TuneRequestCallback fptr) { mTuneRequestCallback = fptr; }
200-
inline void setHandleClock(ClockCallback fptr) { mClockCallback = fptr; }
201-
inline void setHandleStart(StartCallback fptr) { mStartCallback = fptr; }
202-
inline void setHandleTick(TickCallback fptr) { mTickCallback = fptr; }
203-
inline void setHandleContinue(ContinueCallback fptr) { mContinueCallback = fptr; }
204-
inline void setHandleStop(StopCallback fptr) { mStopCallback = fptr; }
205-
inline void setHandleActiveSensing(ActiveSensingCallback fptr) { mActiveSensingCallback = fptr; }
206-
inline void setHandleSystemReset(SystemResetCallback fptr) { mSystemResetCallback = fptr; }
207-
208-
inline void disconnectCallbackFromType(MidiType inType);
186+
inline MidiInterface& setHandleMessage(void (*fptr)(const MidiMessage&)) { mMessageCallback = fptr; return *this; };
187+
inline MidiInterface& setHandleError(ErrorCallback fptr) { mErrorCallback = fptr; return *this; };
188+
inline MidiInterface& setHandleNoteOff(NoteOffCallback fptr) { mNoteOffCallback = fptr; return *this; };
189+
inline MidiInterface& setHandleNoteOn(NoteOnCallback fptr) { mNoteOnCallback = fptr; return *this; };
190+
inline MidiInterface& setHandleAfterTouchPoly(AfterTouchPolyCallback fptr) { mAfterTouchPolyCallback = fptr; return *this; };
191+
inline MidiInterface& setHandleControlChange(ControlChangeCallback fptr) { mControlChangeCallback = fptr; return *this; };
192+
inline MidiInterface& setHandleProgramChange(ProgramChangeCallback fptr) { mProgramChangeCallback = fptr; return *this; };
193+
inline MidiInterface& setHandleAfterTouchChannel(AfterTouchChannelCallback fptr) { mAfterTouchChannelCallback = fptr; return *this; };
194+
inline MidiInterface& setHandlePitchBend(PitchBendCallback fptr) { mPitchBendCallback = fptr; return *this; };
195+
inline MidiInterface& setHandleSystemExclusive(SystemExclusiveCallback fptr) { mSystemExclusiveCallback = fptr; return *this; };
196+
inline MidiInterface& setHandleTimeCodeQuarterFrame(TimeCodeQuarterFrameCallback fptr) { mTimeCodeQuarterFrameCallback = fptr; return *this; };
197+
inline MidiInterface& setHandleSongPosition(SongPositionCallback fptr) { mSongPositionCallback = fptr; return *this; };
198+
inline MidiInterface& setHandleSongSelect(SongSelectCallback fptr) { mSongSelectCallback = fptr; return *this; };
199+
inline MidiInterface& setHandleTuneRequest(TuneRequestCallback fptr) { mTuneRequestCallback = fptr; return *this; };
200+
inline MidiInterface& setHandleClock(ClockCallback fptr) { mClockCallback = fptr; return *this; };
201+
inline MidiInterface& setHandleStart(StartCallback fptr) { mStartCallback = fptr; return *this; };
202+
inline MidiInterface& setHandleTick(TickCallback fptr) { mTickCallback = fptr; return *this; };
203+
inline MidiInterface& setHandleContinue(ContinueCallback fptr) { mContinueCallback = fptr; return *this; };
204+
inline MidiInterface& setHandleStop(StopCallback fptr) { mStopCallback = fptr; return *this; };
205+
inline MidiInterface& setHandleActiveSensing(ActiveSensingCallback fptr) { mActiveSensingCallback = fptr; return *this; };
206+
inline MidiInterface& setHandleSystemReset(SystemResetCallback fptr) { mSystemResetCallback = fptr; return *this; };
207+
208+
inline MidiInterface& disconnectCallbackFromType(MidiType inType);
209209

210210
private:
211211
void launchCallback();
@@ -239,9 +239,9 @@ class MidiInterface
239239
inline Thru::Mode getFilterMode() const;
240240
inline bool getThruState() const;
241241

242-
inline void turnThruOn(Thru::Mode inThruFilterMode = Thru::Full);
243-
inline void turnThruOff();
244-
inline void setThruFilterMode(Thru::Mode inThruFilterMode);
242+
inline MidiInterface& turnThruOn(Thru::Mode inThruFilterMode = Thru::Full);
243+
inline MidiInterface& turnThruOff();
244+
inline MidiInterface& setThruFilterMode(Thru::Mode inThruFilterMode);
245245

246246
private:
247247
void thruFilter(byte inChannel);

0 commit comments

Comments
 (0)