Control Surface main
MIDI Control Surface library for Arduino


This is an example that demonstrates how to extend the library using your own MIDI USB backend. It defines a class for sending and receiving raw MIDI USB packets that can be plugged into Control Surface's USBMIDI_Interface class.

See also
AVR, AVR USB, Nano Every, Due, Nano 33 IoT, Nano 33 BLE, Pi Pico, Teensy 3.x, ESP32






Written by PieterP, 2022-05-28

// This class defines how to send and receive MIDI data using your custom
// backend.
struct MyUSBMIDIBackend {
// USB MIDI packages are 4 bytes.
using MIDIUSBPacket_t = AH::Array<uint8_t, 4>;
// This method is optional. It is called once upon initialization.
void begin() { Serial.begin(115200); }
// Read a single packet. Return a packet of all zeros if there is no packet.
MIDIUSBPacket_t read() { return MIDIUSBPacket_t {}; }
// Write a single packet to the output buffer.
void write(MIDIUSBPacket_t p) { Serial << AH::HexDump( << endl; }
// Transmit the output buffer immediately.
void sendNow() { Serial.flush(); }
// Should the sendNow method be called after each message? Return false if
// your backend can send the data in the output buffer asynchronously and
// automatically. Return true if that's not the case, and an explicit call to
// sendNow is required.
bool preferImmediateSend() { return false; }
// This is the actual MIDI interface that makes use of the backend defined above.
struct MyUSBMIDI_Interface : GenericUSBMIDI_Interface<MyUSBMIDIBackend> {
MyUSBMIDI_Interface() = default;
using MIDIUSBPacket_t = MyUSBMIDIBackend::MIDIUSBPacket_t;
// Instantiate the MIDI interface to use.
MyUSBMIDI_Interface midi;
void setup() {
void loop() {
static Timer<millis> timer {1000};
if (timer)
// As an example, send a MIDI timing clock message every second.
The main header file that includes all Control-Surface header files.
Control_Surface_ & Control_Surface
A predefined instance of the Control Surface to use in the Arduino sketches.
void begin()
Initialize the Control_Surface.
void loop()
Update all MIDI elements, send MIDI events and read MIDI input.
A class for MIDI interfaces sending MIDI messages over a USB MIDI connection.
void sendRealTime(MIDIMessageType rt, Cable cable=CABLE_1)
Send a MIDI Real-Time message.
Print & endl(Print &printer)
Definition: PrintStream.cpp:27
T * begin()
Get a pointer to the first element.
Definition: Array.hpp:78