Control Surface new-input
MIDI Control Surface library for Arduino


This is an example on how to attach your own callbacks for receiving MIDI input.

AVR, AVR USB, Nano Every, Due, Nano 33 IoT, Nano 33 BLE, Teensy 3.x, ESP32






See also

Written by PieterP, 2020-06-11

#include <Control_Surface.h> // Include the Control Surface library
// The MIDI over USB interface to use
// Custom MIDI callback that prints incoming messages.
struct MyMIDI_Callbacks : MIDI_Callbacks {
// Callback for channel messages (notes, control change, pitch bend, etc.).
// Print the message
Serial << F("Channel message: ") //
<< hex << cm.header << ' ' << cm.data1 << ' ' << cm.data2 << dec //
<< F(" on cable ") << cm.cable.getOneBased() << endl;
// Callback for system exclusive messages
// Print the message
Serial << F("System Exclusive message: [") << se.length << "] " //
<< AH::HexDump(, se.length) //
<< F(" on cable ") << se.cable.getOneBased() << endl;
// Callback for real-time messages
// Print the message
Serial << F("Real-time message: ") //
<< hex << rt.message << dec //
<< F(" on cable ") << rt.cable.getOneBased() << endl;
} callback;
void setup() {
Serial.begin(115200); // For printing the messages
midi.begin(); // Initialize the MIDI interface
midi.setCallbacks(callback); // Attach the custom callback
void loop() {
midi.update(); // Continuously handle MIDI input
The main header file that includes all Control-Surface header files.
constexpr uint8_t getOneBased() const
Get the cable as an integer.
Definition: Cable.hpp:36
void update() override
Read the MIDI interface and call the callback if a message was received.
A class for callbacks from MIDI input.
virtual void onChannelMessage(MIDI_Interface &, ChannelMessage)
Callback for incoming MIDI Channel Messages (notes, control change, pitch bend, etc....
virtual void onSysExMessage(MIDI_Interface &, SysExMessage)
Callback for incoming MIDI System Exclusive Messages.
virtual void onRealTimeMessage(MIDI_Interface &, RealTimeMessage)
Callback for incoming MIDI Real-Time Messages.
An abstract class for MIDI interfaces.
void begin() override
Initialize the MIDI Interface.
void setCallbacks(MIDI_Callbacks *cb)
Set the callbacks that will be called when a MIDI message is received.
A class for MIDI interfaces sending MIDI messages over a USB MIDI connection.
Print & hex(Print &printer)
Definition: PrintStream.cpp:62
Print & endl(Print &printer)
Definition: PrintStream.cpp:27
Print & dec(Print &printer)
Definition: PrintStream.cpp:77
uint8_t data2
First MIDI data byte.
uint8_t header
MIDI status byte (message type and channel).
uint8_t data1
First MIDI data byte.
Cable cable
USB MIDI cable number;.
const uint8_t * data