Tips & Tricks #186
franky47
started this conversation in
Show and tell
Replies: 1 comment
-
You can use lambda/anonymous functions for the callbacks? Here is how:
lambda/anonymous functions do not use more (or less) RAM than using regular C-style callbacks. [a] uses the exact same amount of RAM as [b] and do not run faster or slower. The main advantage IMHO is reduces c++ function decoration and thus less verbose. [a] #include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
void setup()
{
MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) {
Serial.println("NoteOn");
});
MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) {
Serial.println("NoteOff");
});
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop()
{
MIDI.read();
} [b] #include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
void handleNoteOn(byte channel, byte pitch, byte velocity)
{
Serial.println("NoteOn");
}
void handleNoteOff(byte channel, byte pitch, byte velocity)
{
Serial.println("NoteOff");
}
void setup()
{
MIDI.setHandleNoteOn(handleNoteOn); // Put only the name of the function
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
}
void loop()
{
MIDI.read();
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If you find clever ways of doing things, in code or in solder, post them here !
Beta Was this translation helpful? Give feedback.
All reactions